Java Code Examples for org.xml.sax.ContentHandler

The following examples show how to use org.xml.sax.ContentHandler. These examples are extracted from open source projects. 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 Project: ontopia   Author: ontopia   File: OSLSchemaWriter.java    License: Apache License 2.0 6 votes vote down vote up
protected void export(TopicRoleConstraint constraint, ContentHandler dh)
  throws SAXException {
  dh.startElement("", "", "playing", getMinMax(constraint));

  exportInstanceOf(constraint.getTypeSpecification(), dh);
  Iterator it = constraint.getAssociationTypes().iterator();
  if (it.hasNext()) {
    dh.startElement("", "", "in", EMPTY_ATTR_LIST);
    while (it.hasNext()) {
      TypeSpecification spec = (TypeSpecification) it.next();
      exportInstanceOf(spec, dh);
    }
    dh.endElement("", "", "in");
  }
  
  dh.endElement("", "", "playing");
}
 
Example #2
Source Project: uima-uimaj   Author: apache   File: XTalkToSAX.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Parse one document off of the incoming XTalk stream into SAX events. A side effect of parsing
 * is that internal arrays will grow to the size of the largest character string encountered in
 * the document. Use bufferSize() and resizeBuffers to manage memory in applications where very
 * large strings may be encountered and the same object is used to parse many incoming documents.
 * 
 * @param is -
 * @param handler -
 * @throws IOException
 *           if underlying IOException from the stream or if XTalk format is invalid.
 * @throws SAXException
 *           if SAXException thrown by the handler
 * 
 * @pre handler != null
 * @pre is != null
 */
public void parse(InputStream is, ContentHandler handler) throws IOException, SAXException {
  this.is = is;
  this.handler = handler;
  try {
    int marker = is.read();
    if (marker == -1) {
      throw new EOFException();
    }
    if ((byte) marker != XTalkTransporter.DOCUMENT_MARKER) {
      throw new IOException("Expected document marker: " + (char) marker);
    }
    int version = is.read();
    if ((byte) version != XTalkTransporter.VERSION_CODE) {
      throw new IOException("Xtalk version code doesn't match "
              + (int) XTalkTransporter.VERSION_CODE + ": " + version);
    }
    handler.startDocument();
    doTopLevelParse();
    handler.endDocument();
  } finally {
    // nullify refs to allow GC
    is = null;
    handler = null;
  }
}
 
Example #3
Source Project: freehealth-connector   Author: taktik   File: ForkContentHandler.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
   ContentHandler[] arr$ = this.handlers;
   int len$ = arr$.length;

   for(int i$ = 0; i$ < len$; ++i$) {
      ContentHandler handler = arr$[i$];
      handler.ignorableWhitespace(ch, start, length);
   }

}
 
Example #4
Source Project: big-c   Author: yncxcw   File: FSEditLogOp.java    License: 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, "SRC", src);
  XMLUtils.addSaxString(contentHandler, "DST", dst);
  XMLUtils.addSaxString(contentHandler, "TIMESTAMP",
      Long.toString(timestamp));
  appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId);
}
 
Example #5
Source Project: morf   Author: alfasoftware   File: XmlDataSetConsumer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param outputStream The output
 * @return A content handler
 * @throws IOException When there's an XML error
 */
private ContentHandler createContentHandler(OutputStream outputStream) throws IOException {
  Properties outputProperties = OutputPropertiesFactory.getDefaultMethodProperties(Method.XML);
  outputProperties.setProperty("indent", "yes");
  outputProperties.setProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "2");
  outputProperties.setProperty(OutputPropertiesFactory.S_KEY_LINE_SEPARATOR, "\n");
  Serializer serializer = SerializerFactory.getSerializer(outputProperties);
  serializer.setOutputStream(outputStream);
  return serializer.asContentHandler();
}
 
Example #6
Source Project: freehealth-connector   Author: taktik   File: ForkContentHandler.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public void endDocument() throws SAXException {
   ContentHandler[] arr$ = this.handlers;
   int len$ = arr$.length;

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

}
 
Example #7
Source Project: openjdk-8   Author: bpupadhyaya   File: Bridge.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @since 2.0.2
 */
