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.lockss.plugin.clockss.wolterskluwer.WoltersKluwerXPathXmlMetadataParser.java

@Override
protected InputSource makeInputSource(CachedUrl cu) throws UnsupportedEncodingException {

    Pair<Reader, String> sgmlReaderPair = makeInputSourceReader(cu);
    String sgmlReader_cset = sgmlReaderPair.getRight();
    if (sgmlReader_cset != Constants.ENCODING_UTF_8) {
        log.debug3("WARNING: WoltersKluwer sgml input NOT UTF");
    }/*ww w  . j  a  va 2  s  .co  m*/
    Reader xmlReader = new WoltersKluwerSgmlAdapter(sgmlReaderPair.getLeft());
    InputSource is = new InputSource(xmlReader);
    is.setEncoding(sgmlReader_cset);
    return is;
}

From source file:org.lockss.plugin.clockss.XPathXmlMetadataParser.java

/**
 * <p>/* w w  w  .jav  a 2s  . c  om*/
 * Subclasses can override this method to make an {@link InputSource} from the
 * given {@link CachedUrl}.
 * </p>
 * <p>
 * By default, this class uses the cached URL's
 * {@link CachedUrl#getUnfilteredInputStream()} method and, if
 * {@link #isDoXmlFiltering()} return true, wraps it in a
 * {@link XmlFilteringInputStream}, setting the encoding of the input source
 * to the value returned by the util {@link CharsetUtil#guessCharsetName()} and
 * if that returns null, to {@link CachedUrl#getEncoding()}.
 * </p>
 * 
 * @param cu
 *          A cached URL.
 * @return An input source for the cached URL's data stream.
 * @throws IOException
 *           if an I/O exception occurs.
 * @since 1.67
 */
protected InputSource makeInputSource(CachedUrl cu) throws IOException {

    // first create a reader so child classes can 
    // do filtering on the reader if they need to
    Pair<Reader, String> iReaderPair = makeInputSourceReader(cu);
    InputSource iSource = new InputSource(iReaderPair.getLeft());
    iSource.setEncoding(iReaderPair.getRight());
    return iSource;
}

From source file:org.moe.natjgen.XcodeFullDocumentation.java

public XcodeFullDocumentation(Indexer indexer, String lang, String type, String element, String name,
        TextEditGroup editGroup) throws IOException {
    this.editGroup = editGroup;

    if (indexer.getXcodePath() == null) {
        throw new IOException("Xcode path could not be retrieved");
    }/*from w w  w  . ja  va  2s  . c  o  m*/

    File file = null;
    ArrayList<String> docsets = indexer.getConfiguration().getDocsets();
    for (String docset : docsets) {
        File ds = new File(indexer.getXcodePath(), "Documentation/DocSets");
        ds = new File(ds, docset);
        ds = new File(ds, DocTokensBase + "/" + lang + "/" + type + "/" + element + "/" + name + ".xml");
        if (ds.exists()) {
            file = ds;
            break;
        }
    }

    if (file == null) {
        throw new IOException("no documentation found for /" + lang + "/" + type + "/" + element + "/" + name);
    }

    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();

        DefaultHandler handler = new DefaultHandler() {

            private final int kROOT = 0;
            private final int kAbstract = 1;
            private final int kDiscussion = 2;
            private final int kParameters = 3;
            private final int kParameter = 31;
            private final int kParameter_Term = 311;
            private final int kParameter_Definition = 312;
            private final int kResultDescription = 4;
            private final int kAvailability = 5;
            private final int kAvailabilityItem = 51;
            private final int kDeclaration = 6;
            private final int kCodeLine = 61;
            private int phase = kROOT;

            private StringBuilder capture;

            private final int maxStackDepth = 1024;
            private boolean contentStack[] = new boolean[maxStackDepth];
            private int contentStackDepth = 0;

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes)
                    throws SAXException {
                switch (phase) {
                case kAbstract:
                case kDiscussion:
                case kResultDescription:
                case kParameter_Definition:
                    contentStack[contentStackDepth] = true;
                    ++contentStackDepth;
                    for (HTMLTagReplacement replacement : replacements) {
                        if (qName.equalsIgnoreCase(replacement.xml)) {
                            contentStack[contentStackDepth - 1] = replacement.param;
                            capture.append(replacement.openPlaceholder);
                            break;
                        }
                    }
                case kROOT:
                    if (qName.equalsIgnoreCase("Abstract")) {
                        phase = kAbstract;
                        capture = new StringBuilder();
                    } else if (qName.equalsIgnoreCase("Discussion")) {
                        phase = kDiscussion;
                        capture = new StringBuilder();
                    } else if (qName.equalsIgnoreCase("Parameters")) {
                        phase = kParameters;
                    } else if (qName.equalsIgnoreCase("ResultDescription")) {
                        phase = kResultDescription;
                        capture = new StringBuilder();
                    } else if (qName.equalsIgnoreCase("Availability")) {
                        phase = kAvailability;
                    } else if (qName.equalsIgnoreCase("Declaration")) {
                        phase = kDeclaration;
                    }
                    break;
                case kParameters:
                    if (qName.equalsIgnoreCase("Parameter")) {
                        phase = kParameter;
                    }
                    break;
                case kParameter:
                    if (qName.equalsIgnoreCase("Term")) {
                        phase = kParameter_Term;
                        capture = new StringBuilder();
                    } else if (qName.equalsIgnoreCase("Definition")) {
                        phase = kParameter_Definition;
                        capture = new StringBuilder();
                    }
                    break;
                case kAvailability:
                    if (qName.equalsIgnoreCase("AvailabilityItem")) {
                        phase = kAvailabilityItem;
                        capture = new StringBuilder();
                    }
                    break;
                case kDeclaration:
                    if (qName.equalsIgnoreCase("CodeLine")) {
                        phase = kCodeLine;
                        capture = new StringBuilder();
                    }
                    break;

                default:
                    break;
                }
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                if (contentStackDepth > 0) {
                    --contentStackDepth;
                    for (HTMLTagReplacement replacement : replacements) {
                        if (qName.equalsIgnoreCase(replacement.xml)) {
                            capture.append(replacement.closePlaceholder);
                            break;
                        }
                    }
                } else {
                    switch (phase) {
                    case kAbstract:
                        if (qName.equalsIgnoreCase("Abstract")) {
                            Abstract = capture.toString();
                            capture = null;
                            phase = kROOT;
                        }
                        break;
                    case kDiscussion:
                        if (qName.equalsIgnoreCase("Discussion")) {
                            Discussion = capture.toString();
                            capture = null;
                            phase = kROOT;
                        }
                        break;
                    case kParameters:
                        if (qName.equalsIgnoreCase("Parameters")) {
                            phase = kROOT;
                        }
                        break;
                    case kResultDescription:
                        if (qName.equalsIgnoreCase("ResultDescription")) {
                            ResultDescription = capture.toString();
                            capture = null;
                            phase = kROOT;
                        }
                        break;
                    case kAvailability:
                        if (qName.equalsIgnoreCase("Availability")) {
                            phase = kROOT;
                        }
                        break;
                    case kDeclaration:
                        if (qName.equalsIgnoreCase("Declaration")) {
                            phase = kROOT;
                        }
                        break;
                    case kParameter:
                        if (qName.equalsIgnoreCase("Parameter")) {
                            phase = kParameters;
                        }
                        break;
                    case kParameter_Term:
                        if (qName.equalsIgnoreCase("Term")) {
                            ParameterNames.add(capture.toString());
                            capture = null;
                            phase = kParameter;
                        }
                        break;
                    case kParameter_Definition:
                        if (qName.equalsIgnoreCase("Definition")) {
                            ParameterDescriptions.add(capture.toString());
                            capture = null;
                            phase = kParameter;
                        }
                        break;
                    case kAvailabilityItem:
                        if (qName.equalsIgnoreCase("AvailabilityItem")) {
                            Availabilities.add(capture.toString());
                            capture = null;
                            phase = kAvailability;
                        }
                        break;
                    case kCodeLine:
                        if (qName.equalsIgnoreCase("CodeLine")) {
                            Declaration = capture.toString();
                            capture = null;
                            phase = kDeclaration;
                        }
                        break;

                    default:
                        break;
                    }
                }
            }

            @Override
            public void characters(char[] ch, int start, int length) throws SAXException {
                if (capture != null) {
                    if (contentStackDepth == 0 || contentStack[contentStackDepth - 1]) {
                        capture.append(new String(ch, start, length));
                    }
                }
            }
        };

        InputStream inputStream = new FileInputStream(file);
        Reader reader = new InputStreamReader(inputStream, "UTF-8");

        InputSource is = new InputSource(reader);
        is.setEncoding("UTF-8");

        parser.parse(is, handler);

    } catch (Exception e) {
        throw new IOException("failed to parse doc", e);
    }
}

From source file:org.n52.sos.XmlToExiConverter.java

protected void decode(String fileName) {
    try (InputStream exiIS = FileUtils.openInputStream(getFile(fileName, EXI_EXTENSION));
            OutputStream os = FileUtils.openOutputStream(getFile(fileName, XML_EXTENSION_2))) {

        Reader reader = new InputStreamReader(exiIS, "ISO-8859-1");
        InputSource is = new InputSource(reader);
        is.setEncoding("ISO-8859-1");

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();

        EXISource exiSource = new EXISource();
        XMLReader exiReader = exiSource.getXMLReader();
        SAXSource saxSource = new SAXSource(is);
        //            SAXSource saxSource = new SAXSource(new InputSource(exiIS));
        exiSource.setXMLReader(exiReader);
        transformer.transform(saxSource, new StreamResult(os));
    } catch (Exception e) {
        System.out.println(e.getStackTrace());
    }/* w w w .  ja v  a 2 s . co m*/
}

From source file:org.ojbc.util.camel.helper.OJBUtils.java

public static SAXSource createSaxSource(String xml) {

    InputSource inputSource = new InputSource(new ByteArrayInputStream(xml.getBytes()));
    inputSource.setEncoding(CharEncoding.UTF_8);

    return new SAXSource(inputSource);
}

From source file:org.ojbc.util.camel.helper.OJBUtils.java

public static SAXSource createSaxSource(InputStream inSream) {

    InputSource inputSource = new InputSource(inSream);
    inputSource.setEncoding(CharEncoding.UTF_8);

    return new SAXSource(inputSource);
}

From source file:org.ojbc.util.camel.processor.accesscontrol.XacmlRequestTransformer.java

private SAXSource getSaxSourceForXmlRequestContent(String requestContent) {

    ByteArrayInputStream requestByteInStream = new ByteArrayInputStream(requestContent.getBytes());
    InputSource requestInputSource = new InputSource(requestByteInStream);
    requestInputSource.setEncoding(CharEncoding.UTF_8);
    SAXSource requestXmlSaxInputSource = new SAXSource(requestInputSource);

    log.info("Got saxSource cor xml request string");

    return requestXmlSaxInputSource;
}

From source file:org.ojbc.util.camel.processor.accesscontrol.XacmlRequestTransformer.java

private SAXSource getSaxSourceForXsl(String xslFileStringContent, String xslUrlExtFormForSystemId) {

    try {//  w w  w  .  ja  v  a 2 s  . c  om

        InputSource inputSource = new InputSource(xslFileStringContent);
        inputSource.setEncoding(CharEncoding.UTF_8);

        SAXSource saxInputSource = new SAXSource(inputSource);

        //need to setSystemId because xsl needs this set in order for to <import> to know where to look to load relative paths
        saxInputSource.setSystemId(xslUrlExtFormForSystemId);

        return saxInputSource;

    } catch (Exception e) {
        throw new RuntimeException("Unable to read XSL: " + xslFileStringContent, e);
    }
}

From source file:org.ojbc.util.xml.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.web.portal.services.SearchResultConverter.java

private SAXSource createSource(InputStream inputStream) {
    InputSource inputSource = new InputSource(inputStream);
    inputSource.setEncoding(CharEncoding.UTF_8);
    return new SAXSource(inputSource);
}