Example usage for org.xml.sax ContentHandler endPrefixMapping

List of usage examples for org.xml.sax ContentHandler endPrefixMapping

Introduction

In this page you can find the example usage for org.xml.sax ContentHandler endPrefixMapping.

Prototype

public void endPrefixMapping(String prefix) throws SAXException;

Source Link

Document

End the scope of a prefix-URI mapping.

Usage

From source file:org.apache.cocoon.components.notification.Notifier.java

/**
 * Generate notification information in XML format.
 *///w  w  w.  j a va  2  s . c om
public static void notify(Notifying n, ContentHandler ch, String mimetype) throws SAXException {
    final String PREFIX = Constants.ERROR_NAMESPACE_PREFIX;
    final String URI = Constants.ERROR_NAMESPACE_URI;

    // Start the document
    ch.startDocument();
    ch.startPrefixMapping(PREFIX, URI);

    // Root element.
    AttributesImpl atts = new AttributesImpl();

    atts.addAttribute(URI, "type", PREFIX + ":type", "CDATA", n.getType());
    atts.addAttribute(URI, "sender", PREFIX + ":sender", "CDATA", n.getSender());
    ch.startElement(URI, "notify", PREFIX + ":notify", atts);
    ch.startElement(URI, "title", PREFIX + ":title", new AttributesImpl());
    ch.characters(n.getTitle().toCharArray(), 0, n.getTitle().length());
    ch.endElement(URI, "title", PREFIX + ":title");
    ch.startElement(URI, "source", PREFIX + ":source", new AttributesImpl());
    ch.characters(n.getSource().toCharArray(), 0, n.getSource().length());
    ch.endElement(URI, "source", PREFIX + ":source");
    ch.startElement(URI, "message", PREFIX + ":message", new AttributesImpl());

    if (n.getMessage() != null) {
        ch.characters(n.getMessage().toCharArray(), 0, n.getMessage().length());
    }

    ch.endElement(URI, "message", PREFIX + ":message");
    ch.startElement(URI, "description", PREFIX + ":description", XMLUtils.EMPTY_ATTRIBUTES);
    ch.characters(n.getDescription().toCharArray(), 0, n.getDescription().length());
    ch.endElement(URI, "description", PREFIX + ":description");

    Map extraDescriptions = n.getExtraDescriptions();
    for (Iterator i = extraDescriptions.entrySet().iterator(); i.hasNext();) {
        final Map.Entry me = (Map.Entry) i.next();
        String key = (String) me.getKey();
        String value = String.valueOf(me.getValue());
        atts = new AttributesImpl();
        atts.addAttribute(URI, "description", PREFIX + ":description", "CDATA", key);
        ch.startElement(URI, "extra", PREFIX + ":extra", atts);
        ch.characters(value.toCharArray(), 0, value.length());
        ch.endElement(URI, "extra", PREFIX + ":extra");
    }

    // End root element.
    ch.endElement(URI, "notify", PREFIX + ":notify");

    // End the document.
    ch.endPrefixMapping(PREFIX);
    ch.endDocument();
}

From source file:org.apache.cocoon.components.source.impl.QDoxSource.java

/**
 * @see XMLizable#toSAX(org.xml.sax.ContentHandler)
 * @throws SAXException if any error occurs during SAX outputting.
 *//*from  ww  w  .j a va  2s.  c o m*/
