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

The following examples show how to use org.xml.sax.ContentHandler#startDocument() . 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: MessageStreamCapTest.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Test
/*
 * if used as a ContentHandler, then the underlying buffer must be a Writer, probably a StringWriter
 */
public void testContentHandlerCap() throws Exception {
	INamedObject owner = new Owner();
	IForwardTarget forward = null;
	String expectedResponseMessage = "<root/>";
	try (MessageOutputStreamCap cap = new MessageOutputStreamCap(owner,forward)) {
		ContentHandler capContentHandler = cap.asContentHandler();
		
		Object capNative = cap.asNative();
		
		assertTrue(capNative instanceof Writer);
		
		capContentHandler.startDocument();
		capContentHandler.startElement("", "root", "root", new AttributesImpl());
		capContentHandler.endElement("", "root", "root");
		capContentHandler.endDocument();

		PipeRunResult result = cap.getPipeRunResult();
		
		assertEquals(expectedResponseMessage,result.getResult().asString());
	}
}
 
Example 2
Source File: AbstractMessageImpl.java    From openjdk-jdk9 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 3
Source File: AbstractMessageImpl.java    From openjdk-jdk8u-backup 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 4
Source File: CpeBuilder.java    From uima-uimafit with Apache License 2.0 6 votes vote down vote up
/**
 * Writes a temporary file containing a XML descriptor of the given resource. Returns the file.
 * 
 * @param resource
 *          A resource specifier that should we materialized.
 * @return The file containing the XML representation of the given resource.
 */
private static File materializeDescriptor(ResourceSpecifier resource) throws IOException,
        SAXException {
  File tempDesc = File.createTempFile("desc", ".xml");
  tempDesc.deleteOnExit();

  // Write the descriptor using XML 1.1 to allow a wider range of characters for parameter values
  try (OutputStream os = Files.newOutputStream(tempDesc.toPath())) {
    XMLSerializer sax2xml = new XMLSerializer(os, true);
    sax2xml.setOutputProperty(OutputKeys.VERSION, "1.1");
    ContentHandler contentHandler = sax2xml.getContentHandler();
    contentHandler.startDocument();
    resource.toXML(sax2xml.getContentHandler(), true);
    contentHandler.endDocument();
  }

  return tempDesc;
}
 
Example 5
Source File: AbstractMessageImpl.java    From TencentKona-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 6
Source File: XMLSerializerTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testXml10() throws Exception {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  XMLSerializer sax2xml = new XMLSerializer(baos, false);
  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);
  assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo/>", xmlStr);    
}
 
Example 7
Source File: ForkingFilter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Starts the event forking.
 */
public void startForking(String uri, String localName, String qName, Attributes atts, ContentHandler side) throws SAXException {
    if(this.side!=null)     throw new IllegalStateException();  // can't fork to two handlers

    this.side = side;
    depth = 1;
    side.setDocumentLocator(loc);
    side.startDocument();
    for( int i=0; i<namespaces.size(); i+=2 )
        side.startPrefixMapping(namespaces.get(i),namespaces.get(i+1));
    side.startElement(uri,localName,qName,atts);
}
 
Example 8
Source File: SAAJMessage.java    From openjdk-jdk9 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 9
Source File: SAAJMessage.java    From openjdk-jdk8u-backup 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 10
Source File: ForkingFilter.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Starts the event forking.
 */
public void startForking(String uri, String localName, String qName, Attributes atts, ContentHandler side) throws SAXException {
    if(this.side!=null)     throw new IllegalStateException();  // can't fork to two handlers

    this.side = side;
    depth = 1;
    side.setDocumentLocator(loc);
    side.startDocument();
    for( int i=0; i<namespaces.size(); i+=2 )
        side.startPrefixMapping(namespaces.get(i),namespaces.get(i+1));
    side.startElement(uri,localName,qName,atts);
}
 
Example 11
Source File: CanonicalTopicMapWriter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
/**
 * PUBLIC: Exports the topic map to the given ContentHandler.
 */

