org.xml.sax.ContentHandler Java Examples

The following examples show how to use org.xml.sax.ContentHandler. 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: OSLSchemaWriter.java    From ontopia with 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 File: XTalkToSAX.java    From uima-uimaj with 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 File: TikaTest.java    From tika-server with 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 #4
Source File: FSEditLogOp.java    From big-c with 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 #5
Source File: ForkContentHandler.java    From freehealth-connector with 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 #6
Source File: AbstractStaxXMLReaderTestCase.java    From spring4-understanding with 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 #7
Source File: XLSX2CSV.java    From azeroth with 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 #8
Source File: XmlUtils.java    From iaf with 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 #9
Source File: Bridge.java    From openjdk-8 with 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 #10
Source File: ForkContentHandler.java    From freehealth-connector with 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 #11
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 #12
Source File: XmlDataSetConsumer.java    From morf with 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 #13
Source File: FSEditLogOp.java    From big-c 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, "SRC", src);
  XMLUtils.addSaxString(contentHandler, "DST", dst);
  XMLUtils.addSaxString(contentHandler, "TIMESTAMP",
      Long.toString(timestamp));
  appendRpcIdsToXml(contentHandler, rpcClientId, rpcCallId);
}
 
Example #14
Source File: SAAJMessage.java    From TencentKona-8 with 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 #15
Source File: StAXEvent2SAX.java    From TencentKona-8 with 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 #16
Source File: OSLSchemaWriter.java    From ontopia with 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 #17
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 #18
Source File: XTMFragmentExporter.java    From ontopia with 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 #19
Source File: DOMForestScanner.java    From openjdk-jdk8u with 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 #20
Source File: ToXMLSAXHandler.java    From TencentKona-8 with 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 #21
Source File: OSLSchemaWriter.java    From ontopia with 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 #22
Source File: HTMLDocumentBuilder.java    From xmlunit with 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 File: OldBridge.java    From jdk8u60 with 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 #24
Source File: ForkContentHandler.java    From freehealth-connector with 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 #25
Source File: DOM2SAX.java    From openjdk-8 with 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 File: NamespaceMappings.java    From openjdk-8-source with 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 #27
Source File: StaxEventXMLReaderTests.java    From java-technology-stack with 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 #28
Source File: ForkContentHandler.java    From freehealth-connector with 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 #29
Source File: EPRHeader.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void writeTo(ContentHandler contentHandler, ErrorHandler errorHandler) throws SAXException {
    epr.writeTo(localName,contentHandler,errorHandler,true);
}
 
Example #30
Source File: RepeatedElementBridge.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void marshal(T object, ContentHandler contentHandler, AttachmentMarshaller am) throws JAXBException {
    delegate.marshal(object, contentHandler, am);
}