Example usage for org.xml.sax ContentHandler endElement

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

Introduction

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

Prototype

public void endElement(String uri, String localName, String qName) throws SAXException;

Source Link

Document

Receive notification of the end of an element.

Usage

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 a  v a2  s .c om
    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 processFeature(final Feature feature, final AttributesImpl attributes) throws SAXException {
    final ContentHandler contentHandler = m_reader.getContentHandler();

    final IFeatureType featureType = feature.getFeatureType();

    /* Add gml:id or fid attribute */
    final String id = feature.getId();
    if (id != null && id.length() > 0) {
        final String version = featureType.getGMLSchema().getGMLVersion();
        final QName idQName = getPrefixedQName(GMLSchemaUtilities.getIdAttribute(version));
        addAttribute(attributes, idQName, "CDATA", id);
    }/*from   w  w  w  . ja  v a2s  . com*/

    /* Write opening tag */
    final QName prefixedQName = getPrefixedQName(feature.getFeatureType().getQName());
    final String localPart = prefixedQName.getLocalPart();
    final String uri = prefixedQName.getNamespaceURI();
    final String qname = elementQName(prefixedQName);
    contentHandler.startElement(uri, localPart, qname, attributes);

    /* Write properties */
    final IPropertyType[] properties = featureType.getProperties();
    for (final IPropertyType pt : properties) {
        // Virtual properties are properties which do not get serialized...
        if (pt.isVirtual())
            continue;

        // REMARK: this only works for sequences of the property!
        // If the content of (i.e. the element itself has the maxOccurs > 1 this will not
        // work. In order to support this, we need a FeatureProperty object as value object (as in deegree2)
        final Object value = feature.getProperty(pt);
        if (pt.isList()) {
            final List<?> values = (List<?>) value;
            if (values != null) {
                /* FIXME: ConcurrentModificationExceptions happens sometimes here */
                for (final Object propertyValue : values)
                    processProperty(pt, propertyValue);
            }
        } else {
            // If value == null && minOccurs == 0 do not write an element
            if (value != null || pt.getMinOccurs() > 0)
                processProperty(pt, value);
        }
    }

    /* Write closing tag */
    contentHandler.endElement(uri, localPart, qname);
}

From source file:org.kalypso.gml.GMLSAXFactory.java

/**
 * Writes one single property//from  w  ww . j ava2s  .  c om
 */
private void processProperty(final IPropertyType pt, final Object propertyValue) throws SAXException {
    final ContentHandler contentHandler = m_reader.getContentHandler();

    final QName name = pt.getQName();
    final QName prefixedQName = getPrefixedQName(name);
    final String uri = prefixedQName.getNamespaceURI();
    final String localPart = prefixedQName.getLocalPart();

    // Find attributes for the current property
    final Attributes atts = attributeForProperty(pt, propertyValue);

    /* Write starting tag */
    final String qname = elementQName(prefixedQName);
    contentHandler.startElement(uri, localPart, qname, atts);

    if (pt instanceof IRelationType) {
        // Write the feature as content. If it is a reference (i.e. no feature), nothing is written, as the href was
        // already set as an attribute
        if (propertyValue instanceof Feature && !(propertyValue instanceof IXLinkedFeature))
            processFeature((Feature) propertyValue, new AttributesImpl());
    } else if (pt instanceof IValuePropertyType)
        processValueType((IValuePropertyType) pt, propertyValue, prefixedQName);
    else
        throw new UnsupportedOperationException();

    /* Write ending tag */
    contentHandler.endElement(uri, localPart, qname);
}

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.nuxeo.ecm.diff.content.adapter.HtmlContentDiffer.java