public void export(TopicMapIF topicmap, ContentHandler dh)
  throws IOException, SAXException {

  dh.startDocument();

  AttributesImpl atts = new AttributesImpl();
  atts.addAttribute("", "", "xmlns", CDATA,
                    "http://www.topicmaps.org/cxtm/1.0/");
  dh.startElement("", "", "topicMap", atts);
  atts.clear();

  // topics
  ContextHolder context = createContext(topicmap);
  Iterator<TopicIF> it = context.topicsInOrder(topicmap.getTopics());
  while (it.hasNext()) 
    writeTopic(it.next(), dh, context);

  // associations
  Iterator<AssociationIF> ait = context.assocsInOrder(topicmap.getAssociations());
  while (ait.hasNext()) 
    writeAssociation(ait.next(), dh, context);
      
  dh.endElement("", "", "topicMap");
  dh.endDocument();
}
 
Example 12
Source File: SAXEventBuffer.java    From citygml4j with Apache License 2.0 5 votes vote down vote up
public void send(ContentHandler handler, boolean release) throws SAXException {
	if (isEmpty())
		throw new IllegalStateException("buffer is empty.");

	eventBuffer = eventBuffer.rewindToHeadBuffer();
	stringBuffer = stringBuffer.rewindToHeadBuffer();
	charactersBuffer = charactersBuffer.rewindToHeadBuffer();
	tmpBuffer = new ArrayBuffer<String>(String.class);
	atts = new AttributesImpl();

	Byte currentEvent = null;
	while (eventBuffer.peek() != null) {
		currentEvent = nextEvent(release);

		if (currentEvent == START_ELEMENT)
			sendStartElement(handler, release);
		else if (currentEvent == END_ELEMENT)
			sendEndElement(handler);
		else if (currentEvent == CHARACTERS)
			sendCharacters(handler, release);
		else if (currentEvent == NAMESPACE_PREFIX_MAPPING)
			sendStartPrefixMapping(handler, release);
		else if (currentEvent == START_DOCUMENT)
			handler.startDocument();
		else if (currentEvent == END_DOCUMENT)
			handler.endDocument();
		else if (currentEvent == QUALIFIED_END_ELEMENT)
			sendQualifiedEndElement(handler, release);
	}

	// clean up
	eventBuffer.decrementPtr();
	stringBuffer.decrementPtr();
	charactersBuffer.decrementPtr();		

	if (release)
		reset();
}
 
Example 13
Source File: ForkContentHandler.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public void startDocument() throws SAXException {
   ContentHandler[] arr$ = this.handlers;
   int len$ = arr$.length;

   for(int i$ = 0; i$ < len$; ++i$) {
      ContentHandler handler = arr$[i$];
      handler.startDocument();
   }

}
 
Example 14
Source File: ForkContentHandler.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public void startDocument() throws SAXException {
   ContentHandler[] arr$ = this.handlers;
   int len$ = arr$.length;

   for(int i$ = 0; i$ < len$; ++i$) {
      ContentHandler handler = arr$[i$];
      handler.startDocument();
   }

}
 
Example 15
Source File: ForkingFilter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Starts the event forking.
 */
public void startForking(String uri, String localName, String qName, Attributes atts, ContentHandler side) throws SAXException {
    if(this.side!=null)     throw new IllegalStateException();  // can't fork to two handlers

    this.side = side;
    depth = 1;
    side.setDocumentLocator(loc);
    side.startDocument();
    for( int i=0; i<namespaces.size(); i+=2 )
        side.startPrefixMapping(namespaces.get(i),namespaces.get(i+1));
    side.startElement(uri,localName,qName,atts);
}
 
