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

The following examples show how to use org.xml.sax.ContentHandler#startElement() . 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: XTM2TopicMapExporter.java    From ontopia with Apache License 2.0 6 votes vote down vote up
private void write(TopicNameIF bn, ContentHandler dh) throws SAXException {
  atts.clear();
  addReifier(atts, bn);
  dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "name", atts);
  writeReifier(bn, dh);
  writeItemIdentities(bn, dh);
  writeType(bn, dh);
  writeScope(bn, dh);
  
  dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "value", EMPTY_ATTR_LIST);
  write(bn.getValue(), dh);
  dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "value");

  Iterator it = filterCollection(bn.getVariants()).iterator();
  while (it.hasNext()) {
    VariantNameIF vn = (VariantNameIF) it.next();
    write(vn, dh);
  }
  
  dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "name");
}
 
Example 2
Source File: ProblemActionHeader.java    From openjdk-jdk8u-backup 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: XMLSerializerTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testXml11Error() throws Exception {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  XMLSerializer sax2xml = new XMLSerializer(baos, false);
  sax2xml.setOutputProperty(OutputKeys.VERSION, "1.1");
  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) {
    eh = true;
  }  
  assertFalse(eh);
}
 
Example 4
Source File: CanonicalTopicMapWriter.java    From ontopia with Apache License 2.0 6 votes vote down vote up
private void writeAssociation(AssociationIF assoc, ContentHandler dh,
                              ContextHolder context) throws SAXException {
  dh.startElement("", "", "association", empty);
  if (assoc.getType() != null)
    writeInstanceOf(assoc.getType(), dh, context);
  writeScope(assoc, dh, context);

  Iterator<AssociationRoleIF> it = context.rolesInOrder(assoc.getRoles());
  while (it.hasNext()) {
    AssociationRoleIF role = it.next();
    dh.startElement("", "", "member", empty);
    if (role.getType() != null)
      writeInstanceOf(role.getType(), dh, context);
    if (role.getPlayer() != null)
      writeTopicRef(role.getPlayer(), dh, context);
    dh.endElement("", "", "member");
  }
      
  dh.endElement("", "", "association");
}
 
Example 5
Source File: EDIReader.java    From edireader with GNU General Public License v3.0 5 votes vote down vote up
protected void startXMLDocument() throws SAXException {
    AttributesImpl attrList = new AttributesImpl();
    attrList.clear();
    final ContentHandler contentHandler = getContentHandler();
    if (contentHandler == null) {
        throw new SAXException("No ContentHandler configured for EDIReader");
    }
    contentHandler.startDocument();
    String rootTag = getXMLTags().getRootTag();
    if (isNamespaceEnabled()) {
        contentHandler.startElement(BERRYWORKS_NAMESPACE, rootTag, rootTag, attrList);
    } else {
        startElement(rootTag, attrList);
    }
}
 
Example 6
Source File: StringHeader.java    From openjdk-jdk8u 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 7
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 8
Source File: StringHeader.java    From openjdk-8-source 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: AbstractReportlet.java    From syncope with Apache License 2.0 5 votes vote down vote up
@Override
@Transactional(readOnly = true)
public void extract(final ContentHandler handler, final AtomicReference<String> status) throws SAXException {
    AttributesImpl atts = new AttributesImpl();
    atts.addAttribute("", "", ReportXMLConst.ATTR_NAME, ReportXMLConst.XSD_STRING, conf.getName());
    atts.addAttribute("", "", ReportXMLConst.ATTR_CLASS, ReportXMLConst.XSD_STRING, getClass().getName());
    handler.startElement("", "", ReportXMLConst.ELEMENT_REPORTLET, atts);

    doExtract(conf, handler, status);

    handler.endElement("", "", ReportXMLConst.ELEMENT_REPORTLET);
}
 
Example 10
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 11
Source File: JsonXmlReader.java    From iaf with Apache License 2.0 5 votes vote down vote up
private void startElement(String typename, String key, String attrName, String attrValue) throws SAXException {
	ContentHandler ch=getContentHandler();
	AttributesImpl attr=new AttributesImpl(); // Saxon requires attr to be not null
	if (key!=null) {
		addAttribute(attr, "key", key);
	}
	if (attrName!=null) {
		addAttribute(attr, attrName, attrValue);
	}
	newLine();
	ch.startElement(TARGETNAMESPACE, typename, typename, attr);
	elementEnded=false;
}
 
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: 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 14
Source File: CasProcessorExecutableImpl.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 < envs.size(); i++) {
    ((CasProcessorRuntimeEnvParam) envs.get(i)).toXML(aContentHandler,
            aWriteDefaultNamespaceAttribute);
  }
  CasProcessorExecArg[] argList = args.getAll();
  // write child elements
  for (int i = 0; i < argList.length; i++) {
    argList[i].toXML(aContentHandler, aWriteDefaultNamespaceAttribute);
  }

  // end element
  aContentHandler.endElement(inf.namespace, inf.elementTagName, inf.elementTagName);
}
 
