List of usage examples for javax.xml.stream XMLStreamWriter writeEndElement
public void writeEndElement() throws XMLStreamException;
From source file:com.norconex.committer.core.AbstractMappedCommitter.java
@SuppressWarnings("deprecation") @Override/*from w w w . j a v a 2s.com*/ public void saveToXML(Writer out) throws IOException { XMLOutputFactory factory = XMLOutputFactory.newInstance(); try { XMLStreamWriter writer = factory.createXMLStreamWriter(out); writer.writeStartElement("committer"); writer.writeAttribute("class", getClass().getCanonicalName()); if (sourceReferenceField != null) { writer.writeStartElement("sourceReferenceField"); writer.writeAttribute("keep", Boolean.toString(keepSourceReferenceField)); writer.writeCharacters(sourceReferenceField); writer.writeEndElement(); } if (targetReferenceField != null) { writer.writeStartElement("targetReferenceField"); writer.writeCharacters(targetReferenceField); writer.writeEndElement(); } if (sourceContentField != null) { writer.writeStartElement("sourceContentField"); writer.writeAttribute("keep", Boolean.toString(keepSourceContentField)); writer.writeCharacters(sourceContentField); writer.writeEndElement(); } if (targetContentField != null) { writer.writeStartElement("targetContentField"); writer.writeCharacters(targetContentField); writer.writeEndElement(); } if (getQueueDir() != null) { writer.writeStartElement("queueDir"); writer.writeCharacters(getQueueDir()); writer.writeEndElement(); } writer.writeStartElement("queueSize"); writer.writeCharacters(ObjectUtils.toString(getQueueSize())); writer.writeEndElement(); writer.writeStartElement("commitBatchSize"); writer.writeCharacters(ObjectUtils.toString(getCommitBatchSize())); writer.writeEndElement(); writer.writeStartElement("maxRetries"); writer.writeCharacters(ObjectUtils.toString(getMaxRetries())); writer.writeEndElement(); writer.writeStartElement("maxRetryWait"); writer.writeCharacters(ObjectUtils.toString(getMaxRetryWait())); writer.writeEndElement(); saveToXML(writer); writer.writeEndElement(); writer.flush(); writer.close(); } catch (XMLStreamException e) { throw new IOException("Cannot save as XML.", e); } }
From source file:com.amalto.core.load.io.XMLStreamUnwrapper.java
/** * Moves to next record in stream and stores it in {@link #stringWriter}. *//* w w w .java 2 s . co m*/ private void moveToNext() { try { XMLStreamWriter writer = xmlOutputFactory.createXMLStreamWriter(stringWriter); boolean hasMadeChanges; do { if (!reader.hasNext()) { break; } hasMadeChanges = false; // Keep a state to skip line feeds final XMLEvent event = reader.nextEvent(); if (event.isEndElement()) { level--; } else if (event.isStartElement()) { level++; } else if (event.isEndDocument()) { level--; } if (level >= RECORD_LEVEL) { if (event.isEndElement()) { writer.writeEndElement(); hasMadeChanges = true; } else if (event.isStartElement()) { final StartElement startElement = event.asStartElement(); final QName name = startElement.getName(); writer.writeStartElement(name.getNamespaceURI(), name.getLocalPart()); boolean isRecordRootElement = (RECORD_LEVEL == level - 1); if (isRecordRootElement) { for (int i = 0; i < rootNamespaceList.size(); i++) { Namespace namespace = rootNamespaceList.get(i); writer.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI()); } } // Declare namespaces (if any) final Iterator elementNamespaces = startElement.getNamespaces(); while (elementNamespaces.hasNext()) { Namespace elementNamespace = (Namespace) elementNamespaces.next(); if (isRecordRootElement) { if (rootNamespaceList.size() > 0) { for (int i = 0; i < rootNamespaceList.size(); i++) { Namespace namespace = rootNamespaceList.get(i); if (!namespace.getPrefix().equals(elementNamespace.getPrefix()) || !namespace.getNamespaceURI() .equals(elementNamespace.getNamespaceURI())) { writer.writeNamespace(elementNamespace.getPrefix(), elementNamespace.getNamespaceURI()); } } } else { writer.writeNamespace(elementNamespace.getPrefix(), elementNamespace.getNamespaceURI()); } } else { writer.writeNamespace(elementNamespace.getPrefix(), elementNamespace.getNamespaceURI()); } } // Write attributes final Iterator attributes = startElement.getAttributes(); while (attributes.hasNext()) { Attribute attribute = (Attribute) attributes.next(); QName attributeName = attribute.getName(); String value = attribute.getValue(); if (StringUtils.isEmpty(attributeName.getNamespaceURI())) { writer.writeAttribute(attributeName.getLocalPart(), value); } else { writer.writeAttribute(attributeName.getNamespaceURI(), attributeName.getLocalPart(), value); } } hasMadeChanges = true; } else if (event.isCharacters()) { final String text = event.asCharacters().getData().trim(); if (!text.isEmpty()) { writer.writeCharacters(text); hasMadeChanges = true; } } } } while (level > RECORD_LEVEL || !hasMadeChanges); writer.flush(); } catch (XMLStreamException e) { throw new RuntimeException("Unexpected parsing exception.", e); } }
From source file:edu.harvard.hms.dbmi.bd2k.irct.ws.rs.resultconverter.XMLTabularDataConverter.java
@Override public StreamingOutput createStream(final Result result) { StreamingOutput stream = new StreamingOutput() { @Override/* www . ja v a2s . c o m*/ public void write(OutputStream outputStream) throws IOException, WebApplicationException { ResultSet rs = null; XMLStreamWriter xtw = null; try { rs = (ResultSet) result.getData(); rs.load(result.getResultSetLocation()); XMLOutputFactory xof = XMLOutputFactory.newInstance(); xtw = xof.createXMLStreamWriter(new OutputStreamWriter(outputStream)); xtw.writeStartDocument("utf-8", "1.0"); xtw.writeStartElement("results"); rs.beforeFirst(); while (rs.next()) { xtw.writeStartElement("result"); for (int i = 0; i < rs.getColumnSize(); i++) { xtw.writeStartElement(rs.getColumn(i).getName().replace(" ", "_")); xtw.writeCharacters(rs.getString(i)); xtw.writeEndElement(); } xtw.writeEndElement(); } xtw.writeEndElement(); xtw.writeEndDocument(); xtw.flush(); } catch (ResultSetException | PersistableException | XMLStreamException e) { log.info("Error creating XML Stream: " + e.getMessage()); } finally { if (xtw != null) { try { xtw.close(); } catch (XMLStreamException e) { e.printStackTrace(); } } if (rs != null && !rs.isClosed()) { try { rs.close(); } catch (ResultSetException e) { e.printStackTrace(); } } if (outputStream != null) { outputStream.close(); } } } }; return stream; }
From source file:com.flexive.shared.media.FxMetadata.java
/** * Get this metadata object as XML document * * @return XML document//from www .jav a 2 s.c o m * @throws FxApplicationException on errors */ public String toXML() throws FxApplicationException { StringWriter sw = new StringWriter(2000); try { XMLStreamWriter writer = getXmlOutputFactory().createXMLStreamWriter(sw); writer.writeStartDocument(); writer.writeStartElement("metadata"); writer.writeAttribute("mediatype", getMediaType().name()); writer.writeAttribute("mimetype", getMimeType()); writer.writeAttribute("filename", getFilename()); writeXMLTags(writer); for (FxMetadataItem mdi : getMetadata()) { final String value = mdi.getValue().replaceAll("[\\x00-\\x1F]", ""); //filter out control characters if (StringUtils.isEmpty(value)) continue; writer.writeStartElement("meta"); writer.writeAttribute("key", mdi.getKey()); writer.writeCData(value); writer.writeEndElement(); } writer.writeEndElement(); writer.writeEndDocument(); writer.flush(); writer.close(); } catch (XMLStreamException e) { throw new FxApplicationException(e, "ex.general.xml", e.getMessage()); } return sw.getBuffer().toString(); }
From source file:net.solarnetwork.web.support.SimpleXmlHttpMessageConverter.java
private void outputMap(Map<?, ?> map, String name, XMLStreamWriter writer) throws IOException, XMLStreamException { writeElement(name, null, writer, false); // for each entry, write an <entry> element for (Map.Entry<?, ?> me : map.entrySet()) { String entryName = me.getKey().toString(); writer.writeStartElement("entry"); writer.writeAttribute("key", entryName); Object value = me.getValue(); if (value instanceof Collection) { // special collection case, we don't add nested element for (Object o : (Collection<?>) value) { outputObject(o, "value", writer); }/* w w w. j ava 2s.co m*/ } else { outputObject(value, null, writer); } writer.writeEndElement(); } writer.writeEndElement(); }
From source file:org.deegree.services.ows.OWS110ExceptionReportSerializer.java
@Override public void serializeExceptionToXML(XMLStreamWriter writer, OWSException ex) throws XMLStreamException { if (ex == null || writer == null) { return;//from www.j av a 2 s. co m } writer.writeStartElement("ows", "ExceptionReport", OWS_NS); writer.writeNamespace("ows", OWS_NS); writer.writeNamespace("xsi", XSINS); writer.writeAttribute(XSINS, "schemaLocation", OWS_NS + " " + OWS_SCHEMA); writer.writeAttribute("version", "" + version); writer.writeStartElement(OWS_NS, "Exception"); writer.writeAttribute("exceptionCode", ex.getExceptionCode()); if (ex.getLocator() != null && !"".equals(ex.getLocator().trim())) { writer.writeAttribute("locator", ex.getLocator()); } String message = ex.getMessage(); if (message != null) { writer.writeStartElement(OWS_NS, "ExceptionText"); writer.writeCharacters(message); writer.writeEndElement(); } writer.writeEndElement(); // Exception writer.writeEndElement(); // ExceptionReport }
From source file:eionet.cr.util.odp.ODPDatasetsPacker.java
/** * * @param writer//from w ww.jav a 2 s .c o m * @throws XMLStreamException */ private void writeManifestFooter(XMLStreamWriter writer) throws XMLStreamException { // Close root element tag writer.writeEndElement(); }
From source file:Main.java
/** * Borrowed from org.apache.xml.security.test.stax.utils.XmlReaderToWriter *//*w w w. j a v a 2 s . c om*/ public static void write(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException { switch (xmlr.getEventType()) { case XMLEvent.START_ELEMENT: final String localName = xmlr.getLocalName(); final String namespaceURI = xmlr.getNamespaceURI(); if (namespaceURI != null && namespaceURI.length() > 0) { final String prefix = xmlr.getPrefix(); if (prefix != null) { writer.writeStartElement(prefix, localName, namespaceURI); } else { writer.writeStartElement(namespaceURI, localName); } } else { writer.writeStartElement(localName); } for (int i = 0, len = xmlr.getNamespaceCount(); i < len; i++) { String prefix = xmlr.getNamespacePrefix(i); if (prefix == null) { writer.writeDefaultNamespace(xmlr.getNamespaceURI(i)); } else { writer.writeNamespace(prefix, xmlr.getNamespaceURI(i)); } } for (int i = 0, len = xmlr.getAttributeCount(); i < len; i++) { final String attUri = xmlr.getAttributeNamespace(i); if (attUri != null && attUri.length() > 0) { final String prefix = xmlr.getAttributePrefix(i); if (prefix != null) { writer.writeAttribute(prefix, attUri, xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i)); } else { writer.writeAttribute(attUri, xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i)); } } else { writer.writeAttribute(xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i)); } } break; case XMLEvent.END_ELEMENT: writer.writeEndElement(); break; case XMLEvent.SPACE: case XMLEvent.CHARACTERS: char[] text = new char[xmlr.getTextLength()]; xmlr.getTextCharacters(0, text, 0, xmlr.getTextLength()); writer.writeCharacters(text, 0, text.length); break; case XMLEvent.PROCESSING_INSTRUCTION: writer.writeProcessingInstruction(xmlr.getPITarget(), xmlr.getPIData()); break; case XMLEvent.CDATA: writer.writeCData(xmlr.getText()); break; case XMLEvent.COMMENT: writer.writeComment(xmlr.getText()); break; case XMLEvent.ENTITY_REFERENCE: writer.writeEntityRef(xmlr.getLocalName()); break; case XMLEvent.START_DOCUMENT: String encoding = xmlr.getCharacterEncodingScheme(); String version = xmlr.getVersion(); if (encoding != null && version != null) { writer.writeStartDocument(encoding, version); } else if (version != null) { writer.writeStartDocument(xmlr.getVersion()); } break; case XMLEvent.END_DOCUMENT: writer.writeEndDocument(); break; case XMLEvent.DTD: writer.writeDTD(xmlr.getText()); break; } }
From source file:com.norconex.collector.http.client.impl.GenericHttpClientFactory.java
private void writeStringElement(XMLStreamWriter writer, String name, String value) throws XMLStreamException { writer.writeStartElement(name);// w w w . j a va2 s . c o m writer.writeCharacters(value); writer.writeEndElement(); }
From source file:net.solarnetwork.util.JavaBeanXmlSerializer.java
/** * Render a Map as XML serialized to a given OutputStream. * /*from w w w .j ava2 s.c o m*/ * @param model * the data to serialize as XML * @param out * the OutputStream to write the XML to */ public void renderMap(Map<String, ?> model, OutputStream out) { Map<String, Object> finalModel = setupDateFormat(model); XMLStreamWriter writer = startXml(out); try { Object singleBean = finalModel.size() == 1 && this.singleBeanAsRoot ? finalModel.values().iterator().next() : null; if (singleBean != null) { outputObject(singleBean, finalModel.keySet().iterator().next().toString(), writer); } else { writeElement(this.rootElementName, null, writer, false); for (Map.Entry<String, Object> me : finalModel.entrySet()) { outputObject(me.getValue(), me.getKey(), writer); } // end root element writer.writeEndElement(); } } catch (XMLStreamException e) { throw new RuntimeException(e); } finally { endXml(writer); } }