Example usage for org.xml.sax InputSource setEncoding

List of usage examples for org.xml.sax InputSource setEncoding

Introduction

In this page you can find the example usage for org.xml.sax InputSource setEncoding.

Prototype

public void setEncoding(String encoding) 

Source Link

Document

Set the character encoding, if known.

Usage

From source file:org.ojbc.web.portal.services.XslFormattersTest.java

private SAXSource createSourceAndSetSystemId(Resource inputStream) {
    try {// w  ww.  j a va2s.co  m
        InputSource inputSource = new InputSource(inputStream.getInputStream());
        inputSource.setEncoding(CharEncoding.UTF_8);
        inputSource.setSystemId(inputStream.getURL().toExternalForm());
        return new SAXSource(inputSource);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.ojbc.web.portal.services.XsltTransformerServiceTest.java

private SAXSource createSource(String xml) {
    InputSource inputSource = new InputSource(new ByteArrayInputStream(xml.getBytes()));
    inputSource.setEncoding(CharEncoding.UTF_8);
    return new SAXSource(inputSource);
}

From source file:org.ojbc.xslt.IncidentReportingTransformerServiceTest.java

private SAXSource createSource(String xml) {
    InputSource inputSource = new InputSource(new ByteArrayInputStream(xml.getBytes()));
    inputSource.setEncoding(org.apache.commons.lang.CharEncoding.UTF_8);
    return new SAXSource(inputSource);
}

From source file:org.openfact.common.converts.DocumentUtils.java

/**
 * Convert an inputStream to a Document Object
 *
 * @param inputStream/*  w w  w .  j a v  a 2  s .c  om*/
 *            The inputstream to convert
 * @return a Document Object
 * @throws IOException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
public static Document getInputStreamToDocument(InputStream inputStream)
        throws IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    // dbf.setAttribute("http://xml.org/sax/features/namespaces",
    // Boolean.TRUE);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Reader reader = new InputStreamReader(inputStream, "ISO8859_1");
    InputSource is = new InputSource(reader);
    is.setEncoding("ISO-8859-1");
    Document doc = db.parse(is);
    return doc;
}

From source file:org.pentaho.platform.plugin.action.jfreereport.JFreeReportComponent.java

private MasterReport getReportFromResource() throws ResourceException, IOException {
    JFreeReportAction jFreeReportAction = (JFreeReportAction) getActionDefinition();
    MasterReport report = null;/*  w  ww.ja  v  a 2  s  . com*/
    Object reportDefinition = jFreeReportAction.getReportDefinition();
    IActionSequenceResource resource = null;
    if (reportDefinition instanceof ActionResource) {
        resource = getResource(((ActionResource) reportDefinition).getName());
    }
    if (resource != null) {
        if (resource.getSourceType() == IActionResource.XML) {
            String repDef = resource.getAddress();
            ReportGenerator generator = ReportGenerator.createInstance();

            // add the runtime context so that PentahoResourceData class can get access to the solution repo
            // generator.setObject(PentahoResourceData.PENTAHO_RUNTIME_CONTEXT_KEY, getRuntimeContext());

            // Read the encoding from the XML file - see BISERVER-895
            final String encoding = XmlHelper.getEncoding(repDef, null);
            ByteArrayInputStream inStream = new ByteArrayInputStream(repDef.getBytes(encoding));
            InputSource repDefInputSource = new InputSource(inStream);
            repDefInputSource.setEncoding(encoding);
            report = generator.parseReport(repDefInputSource, getDefinedResourceURL(null));
        } else {
            report = parseReport(resource);
        }
    }
    return report;
}

From source file:org.pentaho.platform.plugin.action.jfreereport.JFreeReportComponent.java

protected MasterReport createReport(final String reportDefinition) throws ResourceException, IOException {
    ReportGenerator generator = ReportGenerator.createInstance();

    // add the runtime context so that PentahoResourceData class can get access to the solution repo
    // generator.setObject(PentahoResourceData.PENTAHO_RUNTIME_CONTEXT_KEY, getRuntimeContext());

    URL url = null;/*w w w.  ja v  a 2  s.  co m*/
    IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
    try {
        url = new URL(requestContext.getContextPath()); //$NON-NLS-1$ //$NON-NLS-2$ 
    } catch (Exception e) {
        // a null URL is ok
        warn(Messages.getInstance().getString("JFreeReportLoadComponent.WARN_COULD_NOT_CREATE_URL")); //$NON-NLS-1$
    }

    // Read the encoding from the XML file - see BISERVER-895
    final String encoding = XmlHelper.getEncoding(reportDefinition, null);
    ByteArrayInputStream inStream = new ByteArrayInputStream(reportDefinition.getBytes(encoding));
    InputSource reportDefinitionInputSource = new InputSource(inStream);
    reportDefinitionInputSource.setEncoding(encoding);
    return generator.parseReport(reportDefinitionInputSource, getDefinedResourceURL(url));
}

From source file:org.seagatesoft.sde.tagtreebuilder.DOMParserTagTreeBuilder.java

public TagTree buildTagTree(InputSource inputSource, boolean ignoreFormattingTags, String htmlDocument)
        throws IOException, SAXException {
    WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24); // Chrome not working
    HtmlPage page = null;/*from w w w  .j a  va2s . c  om*/
    try {
        page = processWebPage(htmlDocument, webClient);
    } catch (Exception e) {
        System.out.println("Get page error");
    }
    TagTree tree = null;
    if (ignoreFormattingTags) {
        tagNodeCreator = new IgnoreFormattingTagsTagNodeCreator();
    } else {
        tagNodeCreator = new DefaultTagNodeCreator();
    }
    DOMParser parser = new DOMParser();
    try {
        //?  
        parser.setFeature("http://xml.org/sax/features/validation", true);
        parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
        parser.setProperty("http://cyberneko.org/html/properties/default-encoding", "UTF-8");
        parser.setFeature("http://xml.org/sax/features/namespaces", false);
        inputSource.setEncoding("UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    org.jsoup.nodes.Document doc = Jsoup.parse(page.asXml());
    File temprFile = File.createTempFile("parsedHtml", ".html");
    temprFile.deleteOnExit();
    FileOutputStream fos = new FileOutputStream(temprFile);
    fos.write(doc.html().getBytes("UTF-8"));
    fos.close();
    BufferedReader in = new BufferedReader(
            new InputStreamReader(new FileInputStream(temprFile.getAbsolutePath()), "UTF8"));
    parser.parse(new InputSource(in));
    org.w3c.dom.Document documentNode = parser.getDocument();
    //baseURI = documentNode.getBaseURI();
    //Pattern baseDirectoryPattern = Pattern.compile("^(.*/)[^/]*$");
    //Matcher matcher = baseDirectoryPattern.matcher( baseURI );

    /*// dapatkan BaseURI dari dokumen HTML ini
    if ( matcher.lookingAt() )
    {
       baseURI = matcher.group(1);
    }*/

    Node bodyNode = documentNode.getElementsByTagName("BODY").item(0);
    TagNode rootTagNode = new TagNode();
    tree = new TagTree();
    tree.setRoot(rootTagNode);
    rootTagNode.setTagElement(HTMLElements.getElement(bodyNode.getNodeName()).code);
    tree.addTagNodeAtLevel(rootTagNode);
    Node child = bodyNode.getFirstChild();

    while (child != null) {
        tagNodeCreator.createTagNodes(child, rootTagNode, tree);
        child = child.getNextSibling();
    }
    tree.assignNodeNumber();
    return tree;
}

From source file:org.slc.sli.ingestion.parser.impl.EdfiRecordParserImpl2.java

private void parseAndValidate(InputStream input, Schema schema) throws XmlParseException, IOException {
    ValidatorHandler vHandler = schema.newValidatorHandler();
    vHandler.setContentHandler(this);
    vHandler.setErrorHandler(this);

    InputSource is = new InputSource(new InputStreamReader(input, "UTF-8"));
    is.setEncoding("UTF-8");

    try {/*  w w w .  java 2  s  .  co m*/
        XMLReader parser = XMLReaderFactory.createXMLReader();
        parser.setContentHandler(vHandler);
        parser.setErrorHandler(this);

        vHandler.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);

        parser.setFeature("http://apache.org/xml/features/validation/id-idref-checking", false);
        parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);
        parser.setFeature("http://xml.org/sax/features/external-general-entities", false);
        parser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);

        parser.parse(is);
    } catch (SAXException e) {
        throw new XmlParseException(e.getMessage(), e);
    }
}

