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

The following examples show how to use org.xml.sax.ContentHandler#characters() . 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: ProblemActionHeader.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void writeTo(ContentHandler h, ErrorHandler errorHandler) throws SAXException {
    String nsUri = getNamespaceURI();
    String ln = getLocalPart();

    h.startPrefixMapping("",nsUri);
    h.startElement(nsUri,ln,ln,EMPTY_ATTS);
    h.startElement(nsUri,actionLocalName,actionLocalName,EMPTY_ATTS);
    h.characters(action.toCharArray(),0,action.length());
    h.endElement(nsUri,actionLocalName,actionLocalName);
    if (soapAction != null) {
        h.startElement(nsUri,soapActionLocalName,soapActionLocalName,EMPTY_ATTS);
        h.characters(soapAction.toCharArray(),0,soapAction.length());
        h.endElement(nsUri,soapActionLocalName,soapActionLocalName);
    }
    h.endElement(nsUri,ln,ln);
}
 
Example 2
Source File: ProblemActionHeader.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public void writeTo(ContentHandler h, ErrorHandler errorHandler) throws SAXException {
    String nsUri = getNamespaceURI();
    String ln = getLocalPart();

    h.startPrefixMapping("",nsUri);
    h.startElement(nsUri,ln,ln,EMPTY_ATTS);
    h.startElement(nsUri,actionLocalName,actionLocalName,EMPTY_ATTS);
    h.characters(action.toCharArray(),0,action.length());
    h.endElement(nsUri,actionLocalName,actionLocalName);
    if (soapAction != null) {
        h.startElement(nsUri,soapActionLocalName,soapActionLocalName,EMPTY_ATTS);
        h.characters(soapAction.toCharArray(),0,soapAction.length());
        h.endElement(nsUri,soapActionLocalName,soapActionLocalName);
    }
    h.endElement(nsUri,ln,ln);
}
 
Example 3
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 4
Source File: TextBuffer.java    From woodstox with Apache License 2.0 6 votes vote down vote up
public void fireSaxCharacterEvents(ContentHandler h)
    throws SAXException
{
    if (mResultArray != null) { // already have single array?
        h.characters(mResultArray, 0, mResultArray.length);
    } else if (mInputStart >= 0) { // sharing input buffer?
        h.characters(mInputBuffer, mInputStart, mInputLen);
    } else {
        if (mSegments != null) {
            for (int i = 0, len = mSegments.size(); i < len; ++i) {
                char[] ch = mSegments.get(i);
                h.characters(ch, 0, ch.length);
            }
        }
        if (mCurrentSize > 0) {
            h.characters(mCurrentSegment, 0, mCurrentSize);
        }
    }
}
 
Example 5
Source File: PdfStripperProcessor.java    From tika-server with Apache License 2.0 6 votes vote down vote up
private static void setContentHandlerCharacters(ContentHandler handler, char[] chars)
        throws SAXException, NoSuchMethodException, InvocationTargetException, IllegalAccessException,
        NoSuchFieldException, IOException {

    advanceSecureContentHandler(handler, chars.length);

    ContentHandler textHandler = getUnderlyingHandler(handler, ToTextContentHandler.class);
    if (textHandler instanceof ToTextContentHandler) {
        writeCharsToTextHandler((ToTextContentHandler)textHandler, chars);
        return;
    }

    ContentHandler matchHandler = getUnderlyingHandler(handler, MatchingContentHandler.class);
    if (matchHandler instanceof MatchingContentHandler) {
        setCharsBypassingMatching(handler, (MatchingContentHandler)matchHandler, chars);
        return;
    }

    handler.characters(chars, 0, chars.length);
}
 
Example 6
Source File: XMLSerializerTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testXml10Error() throws Exception {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  XMLSerializer sax2xml = new XMLSerializer(baos, false);
  ContentHandler ch = sax2xml.getContentHandler();    
  ch.startDocument();
  char[] data = new char[] {32, 33, 5, 34};
  
  ch.startElement("","foo","foo", new AttributesImpl());
  boolean eh = false;
  try {
    ch.characters(data, 0, 4);
  } catch (SAXParseException e) {
    String msg = e.getMessage();
    String expected = "Trying to serialize non-XML 1.0 character: " + "0x5 at offset 2";
    assertEquals(msg.substring(0, expected.length()), expected);
    eh = true;
  }  
  assertTrue(eh);
}
 