public void toSAX(ContentHandler handler) throws SAXException {
    if (javadocClass == null) {
        logger.error("No classfile loaded! Cannot output SAX events.");
        return;
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Outputting SAX events for class " + javadocClass.getFullyQualifiedName());
        logger.debug("  #fields: " + javadocClass.getFields().length);
        logger.debug("  #methods and constructors: " + javadocClass.getMethods().length);
    }

    // Output SAX 'header':
    handler.startDocument();
    handler.startPrefixMapping(NS_PREFIX, NS_URI);

    // Output class-level element:
    outputClassStartElement(handler, javadocClass);

    // Modifiers:
    outputModifiers(handler, javadocClass);

    // Imports:
    JavaSource parent = javadocClass.getParentSource();
    // Add two implicit imports:
    parent.addImport("java.lang.*");
    if (parent.getPackage() != null) {
        parent.addImport(parent.getPackage() + ".*");
    } else {
        parent.addImport("*");
    }
    String[] imports = parent.getImports();

    saxStartElement(handler, IMPORTS_ELEMENT);
    for (int i = 0; i < imports.length; i++) {
        if (imports[i].endsWith("*")) {
            // package import:
            saxStartElement(handler, IMPORT_ELEMENT, new String[][] { { IMPORT_ATTRIBUTE, "package" } });
            String imp = StringUtils.substringBeforeLast(imports[i], ".*");
            saxCharacters(handler, imp);
        } else {
            saxStartElement(handler, IMPORT_ELEMENT, new String[][] { { IMPORT_ATTRIBUTE, "class" } });
            saxCharacters(handler, imports[i]);
        }
        saxEndElement(handler, IMPORT_ELEMENT);
    }
    saxEndElement(handler, IMPORTS_ELEMENT);

    // Superclass:
    if (!javadocClass.isInterface()) {
        outputSuperClassInheritance(handler, javadocClass, CLASS_INHERITANCE);
    }

    // Implements:
    outputImplements(handler, javadocClass, true);

    // Containing class in case this is an inner class:
    if (containingJavadocClass != null) {
        saxStartElement(handler, NESTED_IN_ELEMENT);
        outputClassStartElement(handler, containingJavadocClass);
        outputModifiers(handler, containingJavadocClass);
        outputComment(handler, containingJavadocClass.getComment());
        outputTags(handler, containingJavadocClass);
        outputClassEndElement(handler, containingJavadocClass);
        saxEndElement(handler, NESTED_IN_ELEMENT);
    }

    // Comment:
    outputComment(handler, javadocClass.getComment());

    // Tags:
    outputTags(handler, javadocClass);

    // Inner classes:
    outputInnerClasses(handler, javadocClass, true);

    // Fields:
    outputFields(handler, javadocClass, true);

    // Constructors:
    outputMethods(handler, javadocClass, CONSTRUCTOR_MODE);

    // Methods:
    outputMethods(handler, javadocClass, METHOD_MODE);

    // Close class-level element:
    outputClassEndElement(handler, javadocClass);

    // Output SAX 'footer':
    handler.endPrefixMapping(NS_PREFIX);
    handler.endDocument();
}

From source file:org.apache.cocoon.forms.util.I18nMessage.java

public void toSAX(ContentHandler contentHandler) throws SAXException {
    contentHandler.startPrefixMapping("i18n", I18nTransformer.I18N_NAMESPACE_URI);
    if (parameters != null) {
        contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TRANSLATE_ELEMENT,
                "i18n:" + I18nTransformer.I18N_TRANSLATE_ELEMENT, XMLUtils.EMPTY_ATTRIBUTES);
    }/*from w  w w .j a va2  s  .  c  o  m*/

    AttributesImpl i18nAttrs = new AttributesImpl();
    if (catalogue != null) {
        i18nAttrs.addCDATAAttribute(I18nTransformer.I18N_NAMESPACE_URI,
                I18nTransformer.I18N_CATALOGUE_ATTRIBUTE, "i18n:" + I18nTransformer.I18N_CATALOGUE_ATTRIBUTE,
                catalogue);
    }

    contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT,
            "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT, i18nAttrs);
    contentHandler.characters(key.toCharArray(), 0, key.length());
    contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT,
            "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT);

    // the parameters
    if (parameters != null) {
        for (int i = 0; i < parameters.length; i++) {
            contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_PARAM_ELEMENT,
                    "i18n:" + I18nTransformer.I18N_PARAM_ELEMENT, XMLUtils.EMPTY_ATTRIBUTES);
            if (keys != null && keys[i]) {
                contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI,
                        I18nTransformer.I18N_TEXT_ELEMENT, "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT,
                        i18nAttrs);
            }
            final String aParam = String.valueOf(parameters[i]);
            contentHandler.characters(aParam.toCharArray(), 0, aParam.length());
            if (keys != null && keys[i]) {
                contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT,
                        "i18n:" + I18nTransformer.I18N_TEXT_ELEMENT);
            }
            contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_PARAM_ELEMENT,
                    "i18n:" + I18nTransformer.I18N_PARAM_ELEMENT);
        }
        contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TRANSLATE_ELEMENT,
                "i18n:" + I18nTransformer.I18N_TRANSLATE_ELEMENT);
    }
    contentHandler.endPrefixMapping("i18n");
}

From source file:org.apache.cocoon.generation.ExceptionGenerator.java

