Search in sources :

Example 61 with InputSource

use of org.xml.sax.InputSource in project XobotOS by xamarin.

the class ExpatParser method handleExternalEntity.

/**
     * Handles an external entity.
     *
     * @param context to be passed back to Expat when we parse the entity
     * @param publicId the publicId of the entity
     * @param systemId the systemId of the entity
     */
/*package*/
void handleExternalEntity(String context, String publicId, String systemId) throws SAXException, IOException {
    EntityResolver entityResolver = xmlReader.entityResolver;
    if (entityResolver == null) {
        return;
    }
    /*
         * The spec. is terribly under-specified here. It says that if the
         * systemId is a URL, we should try to resolve it, but it doesn't
         * specify how to tell whether or not the systemId is a URL let alone
         * how to resolve it.
         *
         * Other implementations do various insane things. We try to keep it
         * simple: if the systemId parses as a URI and it's relative, we try to
         * resolve it against the parent document's systemId. If anything goes
         * wrong, we go with the original systemId. If crazybob had designed
         * the API, he would have left all resolving to the EntityResolver.
         */
    if (this.systemId != null) {
        try {
            URI systemUri = new URI(systemId);
            if (!systemUri.isAbsolute() && !systemUri.isOpaque()) {
                // It could be relative (or it may not be a URI at all!)
                URI baseUri = new URI(this.systemId);
                systemUri = baseUri.resolve(systemUri);
                // Replace systemId w/ resolved URI
                systemId = systemUri.toString();
            }
        } catch (Exception e) {
            System.logI("Could not resolve '" + systemId + "' relative to" + " '" + this.systemId + "' at " + locator, e);
        }
    }
    InputSource inputSource = entityResolver.resolveEntity(publicId, systemId);
    if (inputSource == null) {
        /*
             * The spec. actually says that we should try to treat systemId
             * as a URL and download and parse its contents here, but an
             * entity resolver can easily accomplish the same by returning
             * new InputSource(systemId).
             *
             * Downloading external entities by default would result in several
             * unwanted DTD downloads, not to mention pose a security risk
             * when parsing untrusted XML -- see for example
             * http://archive.cert.uni-stuttgart.de/bugtraq/2002/10/msg00421.html --
             * so we just do nothing instead. This also enables the user to
             * opt out of entity parsing when using
             * {@link org.xml.sax.helpers.DefaultHandler}, something that
             * wouldn't be possible otherwise.
             */
        return;
    }
    String encoding = pickEncoding(inputSource);
    int pointer = createEntityParser(this.pointer, context);
    try {
        EntityParser entityParser = new EntityParser(encoding, xmlReader, pointer, inputSource.getPublicId(), inputSource.getSystemId());
        parseExternalEntity(entityParser, inputSource);
    } finally {
        releaseParser(pointer);
    }
}
Also used : InputSource(org.xml.sax.InputSource) EntityResolver(org.xml.sax.EntityResolver) URI(java.net.URI) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Example 62 with InputSource

use of org.xml.sax.InputSource in project WordPress-Android by wordpress-mobile.

the class HtmlToSpannedConverter method convert.