Example 7
Source File: ProblemActionHeader.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public void writeTo(ContentHandler h, ErrorHandler errorHandler) throws SAXException {
    String nsUri = getNamespaceURI();
    String ln = getLocalPart();

    h.startPrefixMapping("",nsUri);
    h.startElement(nsUri,ln,ln,EMPTY_ATTS);
    h.startElement(nsUri,actionLocalName,actionLocalName,EMPTY_ATTS);
    h.characters(action.toCharArray(),0,action.length());
    h.endElement(nsUri,actionLocalName,actionLocalName);
    if (soapAction != null) {
        h.startElement(nsUri,soapActionLocalName,soapActionLocalName,EMPTY_ATTS);
        h.characters(soapAction.toCharArray(),0,soapAction.length());
        h.endElement(nsUri,soapActionLocalName,soapActionLocalName);
    }
    h.endElement(nsUri,ln,ln);
}
 
Example 8
Source File: StringHeader.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(ContentHandler h, ErrorHandler errorHandler) throws SAXException {
    String nsUri = name.getNamespaceURI();
    String ln = name.getLocalPart();

    h.startPrefixMapping("",nsUri);
    if(mustUnderstand) {
        AttributesImpl attributes = new AttributesImpl();
        attributes.addAttribute(soapVersion.nsUri,MUST_UNDERSTAND,"S:"+MUST_UNDERSTAND,"CDATA", getMustUnderstandLiteral(soapVersion));
        h.startElement(nsUri,ln,ln,attributes);
    } else {
        h.startElement(nsUri,ln,ln,EMPTY_ATTS);
    }
    h.characters(value.toCharArray(),0,value.length());
    h.endElement(nsUri,ln,ln);
}
 
Example 9
Source File: FaultDetailHeader.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(ContentHandler h, ErrorHandler errorHandler) throws SAXException {
    String nsUri = av.nsUri;
    String ln = av.faultDetailTag.getLocalPart();

    h.startPrefixMapping("",nsUri);
    h.startElement(nsUri,ln,ln,EMPTY_ATTS);
    h.startElement(nsUri,wrapper,wrapper,EMPTY_ATTS);
    h.characters(problemValue.toCharArray(),0,problemValue.length());
    h.endElement(nsUri,wrapper,wrapper);
    h.endElement(nsUri,ln,ln);
}
 
Example 10
Source File: OBRXMLWriter.java    From ant-ivy with Apache License 2.0 5 votes vote down vote up
private static void saxCapabilityProperty(String n, String t, String v, ContentHandler handler)
        throws SAXException {
    AttributesImpl atts = new AttributesImpl();
    addAttr(atts, CapabilityPropertyHandler.NAME, n);
    if (t != null) {
        addAttr(atts, CapabilityPropertyHandler.TYPE, t);
    }
    addAttr(atts, CapabilityPropertyHandler.VALUE, v);
    handler.startElement("", CapabilityPropertyHandler.CAPABILITY_PROPERTY,
        CapabilityPropertyHandler.CAPABILITY_PROPERTY, atts);
    handler.endElement("", CapabilityPropertyHandler.CAPABILITY_PROPERTY,
        CapabilityPropertyHandler.CAPABILITY_PROPERTY);
    handler.characters("\n".toCharArray(), 0, 1);
}
 
Example 11
Source File: XMLUtils.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Add a SAX tag with a string inside.
 *
 * @param contentHandler     the SAX content handler
 * @param tag                the element tag to use  
 * @param val                the string to put inside the tag
 */
public static void addSaxString(ContentHandler contentHandler,
    String tag, String val) throws SAXException {
  contentHandler.startElement("", "", tag, new AttributesImpl());
  char c[] = mangleXmlString(val, false).toCharArray();
  contentHandler.characters(c, 0, c.length);
  contentHandler.endElement("", "", tag);
}
 
Example 12
Source File: FaultDetailHeader.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(ContentHandler h, ErrorHandler errorHandler) throws SAXException {
    String nsUri = av.nsUri;
    String ln = av.faultDetailTag.getLocalPart();

    h.startPrefixMapping("",nsUri);
    h.startElement(nsUri,ln,ln,EMPTY_ATTS);
    h.startElement(nsUri,wrapper,wrapper,EMPTY_ATTS);
    h.characters(problemValue.toCharArray(),0,problemValue.length());
    h.endElement(nsUri,wrapper,wrapper);
    h.endElement(nsUri,ln,ln);
}
 
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 characters(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.characters(ch, start, length);
   }

}
 
