Search in sources :

Example 66 with SAXParseException

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

the class JspDocumentParser method parseStandardAction.

//*********************************************************************
// Private utility methods
private Node parseStandardAction(String qName, String localName, Attributes nonTaglibAttrs, Attributes nonTaglibXmlnsAttrs, Attributes taglibAttrs, Mark start, Node parent) throws SAXException {
    Node node = null;
    if (localName.equals(ROOT_ACTION)) {
        if (!(current instanceof Node.Root)) {
            throw new SAXParseException(Localizer.getMessage("jsp.error.nested_jsproot"), locator);
        }
        node = new Node.JspRoot(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
        if (isTop) {
            pageInfo.setHasJspRoot(true);
        }
    } else if (localName.equals(PAGE_DIRECTIVE_ACTION)) {
        if (isTagFile) {
            throw new SAXParseException(Localizer.getMessage("jsp.error.action.istagfile", localName), locator);
        }
        node = new Node.PageDirective(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
        String imports = nonTaglibAttrs.getValue("import");
        // There can only be one 'import' attribute per page directive
        if (imports != null) {
            ((Node.PageDirective) node).addImport(imports);
        }
    } else if (localName.equals(INCLUDE_DIRECTIVE_ACTION)) {
        node = new Node.IncludeDirective(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
        processIncludeDirective(nonTaglibAttrs.getValue("file"), node);
    } else if (localName.equals(DECLARATION_ACTION)) {
        if (scriptlessBodyNode != null) {
            // declared to be scriptless
            throw new SAXParseException(Localizer.getMessage("jsp.error.no.scriptlets", localName), locator);
        }
        node = new Node.Declaration(qName, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(SCRIPTLET_ACTION)) {
        if (scriptlessBodyNode != null) {
            // declared to be scriptless
            throw new SAXParseException(Localizer.getMessage("jsp.error.no.scriptlets", localName), locator);
        }
        node = new Node.Scriptlet(qName, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(EXPRESSION_ACTION)) {
        if (scriptlessBodyNode != null) {
            // declared to be scriptless
            throw new SAXParseException(Localizer.getMessage("jsp.error.no.scriptlets", localName), locator);
        }
        node = new Node.Expression(qName, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(USE_BEAN_ACTION)) {
        node = new Node.UseBean(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(SET_PROPERTY_ACTION)) {
        node = new Node.SetProperty(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(GET_PROPERTY_ACTION)) {
        node = new Node.GetProperty(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(INCLUDE_ACTION)) {
        node = new Node.IncludeAction(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(FORWARD_ACTION)) {
        node = new Node.ForwardAction(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(PARAM_ACTION)) {
        node = new Node.ParamAction(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(PARAMS_ACTION)) {
        node = new Node.ParamsAction(qName, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(PLUGIN_ACTION)) {
        node = new Node.PlugIn(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(TEXT_ACTION)) {
        node = new Node.JspText(qName, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(BODY_ACTION)) {
        node = new Node.JspBody(qName, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(ATTRIBUTE_ACTION)) {
        node = new Node.NamedAttribute(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(OUTPUT_ACTION)) {
        node = new Node.JspOutput(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(TAG_DIRECTIVE_ACTION)) {
        if (!isTagFile) {
            throw new SAXParseException(Localizer.getMessage("jsp.error.action.isnottagfile", localName), locator);
        }
        node = new Node.TagDirective(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
        String imports = nonTaglibAttrs.getValue("import");
        // There can only be one 'import' attribute per tag directive
        if (imports != null) {
            ((Node.TagDirective) node).addImport(imports);
        }
    } else if (localName.equals(ATTRIBUTE_DIRECTIVE_ACTION)) {
        if (!isTagFile) {
            throw new SAXParseException(Localizer.getMessage("jsp.error.action.isnottagfile", localName), locator);
        }
        node = new Node.AttributeDirective(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(VARIABLE_DIRECTIVE_ACTION)) {
        if (!isTagFile) {
            throw new SAXParseException(Localizer.getMessage("jsp.error.action.isnottagfile", localName), locator);
        }
        node = new Node.VariableDirective(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(INVOKE_ACTION)) {
        if (!isTagFile) {
            throw new SAXParseException(Localizer.getMessage("jsp.error.action.isnottagfile", localName), locator);
        }
        node = new Node.InvokeAction(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(DOBODY_ACTION)) {
        if (!isTagFile) {
            throw new SAXParseException(Localizer.getMessage("jsp.error.action.isnottagfile", localName), locator);
        }
        node = new Node.DoBodyAction(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(ELEMENT_ACTION)) {
        node = new Node.JspElement(qName, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else if (localName.equals(FALLBACK_ACTION)) {
        node = new Node.FallBackAction(qName, nonTaglibXmlnsAttrs, taglibAttrs, start, current);
    } else {
        throw new SAXParseException(Localizer.getMessage("jsp.error.xml.badStandardAction", localName), locator);
    }
    return node;
}
Also used : SAXParseException(org.xml.sax.SAXParseException)

Example 67 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 68 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 69 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 70 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)

Aggregations

SAXParseException (org.xml.sax.SAXParseException)206 SAXException (org.xml.sax.SAXException)109 IOException (java.io.IOException)75 DocumentBuilder (javax.xml.parsers.DocumentBuilder)53 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)47 Document (org.w3c.dom.Document)47 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)42 InputSource (org.xml.sax.InputSource)39 ErrorHandler (org.xml.sax.ErrorHandler)34 Test (org.junit.Test)28 Element (org.w3c.dom.Element)24 InputStream (java.io.InputStream)20 ArrayList (java.util.ArrayList)18 NodeList (org.w3c.dom.NodeList)18 FileInputStream (java.io.FileInputStream)17 FileNotFoundException (java.io.FileNotFoundException)17 File (java.io.File)16 Node (org.w3c.dom.Node)13 StringReader (java.io.StringReader)12 URL (java.net.URL)11