public static void toSAX(Throwable thr, ContentHandler handler) throws SAXException {
    Throwable root = ExceptionUtils.getRootCause(thr);
    if (root == null)
        root = thr;//from  w ww . j av a2s  .co m

    AttributesImpl attr = new AttributesImpl();
    handler.startPrefixMapping("ex", EXCEPTION_NS);
    attr.addCDATAAttribute("class", root.getClass().getName());
    handler.startElement(EXCEPTION_NS, "exception-report", "ex:exception-report", attr);

    // Root exception location
    Location loc = LocationUtils.getLocation(root);
    if (LocationUtils.isKnown(loc)) {
        attr.clear();
        dumpLocation(loc, attr, handler);
    }

    // Root exception message
    attr.clear();
    String message = root instanceof LocatableException ? ((LocatableException) root).getRawMessage()
            : root.getMessage();
    simpleElement("message", attr, message, handler);

    // Cocoon stacktrace: dump all located exceptions in the exception stack
    handler.startElement(EXCEPTION_NS, "cocoon-stacktrace", "ex:cocoon-stacktrace", attr);
    Throwable current = thr;
    while (current != null) {
        loc = LocationUtils.getLocation(current);
        if (LocationUtils.isKnown(loc)) {
            // One or more locations: dump it
            handler.startElement(EXCEPTION_NS, "exception", "ex:exception", attr);

            message = current instanceof LocatableException ? ((LocatableException) current).getRawMessage()
                    : current.getMessage();
            simpleElement("message", attr, message, handler);

            attr.clear();
            handler.startElement(EXCEPTION_NS, "locations", "ex:locations", attr);
            dumpLocation(loc, attr, handler);

            if (current instanceof MultiLocatable) {
                List locations = ((MultiLocatable) current).getLocations();
                for (int i = 1; i < locations.size(); i++) { // start at 1 because we already dumped the first one
                    attr.clear();
                    dumpLocation((Location) locations.get(i), attr, handler);
                }
            }
            handler.endElement(EXCEPTION_NS, "locations", "ex:locations");
            handler.endElement(EXCEPTION_NS, "exception", "ex:exception");
        }

        // Dump parent location
        current = ExceptionUtils.getCause(current);
    }

    handler.endElement(EXCEPTION_NS, "cocoon-stacktrace", "ex:cocoon-stacktrace");

    // Root exception stacktrace
    attr.clear();
    simpleElement("stacktrace", attr, ExceptionUtils.getStackTrace(root), handler);

    // Full stack trace (if exception is chained)
    if (thr != root) {
        String trace = SystemUtils.isJavaVersionAtLeast(140) ? ExceptionUtils.getStackTrace(thr)
                : ExceptionUtils.getFullStackTrace(thr);

        simpleElement("full-stacktrace", attr, trace, handler);
    }

    handler.endElement(EXCEPTION_NS, "exception-report", "ex:exception-report");
    handler.endPrefixMapping("ex");
}

From source file:org.apache.cocoon.portal.wsrp.adapter.WSRPAdapter.java

/**
  * Checks the values of the <tt>portlet-key</tt> and the <tt>user</tt> for current portlet-instance<br/>
  * After that all passed the <tt>getMarkup()</tt>-call will be initiated<br />
  * /*  ww w.ja v  a 2 s  . c  om*/
 * @see org.apache.cocoon.portal.coplet.adapter.impl.AbstractCopletAdapter#streamContent(org.apache.cocoon.portal.coplet.CopletInstanceData, org.xml.sax.ContentHandler)
 */
