Search in sources :

Example 31 with SAXParseException

use of org.xml.sax.SAXParseException in project sling by apache.

the class JspDocumentParser method startElement.

/*
     * Receives notification of the start of an element.
     *
     * This method assigns the given tag attributes to one of 3 buckets:
     *
     * - "xmlns" attributes that represent (standard or custom) tag libraries.
     * - "xmlns" attributes that do not represent tag libraries.
     * - all remaining attributes.
     *
     * For each "xmlns" attribute that represents a custom tag library, the
     * corresponding TagLibraryInfo object is added to the set of custom
     * tag libraries.
     */
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
    AttributesImpl taglibAttrs = null;
    AttributesImpl nonTaglibAttrs = null;
    AttributesImpl nonTaglibXmlnsAttrs = null;
    processChars();
    checkPrefixes(uri, qName, attrs);
    if (directivesOnly && !(JSP_URI.equals(uri) && localName.startsWith(DIRECTIVE_ACTION))) {
        return;
    }
    // jsp:text must not have any subelements
    if (JSP_URI.equals(uri) && TEXT_ACTION.equals(current.getLocalName())) {
        throw new SAXParseException(Localizer.getMessage("jsp.error.text.has_subelement"), locator);
    }
    startMark = new Mark(ctxt, path, locator.getLineNumber(), locator.getColumnNumber());
    if (attrs != null) {
        /*
             * Notice that due to a bug in the underlying SAX parser, the
             * attributes must be enumerated in descending order.
             */
        boolean isTaglib = false;
        for (int i = attrs.getLength() - 1; i >= 0; i--) {
            isTaglib = false;
            String attrQName = attrs.getQName(i);
            if (!attrQName.startsWith("xmlns")) {
                if (nonTaglibAttrs == null) {
                    nonTaglibAttrs = new AttributesImpl();
                }
                nonTaglibAttrs.addAttribute(attrs.getURI(i), attrs.getLocalName(i), attrs.getQName(i), attrs.getType(i), attrs.getValue(i));
            } else {
                if (attrQName.startsWith("xmlns:jsp")) {
                    isTaglib = true;
                } else {
                    String attrUri = attrs.getValue(i);
                    // TaglibInfo for this uri already established in
                    // startPrefixMapping
                    isTaglib = pageInfo.hasTaglib(attrUri);
                }
                if (isTaglib) {
                    if (taglibAttrs == null) {
                        taglibAttrs = new AttributesImpl();
                    }
                    taglibAttrs.addAttribute(attrs.getURI(i), attrs.getLocalName(i), attrs.getQName(i), attrs.getType(i), attrs.getValue(i));
                } else {
                    if (nonTaglibXmlnsAttrs == null) {
                        nonTaglibXmlnsAttrs = new AttributesImpl();
                    }
                    nonTaglibXmlnsAttrs.addAttribute(attrs.getURI(i), attrs.getLocalName(i), attrs.getQName(i), attrs.getType(i), attrs.getValue(i));
                }
            }
        }
    }
    Node node = null;
    if (tagDependentPending && JSP_URI.equals(uri) && localName.equals(BODY_ACTION)) {
        tagDependentPending = false;
        tagDependentNesting++;
        current = parseStandardAction(qName, localName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, startMark, current);
        return;
    }
    if (tagDependentPending && JSP_URI.equals(uri) && localName.equals(ATTRIBUTE_ACTION)) {
        current = parseStandardAction(qName, localName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, startMark, current);
        return;
    }
    if (tagDependentPending) {
        tagDependentPending = false;
        tagDependentNesting++;
    }
    if (tagDependentNesting > 0) {
        node = new Node.UninterpretedTag(qName, localName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, startMark, current);
    } else if (JSP_URI.equals(uri)) {
        node = parseStandardAction(qName, localName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, startMark, current);
    } else {
        node = parseCustomAction(qName, localName, uri, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, startMark, current);
        if (node == null) {
            node = new Node.UninterpretedTag(qName, localName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, startMark, current);
        } else {
            // custom action
            String bodyType = getBodyType((Node.CustomTag) node);
            if (scriptlessBodyNode == null && bodyType.equalsIgnoreCase(TagInfo.BODY_CONTENT_SCRIPTLESS)) {
                scriptlessBodyNode = node;
            } else if (TagInfo.BODY_CONTENT_TAG_DEPENDENT.equalsIgnoreCase(bodyType)) {
                tagDependentPending = true;
            }
        }
    }
    current = node;
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) SAXParseException(org.xml.sax.SAXParseException)

Example 32 with SAXParseException

use of org.xml.sax.SAXParseException in project sling by apache.

the class JspDocumentParser method processChars.

