Search in sources :

Example 16 with SAXSourceLocator

use of org.apache.xml.utils.SAXSourceLocator in project robovm by robovm.

the class StylesheetHandler method fatalError.

/**
   * Report a fatal XSLT processing error.
   *
   * @param e The error information encoded as an exception.
   *
   * @throws org.xml.sax.SAXException that wraps a
   * {@link javax.xml.transform.TransformerException} if the current
   * {@link javax.xml.transform.ErrorListener#fatalError}
   * method chooses to flag this condition as an error.
   */
public void fatalError(org.xml.sax.SAXParseException e) throws org.xml.sax.SAXException {
    String formattedMsg = e.getMessage();
    SAXSourceLocator locator = getLocator();
    ErrorListener handler = m_stylesheetProcessor.getErrorListener();
    try {
        handler.fatalError(new TransformerException(formattedMsg, locator));
    } catch (TransformerException te) {
        throw new org.xml.sax.SAXException(te);
    }
}
Also used : ErrorListener(javax.xml.transform.ErrorListener) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) TransformerException(javax.xml.transform.TransformerException)

Example 17 with SAXSourceLocator

use of org.apache.xml.utils.SAXSourceLocator in project robovm by robovm.

the class StylesheetHandler method pushSpaceHandling.

/**
   * Push boolean value on to the spacePreserve stack depending on the value 
   * of xml:space=default/preserve.
   * 
   * @param attrs list of attributes that were passed to startElement.
   */
void pushSpaceHandling(Attributes attrs) throws org.xml.sax.SAXParseException {
    String value = attrs.getValue("xml:space");
    if (null == value) {
        m_spacePreserveStack.push(m_spacePreserveStack.peekOrFalse());
    } else if (value.equals("preserve")) {
        m_spacePreserveStack.push(true);
    } else if (value.equals("default")) {
        m_spacePreserveStack.push(false);
    } else {
        SAXSourceLocator locator = getLocator();
        ErrorListener handler = m_stylesheetProcessor.getErrorListener();
        try {
            //"Illegal value for xml:space", locator));
            handler.error(new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_ILLEGAL_XMLSPACE_VALUE, null), locator));
        } catch (TransformerException te) {
            throw new org.xml.sax.SAXParseException(te.getMessage(), locator, te);
        }
        m_spacePreserveStack.push(m_spacePreserveStack.peek());
    }
}
Also used : ErrorListener(javax.xml.transform.ErrorListener) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) TransformerException(javax.xml.transform.TransformerException)

Example 18 with SAXSourceLocator

use of org.apache.xml.utils.SAXSourceLocator in project robovm by robovm.

the class ProcessorLRE method startElement.

/**
   * Receive notification of the start of an element.
   *
   * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
   * @param uri The Namespace URI, or an empty string.
   * @param localName The local name (without prefix), or empty string if not namespace processing.
   * @param rawName The qualified name (with prefix).
   * @param attributes The specified or defaulted attributes.
   */
