List of usage examples for org.xml.sax ContentHandler startPrefixMapping
public void startPrefixMapping(String prefix, String uri) throws SAXException;
From source file:org.apache.cocoon.components.notification.Notifier.java
/** * Generate notification information in XML format. *//*from w w w . j a v a2 s.com*/ 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 w ww . 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); }/*ww w. j ava 2s . co 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 w w .jav a 2 s. c om 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 /> * //w w w .j a v a2 s. co m * @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); }// w w w. java 2s . c om 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();//from w ww . j a v a2 s . com 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:org.kalypso.gml.GMLSAXFactory.java
private void forcePrefixes(final GMLWorkspace workspace, final String rootNamespace) throws SAXException { final Set<String> forcedTypes = new TreeSet<>(); final String[] knownNamespaces = forceFeatureTypePrefixes(workspace); final String[] additionalNamespaces = forceAdditionalSchemaPrefixes(workspace); forcedTypes.addAll(Arrays.asList(knownNamespaces)); forcedTypes.addAll(Arrays.asList(additionalNamespaces)); forcedTypes.remove(rootNamespace);//from w w w . j av a 2 s. co m // REMARK: remove namespaces of some known simple types: the namespace definition is not needed for them forcedTypes.remove(XMLConstants.W3C_XML_SCHEMA_NS_URI); /* Add them all */ final ContentHandler contentHandler = m_reader.getContentHandler(); for (final String namespace : forcedTypes) { final String preferedPrefix = getPreferedPrefix(namespace); contentHandler.startPrefixMapping(preferedPrefix, namespace); } }
From source file:org.kalypso.ogc.sensor.deegree.ObservationLinkMarshaller.java
@Override public void marshall(final TimeseriesLinkType element) throws SAXException { /* Basic names */ final QName elementName = ObservationLinkHandler.TYPE_NAME; final String namespaceURI = elementName.getNamespaceURI(); final String qname = QNAME_OBSLINK_PREFIX + elementName.getLocalPart(); /* Build attributes */ final AttributesImpl attributes = new AttributesImpl(); addAttribute(attributes, "href", element.getHref()); //$NON-NLS-1$ addAttribute(attributes, "actuate", element.getActuate()); //$NON-NLS-1$ addAttribute(attributes, "arcrole", element.getArcrole()); //$NON-NLS-1$ // addAttribute( attributes, "linktype", element.getLinktype() ); //$NON-NLS-1$ addAttribute(attributes, "role", element.getRole()); //$NON-NLS-1$ addAttribute(attributes, "show", element.getShow()); //$NON-NLS-1$ // addAttribute( attributes, "timeaxis", element.getTimeaxis() ); //$NON-NLS-1$ addAttribute(attributes, "title", element.getTitle()); //$NON-NLS-1$ addAttribute(attributes, "type", element.getType()); //$NON-NLS-1$ // addAttribute( attributes, "valueaxis", element.getValueaxis() ); //$NON-NLS-1$ /* write it */ final ContentHandler contentHandler = m_reader.getContentHandler(); contentHandler.startPrefixMapping(DeegreeUrlCatalog.PREFIX_OBSLINK, namespaceURI); //$NON-NLS-1$ contentHandler.startPrefixMapping(PREFIX_XLINK, NS.XLINK); //$NON-NLS-1$ contentHandler.startElement(namespaceURI, elementName.getLocalPart(), qname, attributes); contentHandler.endElement(namespaceURI, elementName.getLocalPart(), qname); }
From source file:org.onehippo.cms7.autoexport.Exporter.java
private void exportDeltaXML(InitializeItem item, ContentHandler handler) throws SAXException, RepositoryException { DeltaInstruction rootInstruction = item.getDelta().getRootInstruction(); handler.startDocument();/* ww w. j a v a2 s . c om*/ handler.startPrefixMapping(SV_PREFIX, SV_URI); handler.startPrefixMapping(DELTA_PREFIX, DELTA_URI); exportInstruction(rootInstruction, handler); handler.endDocument(); }