public final void marshal(T object, ContentHandler contentHandler, AttachmentMarshaller am) throws JAXBException {
    Marshaller m = context.marshallerPool.take();
    m.setAttachmentMarshaller(am);
    marshal(m,object,contentHandler);
    m.setAttachmentMarshaller(null);
    context.marshallerPool.recycle(m);
}
 
Example #8
Source Project: spring4-understanding   Author: langtianya   File: AbstractStaxXMLReaderTestCase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void contentHandlerNoNamespacesPrefixes() throws Exception {
	standardReader.setFeature("http://xml.org/sax/features/namespaces", false);
	standardReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
	standardReader.parse(new InputSource(createTestInputStream()));

	AbstractStaxXMLReader staxXmlReader = createStaxXmlReader(createTestInputStream());
	ContentHandler contentHandler = mockContentHandler();
	staxXmlReader.setFeature("http://xml.org/sax/features/namespaces", false);
	staxXmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
	staxXmlReader.setContentHandler(contentHandler);
	staxXmlReader.parse(new InputSource());

	verifyIdenticalInvocations(standardContentHandler, contentHandler);
}
 
Example #9
Source Project: ontopia   Author: ontopia   File: OSLSchemaWriter.java    License: Apache License 2.0 5 votes vote down vote up
protected void exportInstanceOf(TypeSpecification spec, ContentHandler dh)
  throws SAXException {
  dh.startElement("", "", "instanceOf", getAttributes(spec.getSubclasses(),
                                              "subclasses", "yes", "no"));
  export(spec.getClassMatcher(), dh);
  dh.endElement("", "", "instanceOf");   
}
 
Example #10
Source Project: hadoop   Author: naver   File: FSEditLogOp.java    License: 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 #11
Source Project: TencentKona-8   Author: Tencent   File: ToXMLSAXHandler.java    License: GNU General Public License v2.0 5 votes vote down vote up
public ToXMLSAXHandler(
    ContentHandler handler,
    LexicalHandler lex,
    String encoding)
{
    super(handler, lex, encoding);

    initCDATA();
    //      initNamespaces();
    m_prefixMap = new NamespaceMappings();
}
 
Example #12
Source Project: ontopia   Author: ontopia   File: OSLSchemaWriter.java    License: Apache License 2.0 5 votes vote down vote up
protected void export(TopicClass klass, ContentHandler dh)
  throws SAXException {
  AttributesImpl atts =
    (AttributesImpl) getAttributes(klass.isStrict(), 
                                      "match", "strict", "loose");
  if (klass.getId() != null)
    atts.addAttribute("", "", "id", "CDATA", klass.getId());
  dh.startElement("", "", "topic", atts);

  exportInstanceOf(klass.getTypeSpecification(), dh);

  // otherClass
  Iterator it = klass.getOtherClasses().iterator();
  while (it.hasNext()) {
    TypeSpecification typespec = (TypeSpecification) it.next();
    dh.startElement("", "", "otherClass", EMPTY_ATTR_LIST);
    export(typespec.getClassMatcher(), dh);
    dh.endElement("", "", "otherClass");
  }
  
  // superclass
  if (klass.getSuperclass() != null) {
    TopicClass superclass = klass.getSuperclass();
    // FIXME: what if no id?
    dh.startElement("", "", "superclass", getAttributes("ref", superclass.getId()));
    dh.endElement("", "", "superclass");
  }
  
  export((TopicConstraintCollection) klass, dh);
  
  dh.endElement("", "", "topic");
}
 
Example #13
Source Project: java-technology-stack   Author: codeEngraver   File: StaxEventXMLReaderTests.java    License: MIT License 5 votes vote down vote up
@Test
public void partial() throws Exception {
	XMLInputFactory inputFactory = XMLInputFactory.newInstance();
	XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(CONTENT));
	eventReader.nextTag();  // skip to root
	StaxEventXMLReader xmlReader = new StaxEventXMLReader(eventReader);
	ContentHandler contentHandler = mock(ContentHandler.class);
	xmlReader.setContentHandler(contentHandler);
	xmlReader.parse(new InputSource());
	verify(contentHandler).startDocument();
	verify(contentHandler).startElement(eq("http://springframework.org/spring-ws"), eq("child"), eq("child"), any(Attributes.class));
	verify(contentHandler).endElement("http://springframework.org/spring-ws", "child", "child");
	verify(contentHandler).endDocument();
}
 