public List<Blob> getContentDiff(Blob leftBlob, Blob rightBlob, Locale locale) throws ContentDiffException {

    try {//  www . j a va 2  s  . c  om
        List<Blob> blobResults = new ArrayList<Blob>();
        StringWriter sw = new StringWriter();

        SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance();
        TransformerHandler transformHandler = stf.newTransformerHandler();
        transformHandler.setResult(new StreamResult(sw));

        XslFilter htmlHeaderXslFilter = new XslFilter();

        StringBuilder sb = new StringBuilder("xslfilter/htmldiffheader");
        sb.append("_");
        sb.append(locale.getLanguage());
        sb.append(".xsl");
        String htmlHeaderXslPath = sb.toString();
        ContentHandler postProcess;
        try {
            postProcess = htmlHeaderXslFilter.xsl(transformHandler, htmlHeaderXslPath);
        } catch (IllegalStateException ise) {
            LOGGER.error(String.format(
                    "Could not find the HTML diff header xsl file '%s', falling back on the default one.",
                    htmlHeaderXslPath), ise);
            postProcess = htmlHeaderXslFilter.xsl(transformHandler, "xslfilter/htmldiffheader.xsl");
        }

        String prefix = "diff";

        HtmlCleaner cleaner = new HtmlCleaner();

        InputSource leftIS = new InputSource(leftBlob.getStream());
        InputSource rightIS = new InputSource(rightBlob.getStream());

        DomTreeBuilder leftHandler = new DomTreeBuilder();
        cleaner.cleanAndParse(leftIS, leftHandler);
        TextNodeComparator leftComparator = new TextNodeComparator(leftHandler, locale);

        DomTreeBuilder rightHandler = new DomTreeBuilder();
        cleaner.cleanAndParse(rightIS, rightHandler);
        TextNodeComparator rightComparator = new TextNodeComparator(rightHandler, locale);

        postProcess.startDocument();
        postProcess.startElement("", "diffreport", "diffreport", new AttributesImpl());
        postProcess.startElement("", "diff", "diff", new AttributesImpl());
        HtmlSaxDiffOutput output = new HtmlSaxDiffOutput(postProcess, prefix);

        HTMLDiffer differ = new HTMLDiffer(output);
        differ.diff(leftComparator, rightComparator);

        postProcess.endElement("", "diff", "diff");
        postProcess.endElement("", "diffreport", "diffreport");
        postProcess.endDocument();

        String stringBlob = sw.toString().replaceAll(NUXEO_DEFAULT_CONTEXT_PATH,
                VirtualHostHelper.getContextPathProperty());
        Blob mainBlob = Blobs.createBlob(stringBlob);
        sw.close();

        mainBlob.setFilename("contentDiff.html");
        mainBlob.setMimeType("text/html");

        blobResults.add(mainBlob);
        return blobResults;

    } catch (Exception e) {
        throw new ContentDiffException(e);
    }
}

From source file:org.onehippo.cms7.autoexport.Exporter.java

private void exportInstruction(DeltaInstruction instruction, ContentHandler handler)
        throws SAXException, RepositoryException {
    if (instruction.isNoneDirective()) {
        if (instruction.isNodeInstruction()) {
            List<String> subContextPaths = new ArrayList<String>();
            for (InitializeItem child : registry.getDescendentInitializeItems(instruction.getContextPath())) {
                subContextPaths.add(child.getContextPath());
            }/*from  w w w . j ava 2  s.co m*/
            ExclusionContext exclusionContext = new ExclusionContext(configuration.getExclusionContext(),
                    subModuleExclusionPatterns);
            ContentHandler filter = new FilterContentHandler(new EmbeddedContentHandler(handler),
                    instruction.getParentPath(), subContextPaths, configuration.getFilterUuidPaths(),
                    exclusionContext);
            ((HippoSession) session).exportDereferencedView(instruction.getContextPath(), filter, false, false);
        } else {
            exportPropertyInstruction(instruction, handler, false);
        }
    } else if (instruction.isCombineDirective()) {
        AttributesImpl attr = new AttributesImpl();
        attr.addAttribute(SV_URI, NAME, QNAME, CDATA, instruction.getName());
        attr.addAttribute(DELTA_URI, MERGE, QMERGE, CDATA, instruction.getDirective());
        handler.startElement(SV_URI, NODE, QNODE, attr);
        if (instruction.getPropertyInstructions() != null) {
            for (DeltaInstruction child : instruction.getPropertyInstructions()) {
                exportInstruction(child, handler);
            }
        }
        if (instruction.getNodeInstructions() != null) {
            for (DeltaInstruction child : instruction.getNodeInstructions()) {
                exportInstruction(child, handler);
            }
        }
        handler.endElement(SV_URI, NODE, QNODE);
    } else if (instruction.isOverrideDirective()) {
        exportPropertyInstruction(instruction, handler, true);
    }
}

From source file:org.onehippo.cms7.autoexport.Exporter.java