Example 16
Source File: XTMSnifferContentHandler.java    From ontopia with Apache License 2.0 4 votes vote down vote up
public void startElement_(String uri, String name, String qname,
                         Attributes atts) throws SAXException {
  // The XTM 1.0 reader can handle XML files where the XTM 1.0
  // content is wrapped in other XML content. We therefore need to
  // be able to pass by multiple elements before reaching the
  // topicMap element.
  
  ContentHandler outer_handler = null;

  if (XTMContentHandler.NS_XTM.equals(uri) ||
      (uri.isEmpty() && "topicMap".equals(qname))) {
    // We are reading XTM 1.0. Update accordingly.
    handler1 = new XTMContentHandler(store_factory,
                                     base_address);
    handler1.setExternalReferenceHandler(reader.getExternalReferenceHandler());
    handler1.register(parser);
    outer_handler = handler1;
    if (reader.getValidation()) {
      outer_handler = new XTMValidatingContentHandler(handler1);
      parser.setContentHandler(outer_handler);
    }
    
    // pass on events
    if (locator != null)
      outer_handler.setDocumentLocator(locator);
    Iterator it = entities.keySet().iterator();
    while (it.hasNext()) {
      String ename = (String) it.next();
      handler1.externalEntityDecl(ename, null, (String) entities.get(ename));
    }
    
    outer_handler.startDocument();
    for (int ix = 0; ix < stack_depth; ix++) // avoid EmptyStackException
      outer_handler.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME,  "fake-element", EMPTY_ATTS);
    outer_handler.startElement(uri, name, qname, atts);
    
  } else if (XTM2ContentHandler.NS_XTM2.equals(uri)) {
    // We are reading XTM 2.x. Update accordingly.
    handler2 = new XTM2ContentHandler(store_factory,
                                      base_address);
    parser.setContentHandler(handler2);
    outer_handler = handler2;

    if (reader.getValidation()) {
      outer_handler = new XTMValidatingContentHandler(handler2,
                                                      XTMVersion.XTM_2_0);
      parser.setContentHandler(outer_handler);
    }

    if (locator != null)
      outer_handler.setDocumentLocator(locator);
    outer_handler.startDocument();
    outer_handler.startElement(uri, name, qname, atts);
  }

  stack_depth++;
}
 
Example 17
Source File: Processor.java    From annotation-tools with MIT License 4 votes vote down vote up
public int process() throws TransformerException, IOException, SAXException
{
    ZipInputStream zis = new ZipInputStream(input);
    final ZipOutputStream zos = new ZipOutputStream(output);
    final OutputStreamWriter osw = new OutputStreamWriter(zos);

    Thread.currentThread()
            .setContextClassLoader(getClass().getClassLoader());

    TransformerFactory tf = TransformerFactory.newInstance();
    if (!tf.getFeature(SAXSource.FEATURE)
            || !tf.getFeature(SAXResult.FEATURE))
        return 0;

    SAXTransformerFactory saxtf = (SAXTransformerFactory) tf;
    Templates templates = null;
    if (xslt != null) {
        templates = saxtf.newTemplates(xslt);
    }

    // configuring outHandlerFactory
    // ///////////////////////////////////////////////////////

    EntryElement entryElement = getEntryElement(zos);

    ContentHandler outDocHandler = null;
    switch (outRepresentation) {
        case BYTECODE:
            outDocHandler = new OutputSlicingHandler(new ASMContentHandlerFactory(zos,
                    computeMax),
                    entryElement,
                    false);
            break;

        case MULTI_XML:
            outDocHandler = new OutputSlicingHandler(new SAXWriterFactory(osw,
                    true),
                    entryElement,
                    true);
            break;

        case SINGLE_XML:
            ZipEntry outputEntry = new ZipEntry(SINGLE_XML_NAME);
            zos.putNextEntry(outputEntry);
            outDocHandler = new SAXWriter(osw, false);
            break;

    }

    // configuring inputDocHandlerFactory
    // /////////////////////////////////////////////////
    ContentHandler inDocHandler = null;
    if (templates == null) {
        inDocHandler = outDocHandler;
    } else {
        inDocHandler = new InputSlicingHandler("class",
                outDocHandler,
                new TransformerHandlerFactory(saxtf,
                        templates,
                        outDocHandler));
    }
    ContentHandlerFactory inDocHandlerFactory = new SubdocumentHandlerFactory(inDocHandler);

    if (inDocHandler != null && inRepresentation != SINGLE_XML) {
        inDocHandler.startDocument();
        inDocHandler.startElement("",
                "classes",
                "classes",
                new AttributesImpl());
    }

    int i = 0;
    ZipEntry ze = null;
    while ((ze = zis.getNextEntry()) != null) {
        update(ze.getName(), n++);
        if (isClassEntry(ze)) {
            processEntry(zis, ze, inDocHandlerFactory);
        } else {
            OutputStream os = entryElement.openEntry(getName(ze));
            copyEntry(zis, os);
            entryElement.closeEntry();
        }

        i++;
    }

    if (inDocHandler != null && inRepresentation != SINGLE_XML) {
        inDocHandler.endElement("", "classes", "classes");
        inDocHandler.endDocument();
    }

    if (outRepresentation == SINGLE_XML) {
        zos.closeEntry();
    }
    zos.flush();
    zos.close();

    return i;
}
 