Example #14
Source Project: freehealth-connector   Author: taktik   File: ForkContentHandler.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public void processingInstruction(String target, String data) throws SAXException {
   ContentHandler[] arr$ = this.handlers;
   int len$ = arr$.length;

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

}
 
Example #15
Source Project: jdk8u60   Author: chenghanpeng   File: OldBridge.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @since 2.0.2
 */
public final void marshal(T object, ContentHandler contentHandler, AttachmentMarshaller am) throws JAXBException {
    Marshaller m = context.marshallerPool.take();
    m.setAttachmentMarshaller(am);
    marshal(m,object,contentHandler);
    m.setAttachmentMarshaller(null);
    context.marshallerPool.recycle(m);
}
 
Example #16
Source Project: ontopia   Author: ontopia   File: XTMFragmentExporter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * PUBLIC: Exports a set of topics without any wrapping element.
 */
public void exportTopics(Iterator it, ContentHandler dh) throws SAXException {
  while (it.hasNext()) {
    TopicIF topic = (TopicIF) it.next();
    if (filterOk(topic))
      writeTopic(topic, dh);
  }
}
 
Example #17
Source Project: uima-uimaj   Author: apache   File: CpeCasProcessorsImpl.java    License: 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 #18
Source Project: TencentKona-8   Author: Tencent   File: SAAJMessage.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void endPrefixMapping(ContentHandler contentHandler, NamedNodeMap attrs, String excludePrefix) throws SAXException {
    if(attrs == null)
        return;
    for(int i=0; i < attrs.getLength();i++) {
        Attr a = (Attr)attrs.item(i);
        //check if attr is ns declaration
        if("xmlns".equals(a.getPrefix()) || "xmlns".equals(a.getLocalName())) {
            if(!fixNull(a.getPrefix()).equals(excludePrefix)) {
                contentHandler.endPrefixMapping(fixNull(a.getPrefix()));
            }
        }
    }
}
 
Example #19
Source Project: freehealth-connector   Author: taktik   File: ForkContentHandler.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public void startPrefixMapping(String prefix, String uri) throws SAXException {
   ContentHandler[] arr$ = this.handlers;
   int len$ = arr$.length;

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

}
 
Example #20
Source Project: big-c   Author: yncxcw   File: FSEditLogOp.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void toXml(ContentHandler contentHandler) throws SAXException {
  XMLUtils.addSaxString(contentHandler, "SNAPSHOTROOT", snapshotRoot);
  XMLUtils.addSaxString(contentHandler, "SNAPSHOTOLDNAME", snapshotOldName);
  XMLUtils.addSaxString(contentHandler, "SNAPSHOTNEWNAME", snapshotNewName);
  appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId);
}
 
Example #21
Source Project: TencentKona-8   Author: Tencent   File: StAXEvent2SAX.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void setContentHandler(ContentHandler handler) throws
    NullPointerException
{
    _sax = handler;
    if (handler instanceof LexicalHandler) {
        _lex = (LexicalHandler) handler;
    }

    if (handler instanceof SAXImpl) {
        _saxImpl = (SAXImpl)handler;
    }
}
 
Example #22
Source Project: xmlunit   Author: xmlunit   File: HTMLDocumentBuilder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Perform Swing-HTML-parse-event-to-Sax-event conversion
 */
public void parse(Reader reader, ContentHandler saxContentHandler)
    throws SAXException, IOException {
    this.saxContentHandler = saxContentHandler;
    preParse();
    delegator.parse(reader, this, IGNORE_HTML_CHAR_SET);
    postParse();
}
 
Example #23
Source Project: tika-server   Author: LexPredict   File: TikaTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Basic text extraction.
 * <p>
 * Tries to close input stream after processing.
 */