public void streamContent(CopletInstanceData coplet, ContentHandler contentHandler) throws SAXException {
    try {
        // set the coplet in the thread local variable to give other components access to
        // the instance
        this.setCurrentCopletInstanceData(coplet);

        // get the portlet key and the user
        final PortletKey portletKey = (PortletKey) coplet.getTemporaryAttribute(ATTRIBUTE_NAME_PORTLET_KEY);
        if (portletKey == null) {
            throw new SAXException("WSRP configuration is missing: portlet key.");
        }
        final User user = (User) coplet.getTemporaryAttribute(ATTRIBUTE_NAME_USER);
        if (user == null) {
            throw new SAXException("WSRP configuration is missing: user.");
        }

        final String portletInstanceKey = (String) coplet
                .getTemporaryAttribute(ATTRIBUTE_NAME_PORTLET_INSTANCE_KEY);

        // getMarkup()
        final WSRPPortlet wsrpportlet = consumerEnvironment.getPortletRegistry().getPortlet(portletKey);

        SimplePortletWindowSession windowSession = getSimplePortletWindowSession(wsrpportlet,
                portletInstanceKey, user);
        final MarkupContext markupContext = this.getMarkupContext(wsrpportlet, windowSession, user);
        if (markupContext == null || markupContext.getMarkupString() == null) {
            throw new SAXException("No markup received from wsrp coplet " + coplet.getId());
        }
        final String content = markupContext.getMarkupString();

        final Boolean usePipeline;
        final boolean usesGet;
        // If the portlet uses the method get we always have to rewrite form elements
        final Producer producer = this.consumerEnvironment.getProducerRegistry()
                .getProducer(portletKey.getProducerId());
        final PortletDescription desc = producer.getPortletDescription(portletKey.getPortletHandle());
        if (desc.getUsesMethodGet() != null && desc.getUsesMethodGet().booleanValue()) {
            usePipeline = Boolean.TRUE;
            usesGet = true;
        } else {
            usePipeline = (Boolean) this.getConfiguration(coplet, "use-pipeline", Boolean.FALSE);
            usesGet = false;
        }
        if (usePipeline.booleanValue()) {
            if (usesGet) {
                contentHandler = new FormRewritingHandler(contentHandler);
            }
            HtmlSaxParser.parseString(content, HtmlSaxParser.getContentFilter(contentHandler));
        } else {
            // stream out the include for the serializer
            IncludingHTMLSerializer.addPortlet(portletInstanceKey, content);
            contentHandler.startPrefixMapping("portal", IncludingHTMLSerializer.NAMESPACE);
            final AttributesImpl attr = new AttributesImpl();
            attr.addCDATAAttribute("portlet", portletInstanceKey);
            contentHandler.startElement(IncludingHTMLSerializer.NAMESPACE, "include", "portal:include", attr);
            contentHandler.endElement(IncludingHTMLSerializer.NAMESPACE, "include", "portal:include");
            contentHandler.endPrefixMapping("portal");
        }
    } catch (WSRPException e) {
        throw new SAXException("Exception during getMarkup of wsrp coplet: " + coplet.getId(), e);
    } catch (SAXException se) {
        throw se;
    } finally {
        this.setCurrentCopletInstanceData(null);
    }
}

From source file:org.exist.cocoon.XMLDBSource.java

private void collectionToSAX(ContentHandler handler) throws SAXException, XMLDBException {

    AttributesImpl attributes = new AttributesImpl();

    if (query != null) {
        // Query collection
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Querying collection " + url + "; query= " + this.query);
        }//from   w w w.  j  av a2  s .c o m

        queryToSAX(handler, collection, null);
    } else {
        // List collection
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Listing collection " + url);
        }

        final String nresources = Integer.toString(collection.getResourceCount());
        attributes.addAttribute("", RESOURCE_COUNT_ATTR, RESOURCE_COUNT_ATTR, "CDATA", nresources);
        final String ncollections = Integer.toString(collection.getChildCollectionCount());
        attributes.addAttribute("", COLLECTION_COUNT_ATTR, COLLECTION_COUNT_ATTR, "CDATA", ncollections);
        attributes.addAttribute("", COLLECTION_BASE_ATTR, COLLECTION_BASE_ATTR, "CDATA", url);

        handler.startDocument();
        handler.startPrefixMapping(PREFIX, URI);
        handler.startElement(URI, COLLECTIONS, QCOLLECTIONS, attributes);

        // Print child collections
        String[] collections = collection.listChildCollections();
        for (int i = 0; i < collections.length; i++) {
            attributes.clear();
            attributes.addAttribute("", NAME_ATTR, NAME_ATTR, CDATA, collections[i]);
            handler.startElement(URI, COLLECTION, QCOLLECTION, attributes);
            handler.endElement(URI, COLLECTION, QCOLLECTION);
        }

        // Print child resources
        String[] resources = collection.listResources();
        for (int i = 0; i < resources.length; i++) {
            attributes.clear();
            attributes.addAttribute("", NAME_ATTR, NAME_ATTR, CDATA, resources[i]);
            handler.startElement(URI, RESOURCE, QRESOURCE, attributes);
            handler.endElement(URI, RESOURCE, QRESOURCE);
        }

        handler.endElement(URI, COLLECTIONS, QCOLLECTIONS);
        handler.endPrefixMapping(PREFIX);
        handler.endDocument();
    }
}