private void exportPropertyInstruction(DeltaInstruction instruction, ContentHandler handler, boolean override)
        throws SAXException, RepositoryException {
    Property property = session.getProperty(instruction.getContextPath());
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute(SV_URI, NAME, QNAME, CDATA, instruction.getName());
    attr.addAttribute(SV_URI, TYPE, QTYPE, CDATA, PropertyType.nameFromValue(property.getType()));
    if (override) {
        attr.addAttribute(DELTA_URI, MERGE, QMERGE, CDATA, "override");
    }//from ww  w. j  ava2  s .  c  om
    if (property.isMultiple()) {
        attr.addAttribute(SV_URI, "multiple", "sv:multiple", CDATA, "true");
    }
    handler.startElement(SV_URI, PROPERTY, QPROPERTY, attr);
    attr = new AttributesImpl();
    if (property.isMultiple()) {
        for (Value value : property.getValues()) {
            handler.startElement(SV_URI, VALUE, QVALUE, attr);
            String stringValue = value.getString();
            handler.characters(stringValue.toCharArray(), 0, stringValue.length());
            handler.endElement(SV_URI, VALUE, QVALUE);
        }
    } else {
        Value value = property.getValue();
        handler.startElement(SV_URI, VALUE, QVALUE, attr);
        String stringValue = value.getString();
        handler.characters(stringValue.toCharArray(), 0, stringValue.length());
        handler.endElement(SV_URI, VALUE, QVALUE);
    }
    handler.endElement(SV_URI, PROPERTY, QPROPERTY);
}

From source file:org.orbeon.oxf.processor.LDAPProcessor.java

private void serialize(List results, Config config, ContentHandler ch) {
    try {// w  w  w.  j  a v  a 2s.  c o m
        ch.startDocument();
        ch.startElement("", "results", "results", SAXUtils.EMPTY_ATTRIBUTES);
        for (Iterator i = results.iterator(); i.hasNext();) {
            SearchResult sr = (SearchResult) i.next();

            ch.startElement("", "result", "result", SAXUtils.EMPTY_ATTRIBUTES);
            addElement(ch, "name", sr.getName());
            try {
                addElement(ch, "fullname", sr.getNameInNamespace());
            } catch (UnsupportedOperationException e) {
                // This seems to be the only  way to know if sr contains a name!
            }
            Attributes attr = sr.getAttributes();
            NamingEnumeration attrEn = attr.getAll();
            while (attrEn.hasMoreElements()) {
                Attribute a = (Attribute) attrEn.next();
                if (config.getAttributes().isEmpty() || config.getAttributes().contains(a.getID())) {
                    ch.startElement("", "attribute", "attribute", SAXUtils.EMPTY_ATTRIBUTES);
                    addElement(ch, "name", a.getID());
                    NamingEnumeration aEn = a.getAll();
                    while (aEn.hasMoreElements()) {
                        Object o = aEn.next();
                        addElement(ch, "value", o.toString());
                    }
                    ch.endElement("", "attribute", "attribute");
                }
            }
            ch.endElement("", "result", "result");
        }
        ch.endElement("", "results", "results");
        ch.endDocument();
    } catch (Exception e) {
        throw new OXFException(e);
    }
}

From source file:org.orbeon.oxf.processor.LDAPProcessor.java

private void addElement(ContentHandler contentHandler, String name, String value) throws Exception {
    if (value != null) {
        contentHandler.startElement("", name, name, SAXUtils.EMPTY_ATTRIBUTES);
        addString(contentHandler, value);
        contentHandler.endElement("", name, name);
    }/*from   ww w.java2 s.c  o m*/
}

From source file:org.orbeon.oxf.xforms.processor.handlers.OutputInterceptor.java

public void outputDelimiter(ContentHandler contentHandler, String delimiterNamespaceURI, String delimiterPrefix,
        String delimiterLocalName, String classes, String id) throws SAXException {

    reusableAttributes.clear();/*from w w  w. j a va  2 s.  com*/
    if (id != null)
        reusableAttributes.addAttribute("", "id", "id", ContentHandlerHelper.CDATA, id);

    if (classes != null)
        reusableAttributes.addAttribute("", "class", "class", ContentHandlerHelper.CDATA, classes);

    final String delimiterQName = XMLUtils.buildQName(delimiterPrefix, delimiterLocalName);
    contentHandler.startElement(delimiterNamespaceURI, delimiterLocalName, delimiterQName, reusableAttributes);
    contentHandler.endElement(delimiterNamespaceURI, delimiterLocalName, delimiterQName);
}