From source file:org.springframework.oxm.support.AbstractMarshaller.java

/**
 * Template method for handling {@code StreamSource}s.
 * <p>This implementation delegates to {@code unmarshalInputStream} or {@code unmarshalReader}.
 * @param streamSource the {@code StreamSource}
 * @return the object graph/*from  w  ww .  ja  v a2s .c  o  m*/
 * @throws IOException if an I/O exception occurs
 * @throws XmlMappingException if the given source cannot be mapped to an object
 */
protected Object unmarshalStreamSource(StreamSource streamSource) throws XmlMappingException, IOException {
    if (streamSource.getInputStream() != null) {
        if (isProcessExternalEntities() && isSupportDtd()) {
            return unmarshalInputStream(streamSource.getInputStream());
        } else {
            InputSource inputSource = new InputSource(streamSource.getInputStream());
            inputSource.setEncoding(getDefaultEncoding());
            return unmarshalSaxSource(new SAXSource(inputSource));
        }
    } else if (streamSource.getReader() != null) {
        if (isProcessExternalEntities() && isSupportDtd()) {
            return unmarshalReader(streamSource.getReader());
        } else {
            return unmarshalSaxSource(new SAXSource(new InputSource(streamSource.getReader())));
        }
    } else {
        return unmarshalSaxSource(new SAXSource(new InputSource(streamSource.getSystemId())));
    }
}

From source file:org.wisdom.content.jackson.JacksonSingleton.java

/**
 * Builds a new XML Document from the given input stream. The stream is not closed by this method,
 * and so you must close it./*from   ww w  .java  2  s.c o  m*/
 *
 * @param stream   the input stream, must not be {@literal null}
 * @param encoding the encoding, if {@literal null}, UTF-8 is used.
 * @return the built document
 * @throws java.io.IOException if the given stream is not a valid XML document,
 *                             or if the given encoding is not supported.
 */
@Override
public Document fromInputStream(InputStream stream, Charset encoding) throws IOException {
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();

        InputSource is = new InputSource(stream);
        if (encoding == null) {
            is.setEncoding(Charsets.UTF_8.name());
        } else {
            is.setEncoding(encoding.name());
        }

        return builder.parse(is); //NOSONAR The used factory is not exposed to XXE.

    } catch (ParserConfigurationException | SAXException e) {
        throw new IOException("Cannot parse the given XML document", e);
    }
}