Java Code Examples for org.xml.sax.ContentHandler#endElement()

The following examples show how to use org.xml.sax.ContentHandler#endElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractMessageImpl.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Writes the whole envelope as SAX events.
 */
@Override
public void writeTo( ContentHandler contentHandler, ErrorHandler errorHandler ) throws SAXException {
    String soapNsUri = soapVersion.nsUri;

    contentHandler.setDocumentLocator(NULL_LOCATOR);
    contentHandler.startDocument();
    contentHandler.startPrefixMapping("S",soapNsUri);
    contentHandler.startElement(soapNsUri,"Envelope","S:Envelope",EMPTY_ATTS);
    if(hasHeaders()) {
        contentHandler.startElement(soapNsUri,"Header","S:Header",EMPTY_ATTS);
        MessageHeaders headers = getHeaders();
        for (Header h : headers.asList()) {
            h.writeTo(contentHandler,errorHandler);
        }
        contentHandler.endElement(soapNsUri,"Header","S:Header");
    }
    // write the body
    contentHandler.startElement(soapNsUri,"Body","S:Body",EMPTY_ATTS);
    writePayloadTo(contentHandler,errorHandler, true);
    contentHandler.endElement(soapNsUri,"Body","S:Body");
    contentHandler.endElement(soapNsUri,"Envelope","S:Envelope");
}
 
Example 2
Source File: XTM2TopicMapExporter.java    From ontopia with Apache License 2.0 6 votes vote down vote up
private void write(OccurrenceIF occ, ContentHandler dh) throws SAXException{
  atts.clear();
  addReifier(atts, occ);
  dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "occurrence", atts);
  writeReifier(occ, dh);
  writeItemIdentities(occ, dh);
  writeType(occ, dh);
  writeScope(occ, dh);

  atts.clear();
  if (occ.getDataType().equals(DataTypes.TYPE_URI)) {
    atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, HREF, CDATA, occ.getLocator().getExternalForm());
    dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, RESOURCEREF, atts);
    dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, RESOURCEREF);
  } else {
    addDatatype(atts, occ.getDataType());
    dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, RESOURCEDATA, atts);
    write(occ.getValue(), dh);
    dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, RESOURCEDATA);
  }
  
  dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "occurrence");
}
 
Example 3
Source File: OSLSchemaWriter.java    From ontopia with Apache License 2.0 6 votes vote down vote up
protected void exportScope(ScopedConstraintIF constraint, ContentHandler dh)
  throws SAXException {
  if (constraint.getScopeSpecification() == null)
    return;
  
  int match = constraint.getScopeSpecification().getMatch();
  if (match == ScopeSpecification.MATCH_SUPERSET)
    dh.startElement("", "", "scope", getAttributes("match", "superset"));
  else if (match == ScopeSpecification.MATCH_SUBSET)
    dh.startElement("", "", "scope", getAttributes("match", "subset"));
  else
    dh.startElement("", "", "scope", EMPTY_ATTR_LIST);

  exportMatchers(constraint.getScopeSpecification().getThemeMatchers(), dh);
  dh.endElement("", "", "scope");   
}
 
Example 4
Source File: AbstractMessageImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Writes the whole envelope as SAX events.
 */
@Override
public void writeTo( ContentHandler contentHandler, ErrorHandler errorHandler ) throws SAXException {
    String soapNsUri = soapVersion.nsUri;

    contentHandler.setDocumentLocator(NULL_LOCATOR);
    contentHandler.startDocument();
    contentHandler.startPrefixMapping("S",soapNsUri);
    contentHandler.startElement(soapNsUri,"Envelope","S:Envelope",EMPTY_ATTS);
    if(hasHeaders()) {
        contentHandler.startElement(soapNsUri,"Header","S:Header",EMPTY_ATTS);
        MessageHeaders headers = getHeaders();
        for (Header h : headers.asList()) {
            h.writeTo(contentHandler,errorHandler);
        }
        contentHandler.endElement(soapNsUri,"Header","S:Header");
    }
    // write the body
    contentHandler.startElement(soapNsUri,"Body","S:Body",EMPTY_ATTS);
    writePayloadTo(contentHandler,errorHandler, true);
    contentHandler.endElement(soapNsUri,"Body","S:Body");
    contentHandler.endElement(soapNsUri,"Envelope","S:Envelope");
}
 