From source file:org.exist.cocoon.XMLDBSource.java

private void queryToSAX(ContentHandler handler, Collection collection, String resource)
        throws SAXException, XMLDBException {

    AttributesImpl attributes = new AttributesImpl();

    XPathQueryService service = (XPathQueryService) collection.getService("XPathQueryService", "1.0");
    ResourceSet resultSet = (resource == null) ? service.query(query) : service.queryResource(resource, query);

    attributes.addAttribute("", QUERY_ATTR, QUERY_ATTR, "CDATA", query);
    attributes.addAttribute("", RESULTS_COUNT_ATTR, RESULTS_COUNT_ATTR, "CDATA",
            Long.toString(resultSet.getSize()));

    handler.startDocument();//ww  w. j  ava 2 s  .co  m
    handler.startPrefixMapping(PREFIX, URI);
    handler.startElement(URI, RESULTSET, QRESULTSET, attributes);

    IncludeXMLConsumer includeHandler = new IncludeXMLConsumer(handler);

    // Print search results
    ResourceIterator results = resultSet.getIterator();
    while (results.hasMoreResources()) {
        XMLResource result = (XMLResource) results.nextResource();

        final String id = result.getId();
        final String documentId = result.getDocumentId();

        attributes.clear();
        if (id != null) {
            attributes.addAttribute("", RESULT_ID_ATTR, RESULT_ID_ATTR, CDATA, id);
        }
        if (documentId != null) {
            attributes.addAttribute("", RESULT_DOCID_ATTR, RESULT_DOCID_ATTR, CDATA, documentId);
        }

        handler.startElement(URI, RESULT, QRESULT, attributes);
        try {
            result.getContentAsSAX(includeHandler);
        } catch (XMLDBException xde) {
            // That may be a text-only result
            Object content = result.getContent();
            if (content instanceof String) {
                String text = (String) content;
                handler.characters(text.toCharArray(), 0, text.length());
            } else {
                // Cannot do better
                throw xde;
            }
        }
        handler.endElement(URI, RESULT, QRESULT);
    }

    handler.endElement(URI, RESULTSET, QRESULTSET);
    handler.endPrefixMapping(PREFIX);
    handler.endDocument();
}

From source file:uk.ac.cam.caret.sakai.rwiki.component.service.impl.XSLTEntityHandler.java

public void parseToSAX(final String toRender, final ContentHandler ch) throws IOException, SAXException {
    /**/*from   w  w w . j a v a 2s .co m*/
     * create a proxy for the stream, filtering out the start element and
     * end element events
     */
    ContentHandler proxy = new ContentHandler() {
        public void setDocumentLocator(Locator arg0) {
            ch.setDocumentLocator(arg0);
        }

        public void startDocument() throws SAXException {
            // ignore
        }

        public void endDocument() throws SAXException {
            // ignore
        }

        public void startPrefixMapping(String arg0, String arg1) throws SAXException {
            ch.startPrefixMapping(arg0, arg1);
        }

        public void endPrefixMapping(String arg0) throws SAXException {
            ch.endPrefixMapping(arg0);
        }

        public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
            ch.startElement(arg0, arg1, arg2, arg3);
        }

        public void endElement(String arg0, String arg1, String arg2) throws SAXException {
            ch.endElement(arg0, arg1, arg2);
        }

        public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
            ch.characters(arg0, arg1, arg2);
        }

        public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {
            ch.ignorableWhitespace(arg0, arg1, arg2);
        }

        public void processingInstruction(String arg0, String arg1) throws SAXException {
            ch.processingInstruction(arg0, arg1);
        }

        public void skippedEntity(String arg0) throws SAXException {
            ch.skippedEntity(arg0);
        }

    };
    InputSource ins = new InputSource(new StringReader(toRender));
    XMLReader xmlReader;
    try {
        SAXParser saxParser = saxParserFactory.newSAXParser();

        xmlReader = saxParser.getXMLReader();
    }

    catch (Exception e) {
        log.error("SAXException when creating XMLReader", e); //$NON-NLS-1$
        // rethrow!!
        throw new SAXException(e);
    }
    xmlReader.setContentHandler(proxy);
    xmlReader.parse(ins);
}