public String getText(InputStream is, Parser parser, ParseContext context, Metadata metadata) throws Exception{
    ContentHandler handler = new BodyContentHandler(1000000);
    try {
        parser.parse(is, handler, metadata, context);
    } finally {
        is.close();
    }
    return handler.toString();
}
 
Example #24
Source Project: openjdk-8-source   Author: keerath   File: NamespaceMappings.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Pop, or undeclare all namespace definitions that are currently
 * declared at the given element depth, or deepter.
 * @param elemDepth the element depth for which mappings declared at this
 * depth or deeper will no longer be valid
 * @param saxHandler The ContentHandler to notify of any endPrefixMapping()
 * calls.  This parameter can be null.
 */
void popNamespaces(int elemDepth, ContentHandler saxHandler)
{
    while (true)
    {
        if (m_nodeStack.isEmpty())
            return;
        MappingRecord map = (MappingRecord)(m_nodeStack.peek());
        int depth = map.m_declarationDepth;
        if (depth < elemDepth)
            return;
        /* the depth of the declared mapping is elemDepth or deeper
         * so get rid of it
         */

        map = (MappingRecord) m_nodeStack.pop();
        final String prefix = map.m_prefix;
        popNamespace(prefix);
        if (saxHandler != null)
        {
            try
            {
                saxHandler.endPrefixMapping(prefix);
            }
            catch (SAXException e)
            {
                // not much we can do if they aren't willing to listen
            }
        }

    }
}
 
Example #25
Source Project: openjdk-8   Author: bpupadhyaya   File: DOM2SAX.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void setContentHandler(ContentHandler handler) throws
    NullPointerException
{
    _sax = handler;
    if (handler instanceof LexicalHandler) {
        _lex = (LexicalHandler) handler;
    }

    if (handler instanceof SAXImpl) {
        _saxImpl = (SAXImpl)handler;
    }
}
 
Example #26
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: DOMForestScanner.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates the whole set of SAX events by treating
 * element e as if it's a root element.
 */
public void scan( Element e, ContentHandler contentHandler ) throws SAXException {
    DOMScanner scanner = new DOMScanner();

    // insert the location resolver into the pipe line
    LocationResolver resolver = new LocationResolver(scanner);
    resolver.setContentHandler(contentHandler);

    // parse this DOM.
    scanner.setContentHandler(resolver);
    scanner.scan(e);
}
 
Example #27
Source Project: azeroth   Author: warlock-china   File: XLSX2CSV.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Parses and shows the content of one sheet using the specified styles and
 * shared-strings tables.
 *
 * @param styles
 * @param strings
 * @param sheetInputStream
 */
public void processSheet(StylesTable styles, ReadOnlySharedStringsTable strings, SheetContentsHandler sheetHandler,
                         InputStream sheetInputStream) throws IOException, ParserConfigurationException, SAXException {
    DataFormatter formatter = new DataFormatter();
    InputSource sheetSource = new InputSource(sheetInputStream);
    try {
        XMLReader sheetParser = SAXHelper.newXMLReader();
        ContentHandler handler = new XSSFSheetXMLHandler(styles, null, strings, sheetHandler, formatter, false);
        sheetParser.setContentHandler(handler);
        sheetParser.parse(sheetSource);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage());
    }
}
 
Example #28
Source Project: iaf   Author: ibissource   File: XmlUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static XMLReader getXMLReader(Resource classloaderProvider, ContentHandler handler) throws ParserConfigurationException, SAXException {
	XMLReader xmlReader = getXMLReader(true, classloaderProvider);
	xmlReader.setContentHandler(handler);
	if (handler instanceof LexicalHandler) {
		xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
	}
	if (handler instanceof ErrorHandler) {
		xmlReader.setErrorHandler((ErrorHandler)handler);
	}
	return xmlReader;
}
 
Example #29
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: TransformerTest.java    License: GNU General Public License v2.0 4 votes vote down vote up
public void setContentHandler(final ContentHandler handler) {
    contentHandler = handler;
}
 
Example #30
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: SaxSerializer.java    License: GNU General Public License v2.0 4 votes vote down vote up
public SaxSerializer(ContentHandler handler) {
    this(handler,null,true);
}