public void startElement(StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes) throws org.xml.sax.SAXException {
    try {
        ElemTemplateElement p = handler.getElemTemplateElement();
        boolean excludeXSLDecl = false;
        boolean isLREAsStyleSheet = false;
        if (null == p) {
            // Literal Result Template as stylesheet.
            XSLTElementProcessor lreProcessor = handler.popProcessor();
            XSLTElementProcessor stylesheetProcessor = handler.getProcessorFor(Constants.S_XSLNAMESPACEURL, "stylesheet", "xsl:stylesheet");
            handler.pushProcessor(lreProcessor);
            Stylesheet stylesheet;
            try {
                stylesheet = getStylesheetRoot(handler);
            } catch (TransformerConfigurationException tfe) {
                throw new TransformerException(tfe);
            }
            // stylesheet.setDOMBackPointer(handler.getOriginatingNode());
            // ***** Note that we're assigning an empty locator. Is this necessary?
            SAXSourceLocator slocator = new SAXSourceLocator();
            Locator locator = handler.getLocator();
            if (null != locator) {
                slocator.setLineNumber(locator.getLineNumber());
                slocator.setColumnNumber(locator.getColumnNumber());
                slocator.setPublicId(locator.getPublicId());
                slocator.setSystemId(locator.getSystemId());
            }
            stylesheet.setLocaterInfo(slocator);
            stylesheet.setPrefixes(handler.getNamespaceSupport());
            handler.pushStylesheet(stylesheet);
            isLREAsStyleSheet = true;
            AttributesImpl stylesheetAttrs = new AttributesImpl();
            AttributesImpl lreAttrs = new AttributesImpl();
            int n = attributes.getLength();
            for (int i = 0; i < n; i++) {
                String attrLocalName = attributes.getLocalName(i);
                String attrUri = attributes.getURI(i);
                String value = attributes.getValue(i);
                if ((null != attrUri) && attrUri.equals(Constants.S_XSLNAMESPACEURL)) {
                    stylesheetAttrs.addAttribute(null, attrLocalName, attrLocalName, attributes.getType(i), attributes.getValue(i));
                } else if ((attrLocalName.startsWith("xmlns:") || attrLocalName.equals("xmlns")) && value.equals(Constants.S_XSLNAMESPACEURL)) {
                // ignore
                } else {
                    lreAttrs.addAttribute(attrUri, attrLocalName, attributes.getQName(i), attributes.getType(i), attributes.getValue(i));
                }
            }
            attributes = lreAttrs;
            // allowed on a stylesheet.
            try {
                stylesheetProcessor.setPropertiesFromAttributes(handler, "stylesheet", stylesheetAttrs, stylesheet);
            } catch (Exception e) {
                if (stylesheet.getDeclaredPrefixes() == null || !declaredXSLNS(stylesheet)) {
                    throw new org.xml.sax.SAXException(XSLMessages.createWarning(XSLTErrorResources.WG_OLD_XSLT_NS, null));
                } else {
                    throw new org.xml.sax.SAXException(e);
                }
            }
            handler.pushElemTemplateElement(stylesheet);
            ElemTemplate template = new ElemTemplate();
            if (slocator != null)
                template.setLocaterInfo(slocator);
            appendAndPush(handler, template);
            XPath rootMatch = new XPath("/", stylesheet, stylesheet, XPath.MATCH, handler.getStylesheetProcessor().getErrorListener());
            template.setMatch(rootMatch);
            // template.setDOMBackPointer(handler.getOriginatingNode());
            stylesheet.setTemplate(template);
            p = handler.getElemTemplateElement();
            excludeXSLDecl = true;
        }
        XSLTElementDef def = getElemDef();
        Class classObject = def.getClassObject();
        boolean isExtension = false;
        boolean isComponentDecl = false;
        boolean isUnknownTopLevel = false;
        while (null != p) {
            // System.out.println("Checking: "+p);
            if (p instanceof ElemLiteralResult) {
                ElemLiteralResult parentElem = (ElemLiteralResult) p;
                isExtension = parentElem.containsExtensionElementURI(uri);
            } else if (p instanceof Stylesheet) {
                Stylesheet parentElem = (Stylesheet) p;
                isExtension = parentElem.containsExtensionElementURI(uri);
                if ((false == isExtension) && (null != uri) && (uri.equals(Constants.S_BUILTIN_EXTENSIONS_URL) || uri.equals(Constants.S_BUILTIN_OLD_EXTENSIONS_URL))) {
                    isComponentDecl = true;
                } else {
                    isUnknownTopLevel = true;
                }
            }
            if (isExtension)
                break;
            p = p.getParentElem();
        }
        ElemTemplateElement elem = null;
        try {
            if (isExtension) {
                // System.out.println("Creating extension(1): "+uri);
                elem = new ElemExtensionCall();
            } else if (isComponentDecl) {
                elem = (ElemTemplateElement) classObject.newInstance();
            } else if (isUnknownTopLevel) {
                // TBD: Investigate, not sure about this.  -sb
                elem = (ElemTemplateElement) classObject.newInstance();
            } else {
                elem = (ElemTemplateElement) classObject.newInstance();
            }
            elem.setDOMBackPointer(handler.getOriginatingNode());
            elem.setLocaterInfo(handler.getLocator());
            elem.setPrefixes(handler.getNamespaceSupport(), excludeXSLDecl);
            if (elem instanceof ElemLiteralResult) {
                ((ElemLiteralResult) elem).setNamespace(uri);
                ((ElemLiteralResult) elem).setLocalName(localName);
                ((ElemLiteralResult) elem).setRawName(rawName);
                ((ElemLiteralResult) elem).setIsLiteralResultAsStylesheet(isLREAsStyleSheet);
            }
        } catch (InstantiationException ie) {
            //"Failed creating ElemLiteralResult instance!", ie);
            handler.error(XSLTErrorResources.ER_FAILED_CREATING_ELEMLITRSLT, null, ie);
        } catch (IllegalAccessException iae) {
            //"Failed creating ElemLiteralResult instance!", iae);
            handler.error(XSLTErrorResources.ER_FAILED_CREATING_ELEMLITRSLT, null, iae);
        }
        setPropertiesFromAttributes(handler, rawName, attributes, elem);
        // bit of a hack here...
        if (!isExtension && (elem instanceof ElemLiteralResult)) {
            isExtension = ((ElemLiteralResult) elem).containsExtensionElementURI(uri);
            if (isExtension) {
                // System.out.println("Creating extension(2): "+uri);
                elem = new ElemExtensionCall();
                elem.setLocaterInfo(handler.getLocator());
                elem.setPrefixes(handler.getNamespaceSupport());
                ((ElemLiteralResult) elem).setNamespace(uri);
                ((ElemLiteralResult) elem).setLocalName(localName);
                ((ElemLiteralResult) elem).setRawName(rawName);
                setPropertiesFromAttributes(handler, rawName, attributes, elem);
            }
        }
        appendAndPush(handler, elem);
    } catch (TransformerException te) {
        throw new org.xml.sax.SAXException(te);
    }
}
Also used : XPath(org.apache.xpath.XPath) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ElemTemplate(org.apache.xalan.templates.ElemTemplate) ElemTemplateElement(org.apache.xalan.templates.ElemTemplateElement) Stylesheet(org.apache.xalan.templates.Stylesheet) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Locator(org.xml.sax.Locator) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) AttributesImpl(org.xml.sax.helpers.AttributesImpl) ElemLiteralResult(org.apache.xalan.templates.ElemLiteralResult) ElemExtensionCall(org.apache.xalan.templates.ElemExtensionCall) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) TransformerException(javax.xml.transform.TransformerException)

