List of usage examples for javax.xml.stream XMLStreamWriter writeAttribute
public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException;
From source file:org.apache.olingo.commons.core.data.AtomSerializer.java
private void feed(final XMLStreamWriter writer, final Feed feed) throws XMLStreamException { if (feed.getBaseURI() != null) { writer.writeAttribute(XMLConstants.XML_NS_URI, Constants.ATTR_XML_BASE, feed.getBaseURI().toASCIIString()); }//w ww . j a v a2s. co m if (feed.getCount() != null) { writer.writeStartElement(version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA), Constants.ATOM_ELEM_COUNT); writer.writeCharacters(Integer.toString(feed.getCount())); writer.writeEndElement(); } if (StringUtils.isNotBlank(feed.getId())) { writer.writeStartElement(Constants.ATOM_ELEM_ID); writer.writeCharacters(feed.getId()); writer.writeEndElement(); } if (feed instanceof AbstractAtomObject) { common(writer, (AbstractAtomObject) feed); } for (Entry entry : feed.getEntries()) { writer.writeStartElement(Constants.ATOM_ELEM_ENTRY); entry(writer, entry); writer.writeEndElement(); } if (feed.getNext() != null) { final LinkImpl next = new LinkImpl(); next.setRel(Constants.NEXT_LINK_REL); next.setHref(feed.getNext().toASCIIString()); links(writer, Collections.<Link>singletonList(next)); } }
From source file:org.apache.olingo.commons.core.serialization.AtomSerializer.java
private void entity(final XMLStreamWriter writer, final Entity entity) throws XMLStreamException, EdmPrimitiveTypeException { if (entity.getBaseURI() != null) { writer.writeAttribute(XMLConstants.XML_NS_URI, Constants.ATTR_XML_BASE, entity.getBaseURI().toASCIIString()); }//ww w .j av a 2s . c om if (serverMode && StringUtils.isNotBlank(entity.getETag())) { writer.writeAttribute(namespaceMetadata, Constants.ATOM_ATTR_ETAG, entity.getETag()); } if (entity.getId() != null) { writer.writeStartElement(Constants.ATOM_ELEM_ID); writer.writeCharacters(entity.getId().toASCIIString()); writer.writeEndElement(); } writer.writeStartElement(Constants.ATOM_ELEM_CATEGORY); writer.writeAttribute(Constants.ATOM_ATTR_SCHEME, version.getNamespace(ODataServiceVersion.NamespaceKey.SCHEME)); if (StringUtils.isNotBlank(entity.getType())) { writer.writeAttribute(Constants.ATOM_ATTR_TERM, new EdmTypeInfo.Builder().setTypeExpression(entity.getType()).build().external(version)); } writer.writeEndElement(); if (entity instanceof AbstractODataObject) { common(writer, (AbstractODataObject) entity); } if (serverMode) { if (entity.getEditLink() != null) { links(writer, Collections.singletonList(entity.getEditLink())); } if (entity.getSelfLink() != null) { links(writer, Collections.singletonList(entity.getSelfLink())); } } links(writer, entity.getAssociationLinks()); links(writer, entity.getNavigationLinks()); links(writer, entity.getMediaEditLinks()); if (serverMode) { for (ODataOperation operation : entity.getOperations()) { writer.writeStartElement(namespaceMetadata, Constants.ATOM_ELEM_ACTION); writer.writeAttribute(Constants.ATTR_METADATA, operation.getMetadataAnchor()); writer.writeAttribute(Constants.ATTR_TITLE, operation.getTitle()); writer.writeAttribute(Constants.ATTR_TARGET, operation.getTarget().toASCIIString()); writer.writeEndElement(); } } writer.writeStartElement(Constants.ATOM_ELEM_CONTENT); if (entity.isMediaEntity()) { if (StringUtils.isNotBlank(entity.getMediaContentType())) { writer.writeAttribute(Constants.ATTR_TYPE, entity.getMediaContentType()); } if (entity.getMediaContentSource() != null) { writer.writeAttribute(Constants.ATOM_ATTR_SRC, entity.getMediaContentSource().toASCIIString()); } writer.writeEndElement(); writer.writeStartElement(namespaceMetadata, Constants.PROPERTIES); properties(writer, entity.getProperties()); } else { writer.writeAttribute(Constants.ATTR_TYPE, ContentType.APPLICATION_XML.toContentTypeString()); writer.writeStartElement(namespaceMetadata, Constants.PROPERTIES); properties(writer, entity.getProperties()); writer.writeEndElement(); } writer.writeEndElement(); for (Annotation annotation : entity.getAnnotations()) { annotation(writer, annotation, null); } }
From source file:org.apache.olingo.commons.core.serialization.AtomSerializer.java
private void entitySet(final XMLStreamWriter writer, final EntitySet entitySet) throws XMLStreamException, EdmPrimitiveTypeException { if (entitySet.getBaseURI() != null) { writer.writeAttribute(XMLConstants.XML_NS_URI, Constants.ATTR_XML_BASE, entitySet.getBaseURI().toASCIIString()); }/*from w w w. j a v a2 s. com*/ if (entitySet.getCount() != null) { writer.writeStartElement(namespaceMetadata, Constants.ATOM_ELEM_COUNT); writer.writeCharacters(Integer.toString(entitySet.getCount())); writer.writeEndElement(); } if (entitySet.getId() != null) { writer.writeStartElement(Constants.ATOM_ELEM_ID); writer.writeCharacters(entitySet.getId().toASCIIString()); writer.writeEndElement(); } if (entitySet instanceof AbstractODataObject) { common(writer, (AbstractODataObject) entitySet); } for (Entity entity : entitySet.getEntities()) { if (entity.getType() == null && entity.getProperties().isEmpty()) { entityRef(writer, entity); writer.writeEndElement(); } else { writer.writeStartElement(Constants.ATOM_ELEM_ENTRY); entity(writer, entity); writer.writeEndElement(); } } if (serverMode) { if (entitySet.getNext() != null) { final LinkImpl next = new LinkImpl(); next.setRel(Constants.NEXT_LINK_REL); next.setHref(entitySet.getNext().toASCIIString()); links(writer, Collections.<Link>singletonList(next)); } if (entitySet.getDeltaLink() != null) { final LinkImpl next = new LinkImpl(); next.setRel(ODataServiceVersion.V40.getNamespace(NamespaceKey.DELTA_LINK_REL)); next.setHref(entitySet.getDeltaLink().toASCIIString()); links(writer, Collections.<Link>singletonList(next)); } } }
From source file:org.apache.olingo.commons.core.serialization.AtomSerializer.java
private <T> void addContextInfo(final XMLStreamWriter writer, final ResWrap<T> container) throws XMLStreamException { if (container.getContextURL() != null) { final ContextURL contextURL = ContextURLParser.parse(container.getContextURL()); String base = contextURL.getServiceRoot().toASCIIString(); if (container.getPayload() instanceof EntitySet) { ((EntitySetImpl) container.getPayload()).setBaseURI(base); }//from ww w. j a va 2s . c om if (container.getPayload() instanceof Entity) { ((EntityImpl) container.getPayload()).setBaseURI(base); } writer.writeAttribute(namespaceMetadata, Constants.CONTEXT, container.getContextURL().toASCIIString()); } if (StringUtils.isNotBlank(container.getMetadataETag())) { writer.writeAttribute(namespaceMetadata, Constants.ATOM_ATTR_METADATAETAG, container.getMetadataETag()); } }
From source file:org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer.java
/** * Writes a salt project to the xml stream. * /*from ww w.j av a2s. com*/ * @param xml * A pre-configured {@link XMLStreamWriter} * @param project */ public void writeSaltProject(XMLStreamWriter xml, SaltProject project) { try { xml.writeStartElement(NS_SALTCOMMON, TAG_SALT_PROJECT, NS_VALUE_SALTCOMMON); xml.writeNamespace(NS_SCORPUSSTRUCTURE, NS_VALUE_SCORPUSSTRUCTURE); xml.writeNamespace(NS_XMI, NS_VALUE_XMI); xml.writeNamespace(NS_XSI, NS_VALUE_XSI); xml.writeNamespace(NS_SALTCORE, NS_VALUE_SALTCORE); xml.writeNamespace(NS_SALTCOMMON, NS_VALUE_SALTCOMMON); xml.writeAttribute(NS_VALUE_XMI, ATT_XMI_VERSION, "2.0"); Iterator<SCorpusGraph> cGraphs = project.getCorpusGraphs().iterator(); while (cGraphs.hasNext()) { if (isPrettyPrint) { xml.writeCharacters("\n"); xml.writeCharacters("\t"); } writeCorpusGraph(xml, cGraphs.next(), true); } if (isPrettyPrint) { xml.writeCharacters("\n"); } xml.writeEndElement(); } catch (XMLStreamException e) { throw new SaltResourceException("Cannot store salt project to file '" + getLocationStr() + "'. ", e); } }
From source file:org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer.java
/** * Writes a corpus graph to the xml stream * //from w w w . j a va 2 s. c o m * @param graph * the corpus graph to be written * @param embedded * determines whether this corpus graph is part of a saltProject * @param xml * xml stream to write corpus graph to, if the passed one is * null, a new one will be created */ public void writeCorpusGraph(XMLStreamWriter xml, SCorpusGraph graph, boolean embedded) { try { if (!embedded) { xml.writeStartDocument("1.0"); if (isPrettyPrint) { xml.writeCharacters("\n"); xml.writeStartElement(NS_SALTCOMMON, TAG_SCORPUSGRAPH, NS_VALUE_SALTCOMMON); xml.writeNamespace(NS_SCORPUSSTRUCTURE, NS_VALUE_SCORPUSSTRUCTURE); xml.writeNamespace(NS_XMI, NS_VALUE_XMI); xml.writeNamespace(NS_XSI, NS_VALUE_XSI); xml.writeNamespace(NS_SALTCORE, NS_VALUE_SALTCORE); xml.writeNamespace(NS_SALTCOMMON, NS_VALUE_SALTCOMMON); xml.writeAttribute(NS_VALUE_XMI, ATT_XMI_VERSION, "2.0"); } } else { xml.writeStartElement(TAG_SCORPUSGRAPH); } // write all labels if (graph.getLabels() != null) { Iterator<Label> labelIt = graph.getLabels().iterator(); while (labelIt.hasNext()) { if (isPrettyPrint) { xml.writeCharacters("\n"); xml.writeCharacters("\t"); } writeLabel(xml, labelIt.next()); } } // stores the position of a single node in the list of nodes to // refer them in a relation Map<SNode, Integer> nodePositions = new HashMap<>(); // write all nodes if (graph.getNodes() != null) { Iterator<SNode> nodeIt = graph.getNodes().iterator(); Integer position = 0; while (nodeIt.hasNext()) { SNode node = nodeIt.next(); writeNode(xml, node, null); nodePositions.put(node, position); position++; } } // write all relations if (graph.getRelations() != null) { Iterator<SRelation<SNode, SNode>> relIt = graph.getRelations().iterator(); while (relIt.hasNext()) { SRelation<SNode, SNode> rel = relIt.next(); writeRelation(xml, rel, nodePositions, null); } } if (isPrettyPrint) { xml.writeCharacters("\n"); xml.writeCharacters("\t"); } xml.writeEndElement(); } catch (XMLStreamException e) { throw new SaltResourceException( "Cannot store salt project to file '" + getLocationStr() + "'. " + e.getMessage(), e); } finally { if (!embedded) { if (xml != null) { try { xml.flush(); xml.close(); } catch (XMLStreamException e) { throw new SaltResourceException("Cannot store salt project to file '" + getLocationStr() + "', because the opened stream is not closable. ", e); } } } } }
From source file:org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer.java
/** * Writes a document graph to the xml stream. * /*from ww w .j a v a 2 s . c om*/ * @param xml * A pre-configured {@link XMLStreamWriter} * @param graph */ public void writeDocumentGraph(XMLStreamWriter xml, SDocumentGraph graph) { try { xml.writeStartElement(NS_SDOCUMENTSTRUCTURE, TAG_SDOCUMENTSTRUCTURE_SDOCUMENTGRAPH, NS_VALUE_SDOCUMENTSTRUCTURE); xml.writeNamespace(NS_SDOCUMENTSTRUCTURE, NS_VALUE_SDOCUMENTSTRUCTURE); xml.writeNamespace(NS_XMI, NS_VALUE_XMI); xml.writeNamespace(NS_XSI, NS_VALUE_XSI); xml.writeNamespace(NS_SALTCORE, NS_VALUE_SALTCORE); xml.writeAttribute(NS_VALUE_XMI, ATT_XMI_VERSION, "2.0"); // write all labels Iterator<Label> labelIt = graph.getLabels().iterator(); while (labelIt.hasNext()) { if (isPrettyPrint) { xml.writeCharacters("\n"); xml.writeCharacters("\t"); } writeLabel(xml, labelIt.next()); } // stores the position of a single layer in the list of layers to // refer them in a relation Map<SLayer, Integer> layerPositions = new HashMap<>(); Iterator<SLayer> layerIt = graph.getLayers().iterator(); Integer position = 0; while (layerIt.hasNext()) { layerPositions.put(layerIt.next(), position); position++; } // stores the position of a single node in the list of nodes to // refer them in a relation Map<SNode, Integer> nodePositions = new HashMap<>(); // write all nodes Iterator<SNode> nodeIt = graph.getNodes().iterator(); position = 0; while (nodeIt.hasNext()) { SNode node = nodeIt.next(); writeNode(xml, node, layerPositions); nodePositions.put(node, position); position++; } // stores the position of a single relation in the list of relations // to refer them later Map<SRelation<SNode, SNode>, Integer> relPositions = new HashMap<>(); { // write all relations Iterator<SRelation<SNode, SNode>> relIt = graph.getRelations().iterator(); position = 0; while (relIt.hasNext()) { SRelation<SNode, SNode> rel = relIt.next(); writeRelation(xml, rel, nodePositions, layerPositions); relPositions.put(rel, position); position++; } } // write layers layerIt = graph.getLayers().iterator(); while (layerIt.hasNext()) { writeLayer(xml, layerIt.next(), nodePositions, relPositions); } if (isPrettyPrint) { xml.writeCharacters("\n"); } xml.writeEndElement(); } catch (XMLStreamException e) { throw new SaltResourceException("Cannot store document graph to file '" + getLocationStr() + "'. ", e); } }
From source file:org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer.java
/** * Writes the passed label object to the passed {@link XMLStreamWriter}. * /*from w w w . j a v a2 s. com*/ * @param label * to be persist * @param xml * stream to write data to * @throws XMLStreamException */ public void writeLabel(XMLStreamWriter xml, Label label) throws XMLStreamException { // ignore when label is reference to SDocument or SDocumentGraph if (label != null && (label.getValue() instanceof SDocument || label.getValue() instanceof SDocumentGraph)) { return; } xml.writeEmptyElement(TAG_LABELS); if (label != null) { String type = ""; if (label instanceof SAnnotation) { type = "saltCore:SAnnotation"; } else if (label instanceof SMetaAnnotation) { type = "saltCore:SMetaAnnotation"; } else if (label instanceof SProcessingAnnotation) { type = "saltCore:SProcessingAnnotation"; } else if (label instanceof SFeature) { type = "saltCore:SFeature"; } else if (label instanceof Identifier) { type = "saltCore:SElementId"; } xml.writeAttribute(NS_VALUE_XSI, ATT_XSI_TYPE, type); if (label.getNamespace() != null && !label.getNamespace().isEmpty()) { if (SaltUtil.FEAT_SDATA.equals(label.getName())) { xml.writeAttribute(ATT_NAMESPACE, "saltCommon"); } else { xml.writeAttribute(ATT_NAMESPACE, label.getNamespace()); } } if (label.getName() != null && !label.getName().isEmpty()) { xml.writeAttribute(ATT_NAME, label.getName()); } if (label.getValue() != null) { xml.writeAttribute(ATT_VALUE, marshallValue(label.getValue())); } } }
From source file:org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer.java
/** * Writes the passed node object to the passed {@link XMLStreamWriter}. * //from w w w.j av a2s . c om * @param node * to be persist * @param xml * stream to write data to * @param layerPositions * @throws XMLStreamException */ public void writeNode(XMLStreamWriter xml, Node node, Map<? extends Layer, Integer> layerPositions) throws XMLStreamException { if (isPrettyPrint) { xml.writeCharacters("\n"); xml.writeCharacters("\t"); } xml.writeStartElement(TAG_NODES); String type = ""; // write type as attribute if (node instanceof STextualDS) { type = TYPE_STEXTUALDS; } else if (node instanceof STimeline) { type = TYPE_STIMELINE; } else if (node instanceof SMedialDS) { type = TYPE_SAUDIODS; } else if (node instanceof SToken) { type = TYPE_STOKEN; } else if (node instanceof SSpan) { type = TYPE_SSPAN; } else if (node instanceof SStructure) { type = TYPE_SSTRUCTURE; } else if (node instanceof SDocument) { type = TYPE_SDOCUMENT; } else if (node instanceof SCorpus) { type = TYPE_SCORPUS; } xml.writeAttribute(NS_VALUE_XSI, ATT_XSI_TYPE, type); // write layers if (node.getLayers().size() > 0) { StringBuilder layerAtt = new StringBuilder(); Iterator<? extends Layer> layerIt = node.getLayers().iterator(); boolean isFirst = true; while (layerIt.hasNext()) { if (!isFirst) { layerAtt.append(" "); } isFirst = false; layerAtt.append("/"); if (writtenRootObjects != null) { layerAtt.append(writtenRootObjects.size()); } layerAtt.append("/@layers."); layerAtt.append(layerPositions.get(layerIt.next())); } xml.writeAttribute(ATT_LAYERS, layerAtt.toString()); } // write all labels Iterator<Label> labelIt = node.getLabels().iterator(); while (labelIt.hasNext()) { if (isPrettyPrint) { xml.writeCharacters("\n"); xml.writeCharacters("\t"); xml.writeCharacters("\t"); } writeLabel(xml, labelIt.next()); } if (isPrettyPrint) { xml.writeCharacters("\n"); xml.writeCharacters("\t"); } xml.writeEndElement(); }
From source file:org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer.java
/** * Writes the passed relation object to the passed {@link XMLStreamWriter}. * /* w ww . j a v a 2 s . c o m*/ * @param relation * to be persist * @param xml * stream to write data to * @param nodePositions * a map containing all positions of nodes in the list of nodes * @param layerPositions * @throws XMLStreamException */ public void writeRelation(XMLStreamWriter xml, Relation relation, Map<? extends Node, Integer> nodePositions, Map<? extends Layer, Integer> layerPositions) throws XMLStreamException { if (isPrettyPrint) { xml.writeCharacters("\n"); xml.writeCharacters("\t"); } xml.writeStartElement(TAG_EDGES); // write type String type = ""; if (relation instanceof STextualRelation) { type = TYPE_STEXTUAL_RELATION; } else if (relation instanceof STimelineRelation) { type = TYPE_STIMELINE_RELATION; } else if (relation instanceof SMedialRelation) { type = TYPE_SAUDIO_RELATION; } else if (relation instanceof SSpanningRelation) { type = TYPE_SSPANNING_RELATION; } else if (relation instanceof SDominanceRelation) { type = TYPE_SDOMINANCE_RELATION; } else if (relation instanceof SPointingRelation) { type = TYPE_SPOINTING_RELATION; } else if (relation instanceof SOrderRelation) { type = TYPE_SORDER_RELATION; } else if (relation instanceof SCorpusRelation) { type = TYPE_SCORPUS_RELATION; } else if (relation instanceof SCorpusDocumentRelation) { type = TYPE_SCORPUS_DOCUMENT_RELATION; } xml.writeAttribute(NS_VALUE_XSI, ATT_XSI_TYPE, type); int sourcePos = nodePositions.get(relation.getSource()); int targetPos = nodePositions.get(relation.getTarget()); if (writtenRootObjects == null) { // write shorcut notation if there is only one root object in the file xml.writeAttribute(ATT_SOURCE, "//@nodes." + sourcePos); xml.writeAttribute(ATT_TARGET, "//@nodes." + targetPos); } else { // write full notation when there are multiple root objects in the file int rootIndex = writtenRootObjects.size(); xml.writeAttribute(ATT_SOURCE, "/" + rootIndex + "/@nodes." + sourcePos); xml.writeAttribute(ATT_TARGET, "/" + rootIndex + "/@nodes." + targetPos); } // write layers if (relation.getLayers().size() > 0) { StringBuilder layerAtt = new StringBuilder(); Iterator<Layer> layerIt = relation.getLayers().iterator(); boolean isFirst = true; while (layerIt.hasNext()) { if (!isFirst) { layerAtt.append(" "); } isFirst = false; layerAtt.append("/"); if (writtenRootObjects != null) { // write full notation when there are multiple root objects in the file layerAtt.append(writtenRootObjects.size()); } layerAtt.append("/@layers."); layerAtt.append(layerPositions.get(layerIt.next())); } xml.writeAttribute(ATT_LAYERS, layerAtt.toString()); } // write all labels Iterator<Label> labelIt = relation.getLabels().iterator(); while (labelIt.hasNext()) { if (isPrettyPrint) { xml.writeCharacters("\n"); xml.writeCharacters("\t"); xml.writeCharacters("\t"); } writeLabel(xml, labelIt.next()); } if (isPrettyPrint) { xml.writeCharacters("\n"); xml.writeCharacters("\t"); } xml.writeEndElement(); }