Example 14
Source File: XTMTopicMapExporter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
protected void writeVariantName(VariantNameIF variant, ContentHandler dh)
    throws SAXException {

  dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "variantName", EMPTY_ATTR_LIST);
  if (Objects.equals(variant.getDataType(), DataTypes.TYPE_URI)) {
	LocatorIF varloc = variant.getLocator();
	if (varloc != null) {
		String notation = varloc.getNotation();
		if (notation != null && URI.equals(notation)) {
			// Write resourceRef
			atts.clear();
			atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, XLINK_HREF, CDATA, varloc.getExternalForm());
			dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, RESOURCEREF, atts);
			dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, RESOURCEREF);
		} else
			reportInvalidLocator(varloc);
	}
} else {
	// FIXME: what to do about data type?
    atts.clear();
    dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, RESOURCEDATA, atts);
    String value = variant.getValue();
    if (value != null && !value.equals("")) {
      char[] chars = value.toCharArray();
      dh.characters(chars, 0, chars.length);
    }
    dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, RESOURCEDATA);
  }
  dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "variantName");
}
 
Example 15
Source File: FaultDetailHeader.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(ContentHandler h, ErrorHandler errorHandler) throws SAXException {
    String nsUri = av.nsUri;
    String ln = av.faultDetailTag.getLocalPart();

    h.startPrefixMapping("",nsUri);
    h.startElement(nsUri,ln,ln,EMPTY_ATTS);
    h.startElement(nsUri,wrapper,wrapper,EMPTY_ATTS);
    h.characters(problemValue.toCharArray(),0,problemValue.length());
    h.endElement(nsUri,wrapper,wrapper);
    h.endElement(nsUri,ln,ln);
}
 
Example 16
Source File: XSLTEntityHandler.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
/**
 * Serialises the rendered content of the RWiki Object to SAX
 * 
 * @param rwo
 * @param ch
 * @param withBreadCrumb 
 */
public void renderToXML(RWikiObject rwo, final ContentHandler ch, boolean withBreadCrumb, boolean escapeXML)
		throws SAXException, IOException
{

	String renderedPage;
	try
	{
		renderedPage = render(rwo,withBreadCrumb);
	}
	catch (Exception e)
	{
		renderedPage = Messages.getString("XSLTEntityHandler.32") + rwo.getName() + Messages.getString("XSLTEntityHandler.33") + e.getClass() + Messages.getString("XSLTEntityHandler.34") + e.getMessage(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		log.info(renderedPage, e);
	}
	String contentDigest = DigestHtml.digest(renderedPage);
	if (contentDigest.length() > 500)
	{
		contentDigest = contentDigest.substring(0, 500);
	}
	if (renderedPage == null || renderedPage.trim().length() == 0)
	{
		renderedPage = Messages.getString("XSLTEntityHandler.35"); //$NON-NLS-1$
	}
	if (contentDigest == null || contentDigest.trim().length() == 0)
	{
		contentDigest = Messages.getString("XSLTEntityHandler.36"); //$NON-NLS-1$
	}

	String cdataEscapedRendered = renderedPage
			.replaceAll("]]>", "]]>]]&gt;<![CDATA["); //$NON-NLS-1$ //$NON-NLS-2$
	String cdataContentDigest = contentDigest.replaceAll("]]>", "]]>]]&gt;<![CDATA["); //$NON-NLS-1$ //$NON-NLS-2$

       /* http://jira.sakaiproject.org/browse/SAK-13281
        * ensure all page content is escaped or double escaped before it goes into the parser,
        * if this is not done then the parser will unescape html entities during processing
        */		
       renderedPage = "<content><rendered>" + (escapeXML ? StringEscapeUtils.escapeXml11(renderedPage) : renderedPage) //$NON-NLS-1$
			+ "</rendered><rendered-cdata><![CDATA[" + cdataEscapedRendered + "]]></rendered-cdata><contentdigest><![CDATA[" + cdataContentDigest //$NON-NLS-1$ //$NON-NLS-2$
			+ "]]></contentdigest></content>"; //$NON-NLS-1$

	try
	{
		parseToSAX(renderedPage, ch);
	}
	catch (SAXException ex)
	{
		SimpleCoverage.cover("Failed to parse renderedPage from " + rwo.getName()); //$NON-NLS-1$
		Attributes dummyAttributes = new AttributesImpl();
		ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERROR,
				SchemaNames.EL_NSERROR, dummyAttributes);
		ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERRORDESC,
				SchemaNames.EL_NSERRORDESC, dummyAttributes);
		String s = Messages.getString("XSLTEntityHandler.46") //$NON-NLS-1$
				+ ex.getMessage();
		ch.characters(s.toCharArray(), 0, s.length());
		ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERRORDESC,
				SchemaNames.EL_NSERRORDESC);
		ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_RAWCONTENT,
				SchemaNames.EL_NSRAWCONTENT, dummyAttributes);
		ch.characters(renderedPage.toCharArray(), 0, renderedPage.length());
		ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_RAWCONTENT,
				SchemaNames.EL_NSRAWCONTENT);
		ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERROR,
				SchemaNames.EL_NSERROR);

	}

	// SimpleCoverage.cover("Failed to parse ::\n" + renderedPage
	// + "\n:: from ::\n" + rwo.getContent());
	// Attributes dummyAttributes = new AttributesImpl();
	// ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERROR,
	// SchemaNames.EL_NSERROR, dummyAttributes);
	// ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERRORDESC,
	// SchemaNames.EL_NSERRORDESC, dummyAttributes);
	// String s = "The Rendered Content did not parse correctly "
	// + ex.getMessage();
	// ch.characters(s.toCharArray(), 0, s.length());
	// ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERRORDESC,
	// SchemaNames.EL_NSERRORDESC);
	// ch.startElement(SchemaNames.NS_CONTAINER,
	// SchemaNames.EL_RAWCONTENT, SchemaNames.EL_NSRAWCONTENT,
	// dummyAttributes);
	// ch.characters(renderedPage.toCharArray(), 0, renderedPage.length());
	// ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_RAWCONTENT,
	// SchemaNames.EL_NSRAWCONTENT);
	// ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERROR,
	// SchemaNames.EL_NSERROR);

}
 