private void processChars() throws SAXException {
    if (charBuffer == null || directivesOnly) {
        return;
    }
    /*
         * JSP.6.1.1: All textual nodes that have only white space are to be
         * dropped from the document, except for nodes in a jsp:text element,
         * and any leading and trailing white-space-only textual nodes in a
         * jsp:attribute whose 'trim' attribute is set to FALSE, which are to
         * be kept verbatim.
         * JSP.6.2.3 defines white space characters.
         */
    boolean isAllSpace = true;
    if (!(current instanceof Node.JspText) && !(current instanceof Node.NamedAttribute)) {
        for (int i = 0; i < charBuffer.length(); i++) {
            if (!(charBuffer.charAt(i) == ' ' || charBuffer.charAt(i) == '\n' || charBuffer.charAt(i) == '\r' || charBuffer.charAt(i) == '\t')) {
                isAllSpace = false;
                break;
            }
        }
    }
    if (!isAllSpace && tagDependentPending) {
        tagDependentPending = false;
        tagDependentNesting++;
    }
    if (tagDependentNesting > 0) {
        if (charBuffer.length() > 0) {
            new Node.TemplateText(charBuffer.toString(), startMark, current);
        }
        startMark = new Mark(ctxt, path, locator.getLineNumber(), locator.getColumnNumber());
        charBuffer = null;
        return;
    }
    if ((current instanceof Node.JspText) || (current instanceof Node.NamedAttribute) || !isAllSpace) {
        int line = startMark.getLineNumber();
        int column = startMark.getColumnNumber();
        CharArrayWriter ttext = new CharArrayWriter();
        int lastCh = 0, elType = 0;
        for (int i = 0; i < charBuffer.length(); i++) {
            int ch = charBuffer.charAt(i);
            if (ch == '\n') {
                column = 1;
                line++;
            } else {
                column++;
            }
            if ((lastCh == '$' || lastCh == '#') && ch == '{') {
                elType = lastCh;
                if (ttext.size() > 0) {
                    new Node.TemplateText(ttext.toString(), startMark, current);
                    ttext = new CharArrayWriter();
                    //We subtract two from the column number to
                    //account for the '[$,#]{' that we've already parsed
                    startMark = new Mark(ctxt, path, line, column - 2);
                }
                // following "${" || "#{" to first unquoted "}"
                i++;
                boolean singleQ = false;
                boolean doubleQ = false;
                lastCh = 0;
                for (; ; i++) {
                    if (i >= charBuffer.length()) {
                        throw new SAXParseException(Localizer.getMessage("jsp.error.unterminated", (char) elType + "{"), locator);
                    }
                    ch = charBuffer.charAt(i);
                    if (ch == '\n') {
                        column = 1;
                        line++;
                    } else {
                        column++;
                    }
                    if (lastCh == '\\' && (singleQ || doubleQ)) {
                        ttext.write(ch);
                        lastCh = 0;
                        continue;
                    }
                    if (ch == '}') {
                        new Node.ELExpression((char) elType, ttext.toString(), startMark, current);
                        ttext = new CharArrayWriter();
                        startMark = new Mark(ctxt, path, line, column);
                        break;
                    }
                    if (ch == '"')
                        doubleQ = !doubleQ;
                    else if (ch == '\'')
                        singleQ = !singleQ;
                    ttext.write(ch);
                    lastCh = ch;
                }
            } else if (lastCh == '\\' && (ch == '$' || ch == '#')) {
                ttext.write(ch);
                // Not start of EL anymore
                ch = 0;
            } else {
                if (lastCh == '$' || lastCh == '#' || lastCh == '\\') {
                    ttext.write(lastCh);
                }
                if (ch != '$' && ch != '#' && ch != '\\') {
                    ttext.write(ch);
                }
            }
            lastCh = ch;
        }
        if (lastCh == '$' || lastCh == '#' || lastCh == '\\') {
            ttext.write(lastCh);
        }
        if (ttext.size() > 0) {
            new Node.TemplateText(ttext.toString(), startMark, current);
        }
    }
    startMark = new Mark(ctxt, path, locator.getLineNumber(), locator.getColumnNumber());
    charBuffer = null;
}
Also used : SAXParseException(org.xml.sax.SAXParseException) CharArrayWriter(java.io.CharArrayWriter)

Example 33 with SAXParseException

use of org.xml.sax.SAXParseException in project sling by apache.

the class MyErrorHandler method parseXMLDocument.

// --------------------------------------------------------- Public Methods
/**
     * Parse the specified XML document, and return a <code>TreeNode</code>
     * that corresponds to the root node of the document tree.
     *
     * @param uri URI of the XML document being parsed
     * @param is Input source containing the deployment descriptor
     *
     * @exception JasperException if an input/output error occurs
     * @exception JasperException if a parsing error occurs
     */