Example 5
Source File: CasProcessorFilterImpl.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
@Override
public void toXML(ContentHandler aContentHandler, boolean aWriteDefaultNamespaceAttribute)
        throws SAXException {
  XmlizationInfo inf = getXmlizationInfo();

  // write the element's start tag
  // get attributes (can be provided by subclasses)
  AttributesImpl attrs = getXMLAttributes();
  // add default namespace attr if desired
  if (aWriteDefaultNamespaceAttribute) {
    if (inf.namespace != null) {
      attrs.addAttribute("", "xmlns", "xmlns", null, inf.namespace);
    }
  }

  // start element
  aContentHandler.startElement(inf.namespace, inf.elementTagName, inf.elementTagName, attrs);

  aContentHandler.characters(filter.toCharArray(), 0, filter.length());

  // end element
  aContentHandler.endElement(inf.namespace, inf.elementTagName, inf.elementTagName);
}
 
Example 6
Source File: XmlDataSetConsumer.java    From morf with Apache License 2.0 6 votes vote down vote up
/**
 * Serialise the meta data for a table.
 *
 * @param table The meta data to serialise.
 * @param contentHandler Content handler to receive the meta data xml.
 * @throws SAXException Propagates from SAX API calls.
 */
private void outputTableMetaData(Table table, ContentHandler contentHandler) throws SAXException {
  AttributesImpl tableAttributes = new AttributesImpl();
  tableAttributes.addAttribute(XmlDataSetNode.URI, XmlDataSetNode.NAME_ATTRIBUTE, XmlDataSetNode.NAME_ATTRIBUTE,
    XmlDataSetNode.STRING_TYPE, table.getName());
  contentHandler.startElement(XmlDataSetNode.URI, XmlDataSetNode.METADATA_NODE, XmlDataSetNode.METADATA_NODE, tableAttributes);

  for (Column column : table.columns()) {
    emptyElement(contentHandler, XmlDataSetNode.COLUMN_NODE, buildColumnAttributes(column));
  }

  // we need to sort the indexes by name to ensure consistency, since indexes don't have an explicit "sequence" in databases.
  List<Index> indexes = new ArrayList<>(table.indexes());
  Collections.sort(indexes, new Comparator<Index>() {
    @Override
    public int compare(Index o1, Index o2) {
      return o1.getName().compareTo(o2.getName());
    }
  });

  for (Index index : indexes) {
    emptyElement(contentHandler, XmlDataSetNode.INDEX_NODE, buildIndexAttributes(index));
  }

  contentHandler.endElement(XmlDataSetNode.URI, XmlDataSetNode.METADATA_NODE, XmlDataSetNode.METADATA_NODE);
}
 
Example 7
Source File: XTM2TopicMapExporter.java    From ontopia with Apache License 2.0 6 votes vote down vote up
private void write(AssociationIF assoc, ContentHandler dh)
  throws SAXException {
  Collection roles = filterCollection(assoc.getRoles());
  if (roles.isEmpty())
    return; // don't export empty assocs; they aren't valid
  
  atts.clear();
  addReifier(atts, assoc);
  dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "association", atts);
  writeReifier(assoc, dh);
  writeItemIdentities(assoc, dh);
  writeType(assoc, dh);
  writeScope(assoc, dh);

  Iterator it = assoc.getRoles().iterator();
  while (it.hasNext())
    write((AssociationRoleIF) it.next(), dh);
 
  dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "association");
}
 
Example 8
Source File: XSLTEntityHandler.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Adds an element to the content handler.
 * 
 * @param ch
 *        the content handler
 * @param ns
 *        the name space of the element
 * @param lname
 *        the local name
 * @param qname
 *        the qname
 * @param attr
 *        the attribute list
 * @param content
 *        content of the element
 * @throws SAXException
 *         if the underlying sax chain has a problem
 */