Example 19 with SAXSourceLocator

use of org.apache.xml.utils.SAXSourceLocator in project robovm by robovm.

the class StylesheetHandler method warn.

/**
   * Warn the user of an problem.
   *
   * @param msg An key into the {@link org.apache.xalan.res.XSLTErrorResources}
   * table, that is one of the WG_ prefixed definitions.
   * @param args An array of arguments for the given warning.
   *
   * @throws org.xml.sax.SAXException that wraps a
   * {@link javax.xml.transform.TransformerException} if the current
   * {@link javax.xml.transform.ErrorListener#warning}
   * method chooses to flag this condition as an error.
   * @xsl.usage internal
   */
public void warn(String msg, Object[] args) throws org.xml.sax.SAXException {
    String formattedMsg = XSLMessages.createWarning(msg, args);
    SAXSourceLocator locator = getLocator();
    ErrorListener handler = m_stylesheetProcessor.getErrorListener();
    try {
        if (null != handler)
            handler.warning(new TransformerException(formattedMsg, locator));
    } catch (TransformerException te) {
        throw new org.xml.sax.SAXException(te);
    }
}
Also used : ErrorListener(javax.xml.transform.ErrorListener) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) TransformerException(javax.xml.transform.TransformerException)

Example 20 with SAXSourceLocator

use of org.apache.xml.utils.SAXSourceLocator in project robovm by robovm.

the class StylesheetHandler method error.

/**
   * Receive notification of a recoverable XSLT processing error.
   *
   * @param e The error information encoded as an exception.
   *
   * @throws org.xml.sax.SAXException that wraps a
   * {@link javax.xml.transform.TransformerException} if the current
   * {@link javax.xml.transform.ErrorListener#error}
   * method chooses to flag this condition as an error.
   */
public void error(org.xml.sax.SAXParseException e) throws org.xml.sax.SAXException {
    String formattedMsg = e.getMessage();
    SAXSourceLocator locator = getLocator();
    ErrorListener handler = m_stylesheetProcessor.getErrorListener();
    try {
        handler.error(new TransformerException(formattedMsg, locator));
    } catch (TransformerException te) {
        throw new org.xml.sax.SAXException(te);
    }
}
Also used : ErrorListener(javax.xml.transform.ErrorListener) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) TransformerException(javax.xml.transform.TransformerException)

Aggregations

SAXSourceLocator (org.apache.xml.utils.SAXSourceLocator)24 TransformerException (javax.xml.transform.TransformerException)22 ErrorListener (javax.xml.transform.ErrorListener)18 ElemTemplateElement (org.apache.xalan.templates.ElemTemplateElement)4 Stylesheet (org.apache.xalan.templates.Stylesheet)4 IOException (java.io.IOException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SourceLocator (javax.xml.transform.SourceLocator)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 ElemExtensionCall (org.apache.xalan.templates.ElemExtensionCall)2 ElemLiteralResult (org.apache.xalan.templates.ElemLiteralResult)2 ElemTemplate (org.apache.xalan.templates.ElemTemplate)2 StylesheetComposed (org.apache.xalan.templates.StylesheetComposed)2 StylesheetRoot (org.apache.xalan.templates.StylesheetRoot)2 DTM (org.apache.xml.dtm.DTM)2 DTMIterator (org.apache.xml.dtm.DTMIterator)2 XPath (org.apache.xpath.XPath)2 XPathContext (org.apache.xpath.XPathContext)2 XString (org.apache.xpath.objects.XString)2 Locator (org.xml.sax.Locator)2