Example 15
Source File: OBRXMLWriter.java    From ant-ivy with Apache License 2.0 5 votes vote down vote up
public static void writeManifests(Iterable<ManifestAndLocation> manifestAndLocations,
        ContentHandler handler, boolean quiet) throws SAXException {
    int level = quiet ? Message.MSG_DEBUG : Message.MSG_WARN;
    handler.startDocument();
    AttributesImpl atts = new AttributesImpl();
    handler.startElement("", RepositoryHandler.REPOSITORY, RepositoryHandler.REPOSITORY, atts);
    int nbOk = 0;
    int nbRejected = 0;
    for (ManifestAndLocation manifestAndLocation : manifestAndLocations) {
        BundleInfo bundleInfo;
        try {
            bundleInfo = ManifestParser.parseManifest(manifestAndLocation.getManifest());
            bundleInfo
                    .addArtifact(new BundleArtifact(false, manifestAndLocation.getUri(), null));
            if (manifestAndLocation.getSourceURI() != null) {
                bundleInfo.addArtifact(new BundleArtifact(true, manifestAndLocation
                        .getSourceURI(), null));
            }
            nbOk++;
        } catch (ParseException e) {
            nbRejected++;
            IvyContext
                    .getContext()
                    .getMessageLogger()
                    .log("Rejected " + manifestAndLocation.getUri() + ": " + e.getMessage(),
                        level);
            continue;
        }
        saxBundleInfo(bundleInfo, handler);
    }
    handler.endElement("", RepositoryHandler.REPOSITORY, RepositoryHandler.REPOSITORY);
    handler.endDocument();
    Message.info(nbOk + " bundle" + (nbOk > 1 ? "s" : "") + " added, " + nbRejected + " bundle"
            + (nbRejected > 1 ? "s" : "") + " rejected.");
}
 
Example 16
Source File: XSLTEntityHandler.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void addRequestParameters(ContentHandler ch, HttpServletRequest request)
		throws Exception
{
	if (!isAvailable()) return;

	AttributesImpl dummyAttributes = new AttributesImpl();

	// add the request parameters
	ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_PARAMS,
			SchemaNames.EL_NSREQUEST_PARAMS, dummyAttributes);
	for (Enumeration e = request.getParameterNames(); e.hasMoreElements();)
	{
		String name = (String) e.nextElement();
		String[] attr = request.getParameterValues(name);
		AttributesImpl propA = new AttributesImpl();
		propA.addAttribute("", SchemaNames.ATTR_NAME, //$NON-NLS-1$
				SchemaNames.ATTR_NAME, "string", name); //$NON-NLS-1$
		ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_PARAM,
				SchemaNames.EL_NSREQUEST_PARAM, propA);
		for (int i = 0; i < attr.length; i++)
		{
			addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_VALUE,
					SchemaNames.EL_NSVALUE, dummyAttributes, attr[i]);
		}
		ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_PARAM,
				SchemaNames.EL_NSREQUEST_PARAM);
	}

	ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_PARAMS,
			SchemaNames.EL_REQUEST_PARAMS);
}
 
Example 17
Source File: OSLSchemaWriter.java    From ontopia with Apache License 2.0 4 votes vote down vote up
protected void emptyElement(ContentHandler dh, String elem,
                            Attributes atts) throws SAXException {
  dh.startElement("", "", elem, atts);
  dh.endElement("", "", elem);
}
 
Example 18
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 19
Source File: OSLSchemaWriter.java    From ontopia with Apache License 2.0 4 votes vote down vote up
protected void export(RuleSet ruleset, ContentHandler dh)
  throws SAXException {
  dh.startElement("", "", "ruleset", getAttributes("id", ruleset.getId()));
  export((TopicConstraintCollection) ruleset, dh);
  dh.endElement("", "", "ruleset");
}
 
Example 20
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++;
}