public void addElement(final ContentHandler ch, final String ns, final String lname,
		final String qname, final Attributes attr, final Object content)
		throws SAXException
{

	ch.startElement(ns, lname, qname, attr);
	try
	{
		if (content != null)
		{
			char[] c = String.valueOf(content).toCharArray();
			ch.characters(c, 0, c.length);
		}
	}
	finally
	{
		ch.endElement(ns, lname, qname);
	}
}
 
Example 9
Source File: TagInfoset.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes the end element event.
 */
public void writeEnd(ContentHandler contentHandler) throws SAXException{
    contentHandler.endElement(fixNull(nsUri),localName,getQName());
    for( int i=ns.length-2; i>=0; i-=2 ) {
        contentHandler.endPrefixMapping(fixNull(ns[i]));
    }
}
 
Example 10
Source File: XTMTopicMapExporter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
protected void writeInstanceOf(TypedIF typed, ContentHandler dh)
    throws SAXException {
  TopicIF type = typed.getType();
  if (type != null && filterOk(type)) {
    dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, INSTANCE_OF, EMPTY_ATTR_LIST);
    writeTopicRef(type, dh);
    dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, INSTANCE_OF);
  }
}
 
Example 11
Source File: XMLSerializerTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testXml11() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLSerializer sax2xml = new XMLSerializer(baos, false);
    sax2xml.setOutputProperty(OutputKeys.VERSION, "1.1");
    ContentHandler ch = sax2xml.getContentHandler();    
    ch.startDocument();
    ch.startElement("","foo","foo", new AttributesImpl());
    ch.endElement("", "foo", "foo");
    ch.endDocument();
    String xmlStr = new String(baos.toByteArray(), StandardCharsets.UTF_8);
//    if (xmlStr.contains("1.0")) {
    // useful to investigate issues when bad XML output is produced
    //   related to which Java implementation is being used
      TransformerFactory transformerFactory = XMLUtils.createTransformerFactory();
      Transformer t = transformerFactory.newTransformer();
      t.setOutputProperty(OutputKeys.VERSION, "1.1");
      
      System.out.println("Java version is " + 
                            System.getProperty("java.vendor") + " " +
                            System.getProperty("java.version") + " " +
                            System.getProperty("java.vm.name") + " " +
                            System.getProperty("java.vm.version") + 
                         "\n  javax.xml.transform.TransformerFactory: " +
                            System.getProperty("javax.xml.transform.TransformerFactory") + 
                         "\n  Transformer version: " +
                            t.getOutputProperty(OutputKeys.VERSION));
//    }
    assertEquals("<?xml version=\"1.1\" encoding=\"UTF-8\"?><foo/>", xmlStr);
  }
 
Example 12
Source File: FSEditLogOp.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private static void appendAclEntriesToXml(ContentHandler contentHandler,
    List<AclEntry> aclEntries) throws SAXException {
  for (AclEntry e : aclEntries) {
    contentHandler.startElement("", "", "ENTRY", new AttributesImpl());
    XMLUtils.addSaxString(contentHandler, "SCOPE", e.getScope().name());
    XMLUtils.addSaxString(contentHandler, "TYPE", e.getType().name());
    if (e.getName() != null) {
      XMLUtils.addSaxString(contentHandler, "NAME", e.getName());
    }
    fsActionToXml(contentHandler, e.getPermission());
    contentHandler.endElement("", "", "ENTRY");
  }
}
 
Example 13
Source File: TagInfoset.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes the end element event.
 */
public void writeEnd(ContentHandler contentHandler) throws SAXException{
    contentHandler.endElement(fixNull(nsUri),localName,getQName());
    for( int i=ns.length-2; i>=0; i-=2 ) {
        contentHandler.endPrefixMapping(fixNull(ns[i]));
    }
}
 