Example 17
Source File: SAXEventBuffer.java    From citygml4j with Apache License 2.0 4 votes vote down vote up
private void sendCharacters(ContentHandler handler, boolean release) throws SAXException {
	char[] ch = nextCharacters(release);		
	handler.characters(ch, 0, ch.length);
}
 
Example 18
Source File: XTMFragmentExporter.java    From ontopia with Apache License 2.0 4 votes vote down vote up
@Override
protected void writeOccurrences(Collection occurrences, ContentHandler dh)
  throws SAXException {
  Iterator iter = occurrences.iterator();
  while (iter.hasNext()) {
    OccurrenceIF occr = (OccurrenceIF)iter.next();

    TopicIF reifier = occr.getReifier();
    if (reifier != null) {
      reifiers.add(reifier); // make sure this is exported, too
    }

    atts.clear();
    addId(atts, occr);
    dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME,  "occurrence", atts);

    if (occr.getType() != null)
      writeInstanceOf(occr, dh);
    if (occr.getScope().size() > 0)
      writeScope(occr.getScope(), dh);

    // write resourceRef
    LocatorIF occloc = occr.getLocator();
    if (occloc != null) {
      if (locator_handler != null)
        occloc = locator_handler.process(occloc);
      String notation = occloc.getNotation();
      if (notation != null && notation.equals(URI)) {
        atts.clear();
        atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, XLINK_HREF, CDATA, occloc.getAddress());
        dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME,  RESOURCEREF, atts);
        dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME,  RESOURCEREF);
      } else
        reportInvalidLocator(occloc);
    }

    // write resourceData
    else {
      dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME,  "resourceData", EMPTY_ATTR_LIST);
      if (occr.getValue() != null && !occr.getValue().equals("")) {
        char[] chars = occr.getValue().toCharArray();
        dh.characters(chars, 0, chars.length);
      }
      dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME,  "resourceData");
    }
    dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME,  "occurrence");
  }
}
 
Example 19
Source File: OBRXMLWriter.java    From ant-ivy with Apache License 2.0 4 votes vote down vote up
private static void characters(ContentHandler handler, String value) throws SAXException {
    char[] chars = value.toCharArray();
    handler.characters(chars, 0, chars.length);
}
 
Example 20
Source File: JsonXmlReader.java    From iaf with Apache License 2.0 4 votes vote down vote up
private void newLine() throws SAXException {
	ContentHandler ch=getContentHandler();
	ch.characters("\n".toCharArray(), 0, 1);
}