Example 18
Source File: Processor.java    From tajo with Apache License 2.0 4 votes vote down vote up
public int process() throws TransformerException, IOException, SAXException {
    ZipInputStream zis = new ZipInputStream(input);
    final ZipOutputStream zos = new ZipOutputStream(output);
    final OutputStreamWriter osw = new OutputStreamWriter(zos);

    Thread.currentThread().setContextClassLoader(
            getClass().getClassLoader());

    TransformerFactory tf = TransformerFactory.newInstance();
    if (!tf.getFeature(SAXSource.FEATURE)
            || !tf.getFeature(SAXResult.FEATURE)) {
        return 0;
    }

    SAXTransformerFactory saxtf = (SAXTransformerFactory) tf;
    Templates templates = null;
    if (xslt != null) {
        templates = saxtf.newTemplates(xslt);
    }

    // configuring outHandlerFactory
    // ///////////////////////////////////////////////////////

    EntryElement entryElement = getEntryElement(zos);

    ContentHandler outDocHandler = null;
    switch (outRepresentation) {
    case BYTECODE:
        outDocHandler = new OutputSlicingHandler(
                new ASMContentHandlerFactory(zos), entryElement, false);
        break;

    case MULTI_XML:
        outDocHandler = new OutputSlicingHandler(new SAXWriterFactory(osw,
                true), entryElement, true);
        break;

    case SINGLE_XML:
        ZipEntry outputEntry = new ZipEntry(SINGLE_XML_NAME);
        zos.putNextEntry(outputEntry);
        outDocHandler = new SAXWriter(osw, false);
        break;

    }

    // configuring inputDocHandlerFactory
    // /////////////////////////////////////////////////
    ContentHandler inDocHandler;
    if (templates == null) {
        inDocHandler = outDocHandler;
    } else {
        inDocHandler = new InputSlicingHandler("class", outDocHandler,
                new TransformerHandlerFactory(saxtf, templates,
                        outDocHandler));
    }
    ContentHandlerFactory inDocHandlerFactory = new SubdocumentHandlerFactory(
            inDocHandler);

    if (inDocHandler != null && inRepresentation != SINGLE_XML) {
        inDocHandler.startDocument();
        inDocHandler.startElement("", "classes", "classes",
                new AttributesImpl());
    }

    int i = 0;
    ZipEntry ze;
    while ((ze = zis.getNextEntry()) != null) {
        update(ze.getName(), n++);
        if (isClassEntry(ze)) {
            processEntry(zis, ze, inDocHandlerFactory);
        } else {
            OutputStream os = entryElement.openEntry(getName(ze));
            copyEntry(zis, os);
            entryElement.closeEntry();
        }

        i++;
    }

    if (inDocHandler != null && inRepresentation != SINGLE_XML) {
        inDocHandler.endElement("", "classes", "classes");
        inDocHandler.endDocument();
    }

    if (outRepresentation == SINGLE_XML) {
        zos.closeEntry();
    }
    zos.flush();
    zos.close();

    return i;
}
 
Example 19
Source File: SaxDocumentBuilder.java    From iaf with Apache License 2.0 4 votes vote down vote up
public SaxDocumentBuilder(String elementName, ContentHandler handler) throws SAXException {
	super(elementName, handler);
	handler.startDocument();
}
 
Example 20
Source File: DOMContentHandlerDecoratorImpl.java    From Asqatasun with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void startDocument() throws SAXException {
    for (ContentHandler contentHandler : contentHandlerSet) {
        contentHandler.startDocument();
    }
}