Example 14
Source File: FSEditLogOp.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
protected void toXml(ContentHandler contentHandler) throws SAXException {
  XMLUtils.addSaxString(contentHandler, "LENGTH",
      Integer.toString(length));
  XMLUtils.addSaxString(contentHandler, "TRG", trg);
  XMLUtils.addSaxString(contentHandler, "TIMESTAMP",
      Long.toString(timestamp));
  contentHandler.startElement("", "", "SOURCES", new AttributesImpl());
  for (int i = 0; i < srcs.length; ++i) {
    XMLUtils.addSaxString(contentHandler,
        "SOURCE" + (i + 1), srcs[i]);
  }
  contentHandler.endElement("", "", "SOURCES");
  appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId);
}
 
Example 15
Source File: SAAJMessage.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
    String soapNsUri = soapVersion.nsUri;
    if (!parsedMessage) {
        DOMScanner ds = new DOMScanner();
        ds.setContentHandler(contentHandler);
        ds.scan(sm.getSOAPPart());
    } else {
        contentHandler.setDocumentLocator(NULL_LOCATOR);
        contentHandler.startDocument();
        contentHandler.startPrefixMapping("S", soapNsUri);
        startPrefixMapping(contentHandler, envelopeAttrs,"S");
        contentHandler.startElement(soapNsUri, "Envelope", "S:Envelope", getAttributes(envelopeAttrs));
        if (hasHeaders()) {
            startPrefixMapping(contentHandler, headerAttrs,"S");
            contentHandler.startElement(soapNsUri, "Header", "S:Header", getAttributes(headerAttrs));
            MessageHeaders headers = getHeaders();
            for (Header h : headers.asList()) {
                h.writeTo(contentHandler, errorHandler);
            }
            endPrefixMapping(contentHandler, headerAttrs,"S");
            contentHandler.endElement(soapNsUri, "Header", "S:Header");

        }
        startPrefixMapping(contentHandler, bodyAttrs,"S");
        // write the body
        contentHandler.startElement(soapNsUri, "Body", "S:Body", getAttributes(bodyAttrs));
        writePayloadTo(contentHandler, errorHandler, true);
        endPrefixMapping(contentHandler, bodyAttrs,"S");
        contentHandler.endElement(soapNsUri, "Body", "S:Body");
        endPrefixMapping(contentHandler, envelopeAttrs,"S");
        contentHandler.endElement(soapNsUri, "Envelope", "S:Envelope");
    }
}
 
Example 16
Source File: SAAJMessage.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
    String soapNsUri = soapVersion.nsUri;
    if (!parsedMessage) {
        DOMScanner ds = new DOMScanner();
        ds.setContentHandler(contentHandler);
        ds.scan(sm.getSOAPPart());
    } else {
        contentHandler.setDocumentLocator(NULL_LOCATOR);
        contentHandler.startDocument();
        contentHandler.startPrefixMapping("S", soapNsUri);
        startPrefixMapping(contentHandler, envelopeAttrs,"S");
        contentHandler.startElement(soapNsUri, "Envelope", "S:Envelope", getAttributes(envelopeAttrs));
        if (hasHeaders()) {
            startPrefixMapping(contentHandler, headerAttrs,"S");
            contentHandler.startElement(soapNsUri, "Header", "S:Header", getAttributes(headerAttrs));
            MessageHeaders headers = getHeaders();
            for (Header h : headers.asList()) {
                h.writeTo(contentHandler, errorHandler);
            }
            endPrefixMapping(contentHandler, headerAttrs,"S");
            contentHandler.endElement(soapNsUri, "Header", "S:Header");

        }
        startPrefixMapping(contentHandler, bodyAttrs,"S");
        // write the body
        contentHandler.startElement(soapNsUri, "Body", "S:Body", getAttributes(bodyAttrs));
        writePayloadTo(contentHandler, errorHandler, true);
        endPrefixMapping(contentHandler, bodyAttrs,"S");
        contentHandler.endElement(soapNsUri, "Body", "S:Body");
        endPrefixMapping(contentHandler, envelopeAttrs,"S");
        contentHandler.endElement(soapNsUri, "Envelope", "S:Envelope");
    }
}
 