public TreeNode parseXMLDocument(String uri, InputSource is) throws JasperException {
    Document document = null;
    // Perform an XML parse of this document, via JAXP
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(validating);
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(entityResolver);
        builder.setErrorHandler(errorHandler);
        document = builder.parse(is);
    } catch (ParserConfigurationException ex) {
        throw new JasperException(Localizer.getMessage("jsp.error.parse.xml", uri), ex);
    } catch (SAXParseException ex) {
        throw new JasperException(Localizer.getMessage("jsp.error.parse.xml.line", uri, Integer.toString(ex.getLineNumber()), Integer.toString(ex.getColumnNumber())), ex);
    } catch (SAXException sx) {
        throw new JasperException(Localizer.getMessage("jsp.error.parse.xml", uri), sx);
    } catch (IOException io) {
        throw new JasperException(Localizer.getMessage("jsp.error.parse.xml", uri), io);
    }
    // Convert the resulting document to a graph of TreeNodes
    return (convert(null, document.getDocumentElement()));
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 34 with SAXParseException

use of org.xml.sax.SAXParseException in project robovm by robovm.

the class Properties method loadFromXML.

/**
     * Loads the properties from an {@code InputStream} containing the
     * properties in XML form. The XML document must begin with (and conform to)
     * following DOCTYPE:
     *
     * <pre>
     * &lt;!DOCTYPE properties SYSTEM &quot;http://java.sun.com/dtd/properties.dtd&quot;&gt;
     * </pre>
     *
     * Also the content of the XML data must satisfy the DTD but the xml is not
     * validated against it. The DTD is not loaded from the SYSTEM ID. After
     * this method returns the InputStream is not closed.
     *
     * @param in the InputStream containing the XML document.
     * @throws IOException in case an error occurs during a read operation.
     * @throws InvalidPropertiesFormatException if the XML data is not a valid
     *             properties file.
     */
public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException {
    if (in == null) {
        throw new NullPointerException("in == null");
    }
    if (builder == null) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new Error(e);
        }
        builder.setErrorHandler(new ErrorHandler() {

            public void warning(SAXParseException e) throws SAXException {
                throw e;
            }

            public void error(SAXParseException e) throws SAXException {
                throw e;
            }

            public void fatalError(SAXParseException e) throws SAXException {
                throw e;
            }
        });
        builder.setEntityResolver(new EntityResolver() {

            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                if (systemId.equals(PROP_DTD_NAME)) {
                    InputSource result = new InputSource(new StringReader(PROP_DTD));
                    result.setSystemId(PROP_DTD_NAME);
                    return result;
                }
                throw new SAXException("Invalid DOCTYPE declaration: " + systemId);
            }
        });
    }
    try {
        Document doc = builder.parse(in);
        NodeList entries = doc.getElementsByTagName("entry");
        if (entries == null) {
            return;
        }
        int entriesListLength = entries.getLength();
        for (int i = 0; i < entriesListLength; i++) {
            Element entry = (Element) entries.item(i);
            String key = entry.getAttribute("key");
            String value = entry.getTextContent();
            /*
                 * key != null & value != null but key or(and) value can be
                 * empty String
                 */
            put(key, value);
        }
    } catch (IOException e) {
        throw e;
    } catch (SAXException e) {
        throw new InvalidPropertiesFormatException(e);
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 35 with SAXParseException

use of org.xml.sax.SAXParseException in project robovm by robovm.

the class XMLFilterImplTest method testError.

public void testError() {
    SAXParseException exception = new SAXParseException("Oops!", null);
    try {
        parent.error(exception);
    } catch (SAXException e) {
        throw new RuntimeException("Unexpected exception", e);
    }
    assertEquals(logger.size(), 1);
    assertEquals("error", logger.getMethod());
    assertEquals(new Object[] { exception }, logger.getArgs());
}
Also used : SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

Aggregations

SAXParseException (org.xml.sax.SAXParseException)365 SAXException (org.xml.sax.SAXException)170 IOException (java.io.IOException)131 DocumentBuilder (javax.xml.parsers.DocumentBuilder)73 InputSource (org.xml.sax.InputSource)69 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)68 Document (org.w3c.dom.Document)64 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)56 ErrorHandler (org.xml.sax.ErrorHandler)52 InputStream (java.io.InputStream)36 File (java.io.File)34 ArrayList (java.util.ArrayList)33 FileInputStream (java.io.FileInputStream)31 FileNotFoundException (java.io.FileNotFoundException)31 Element (org.w3c.dom.Element)30 StringReader (java.io.StringReader)21 NodeList (org.w3c.dom.NodeList)21 URL (java.net.URL)20 Test (org.junit.jupiter.api.Test)19 Node (org.w3c.dom.Node)19