public Spanned convert() {
    mReader.setContentHandler(this);
    try {
        mReader.parse(new InputSource(new StringReader(mSource)));
    } catch (IOException e) {
        // We are reading from a string. There should not be IO problems.
        throw new RuntimeException(e);
    } catch (SAXException e) {
        // TagSoup doesn't throw parse exceptions.
        throw new RuntimeException(e);
    }
    // Fix flags and range for paragraph-type markup.
    Object[] obj = mSpannableStringBuilder.getSpans(0, mSpannableStringBuilder.length(), ParagraphStyle.class);
    for (int i = 0; i < obj.length; i++) {
        int start = mSpannableStringBuilder.getSpanStart(obj[i]);
        int end = mSpannableStringBuilder.getSpanEnd(obj[i]);
        // If the last line of the range is blank, back off by one.
        if (end - 2 >= 0) {
            if (mSpannableStringBuilder.charAt(end - 1) == '\n' && mSpannableStringBuilder.charAt(end - 2) == '\n') {
                end--;
            }
        }
        if (end == start) {
            mSpannableStringBuilder.removeSpan(obj[i]);
        } else {
            try {
                mSpannableStringBuilder.setSpan(obj[i], start, end, Spannable.SPAN_PARAGRAPH);
            } catch (Exception e) {
            }
        }
    }
    return mSpannableStringBuilder;
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) IOException(java.io.IOException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 63 with InputSource

use of org.xml.sax.InputSource in project XobotOS by xamarin.

the class XMLParser method importPrefs.

/***************************************************************************
     * utilities for Preferences import
     **************************************************************************/
static void importPrefs(InputStream in) throws IOException, InvalidPreferencesFormatException {
    try {
        // load XML document
        Document doc = builder.parse(new InputSource(in));
        // check preferences' export version
        Element preferences;
        preferences = doc.getDocumentElement();
        String version = preferences.getAttribute("EXTERNAL_XML_VERSION");
        if (version != null && Float.parseFloat(version) > XML_VERSION) {
            throw new InvalidPreferencesFormatException("Preferences version " + version + " is not supported");
        }
        // check preferences root's type
        Element root = (Element) preferences.getElementsByTagName("root").item(0);
        Preferences prefsRoot = null;
        String type = root.getAttribute("type");
        if (type.equals("user")) {
            prefsRoot = Preferences.userRoot();
        } else {
            prefsRoot = Preferences.systemRoot();
        }
        // load node
        loadNode(prefsRoot, root);
    } catch (FactoryConfigurationError e) {
        throw new InvalidPreferencesFormatException(e);
    } catch (SAXException e) {
        throw new InvalidPreferencesFormatException(e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) FactoryConfigurationError(javax.xml.parsers.FactoryConfigurationError) SAXException(org.xml.sax.SAXException)

Example 64 with InputSource

use of org.xml.sax.InputSource in project XobotOS by xamarin.

the class DocumentBuilder method parse.

/**
     * Parse the content of the given <code>InputStream</code> as an
     * XML document and return a new DOM {@link Document} object.
     * An <code>IllegalArgumentException</code> is thrown if the
     * <code>InputStream</code> is null.
     *
     * @param is InputStream containing the content to be parsed.
     * @param systemId Provide a base for resolving relative URIs.
     * @return A new DOM Document object.
     * @exception IOException If any IO errors occur.
     * @exception SAXException If any parse errors occur.
     * @see org.xml.sax.DocumentHandler
     */
public Document parse(InputStream is, String systemId) throws SAXException, IOException {
    if (is == null) {
        throw new IllegalArgumentException("InputStream cannot be null");
    }
    InputSource in = new InputSource(is);
    in.setSystemId(systemId);
    return parse(in);
}
Also used : InputSource(org.xml.sax.InputSource)

Example 65 with InputSource

use of org.xml.sax.InputSource in project XobotOS by xamarin.

the class DocumentBuilder method parse.

/**
     * Parse the content of the given file as an XML document
     * and return a new DOM {@link Document} object.
     * An <code>IllegalArgumentException</code> is thrown if the
     * <code>File</code> is <code>null</code> null.
     *
     * @param f The file containing the XML to parse.
     * @exception IOException If any IO errors occur.
     * @exception SAXException If any parse errors occur.
     * @see org.xml.sax.DocumentHandler
     * @return A new DOM Document object.
     */
public Document parse(File f) throws SAXException, IOException {
    if (f == null) {
        throw new IllegalArgumentException("File cannot be null");
    }
    String escapedURI = FilePathToURI.filepath2URI(f.getAbsolutePath());
    if (DEBUG) {
        System.out.println("Escaped URI = " + escapedURI);
    }
    InputSource in = new InputSource(escapedURI);
    return parse(in);
}
Also used : InputSource(org.xml.sax.InputSource)

Aggregations

InputSource (org.xml.sax.InputSource)1124 StringReader (java.io.StringReader)401 IOException (java.io.IOException)304 Document (org.w3c.dom.Document)282 SAXException (org.xml.sax.SAXException)281 DocumentBuilder (javax.xml.parsers.DocumentBuilder)262 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)213 XMLReader (org.xml.sax.XMLReader)194 Test (org.junit.Test)160 InputStream (java.io.InputStream)158 NodeList (org.w3c.dom.NodeList)145 Element (org.w3c.dom.Element)144 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)143 ByteArrayInputStream (java.io.ByteArrayInputStream)103 SAXParser (javax.xml.parsers.SAXParser)103 SAXSource (javax.xml.transform.sax.SAXSource)95 SAXParserFactory (javax.xml.parsers.SAXParserFactory)90 File (java.io.File)82 Node (org.w3c.dom.Node)82 URL (java.net.URL)65