Example 17
Source File: CanonicalTopicMapWriter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private void writeScope(ScopedIF scoped, ContentHandler dh,
                        ContextHolder context) throws SAXException {
  if (scoped.getScope().size() > 0) {
    dh.startElement("", "", "scope", empty);
      
    Iterator<TopicIF> it = context.topicRefsInOrder(scoped.getScope());
    while (it.hasNext())
      writeTopicRef(it.next(), dh, context);

    dh.endElement("", "", "scope");
  }
}
 
Example 18
Source File: XTMTopicMapExporter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
protected void writeTopicRef(TopicIF topic, ContentHandler dh)
    throws SAXException {
  atts.clear();
  atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, XLINK_HREF, CDATA, "#" + getElementId(topic));
  dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "topicRef", atts);
  dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "topicRef");
}
 
Example 19
Source File: CpeCasProcessorsImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
@Override
public void toXML(ContentHandler aContentHandler, boolean aWriteDefaultNamespaceAttribute)
        throws SAXException {
  XmlizationInfo inf = getXmlizationInfo();

  // write the element's start tag
  // get attributes (can be provided by subclasses)
  AttributesImpl attrs = getXMLAttributes();
  // add default namespace attr if desired
  if (aWriteDefaultNamespaceAttribute) {
    if (inf.namespace != null) {
      attrs.addAttribute("", "xmlns", "xmlns", null, inf.namespace);
    }
  }

  // start element
  aContentHandler.startElement(inf.namespace, inf.elementTagName, inf.elementTagName, attrs);

  // write child elements
  for (int i = 0; i < casProcessors.size(); i++) {
    ((CpeCasProcessor) casProcessors.get(i)).toXML(aContentHandler,
            aWriteDefaultNamespaceAttribute);
  }

  // end element
  aContentHandler.endElement(inf.namespace, inf.elementTagName, inf.elementTagName);
}
 
Example 20
Source File: AdditionalObjectsHandler.java    From web-feature-service with Apache License 2.0 4 votes vote down vote up
protected void writeObjects() {
    if (!shouldRun)
        return;

    try {
        // close writers to flush buffers
        for (CityModelWriter tempWriter : tempWriters.values())
            tempWriter.close();

        CityGMLInputFactory in = cityGMLBuilder.createCityGMLInputFactory();
        in.setProperty(CityGMLInputFactory.FEATURE_READ_MODE, FeatureReadMode.SPLIT_PER_COLLECTION_MEMBER);

        startAdditionalObjects();

        Attributes dummyAttributes = new AttributesImpl();
        String propertyName = "member";
        String propertyQName = saxWriter.getPrefix(Constants.WFS_NAMESPACE_URI) + ":" + propertyName;

        // iterate over temp files and send additional objects to response stream
        for (Path tempFile : tempFiles.values()) {
            try (CityGMLReader tempReader = in.createCityGMLReader(tempFile.toFile())) {
                while (tempReader.hasNext()) {
                    XMLChunk chunk = tempReader.nextChunk();
                    if ("CityModel".equals(chunk.getTypeName().getLocalPart())
                            && Modules.isCityGMLModuleNamespace(chunk.getTypeName().getNamespaceURI()))
                        continue;

                    ContentHandler handler;
                    if (transformerChainFactory == null)
                        handler = saxWriter;
                    else {
                        TransformerChain chain = transformerChainFactory.buildChain();
                        chain.tail().setResult(new SAXResult(saxWriter));
                        handler = chain.head();
                        handler.startDocument();
                    }

                    handler.startElement(Constants.WFS_NAMESPACE_URI, propertyName, propertyQName, dummyAttributes);
                    chunk.send(handler, true);
                    handler.endElement(Constants.WFS_NAMESPACE_URI, propertyName, propertyQName);

                    if (transformerChainFactory != null)
                        handler.endDocument();
                }
            }
        }

    } catch (CityGMLWriteException | CityGMLBuilderException | CityGMLReadException | SAXException | TransformerConfigurationException e) {
        eventDispatcher.triggerSyncEvent(new InterruptEvent("Failed to write additional objects.", LogLevel.ERROR, e, eventChannel, this));
    }
}