List of usage examples for javax.xml.stream XMLStreamWriter writeStartElement
public void writeStartElement(String localName) throws XMLStreamException;
From source file:org.intermine.api.xml.ProfileManagerBinding.java
/** * Convert the contents of a ProfileManager to XML and write the XML to the given writer. * @param profileManager the ProfileManager * @param writer the XMLStreamWriter to write to *///from w ww. j a va2 s . c om public static void marshal(ProfileManager profileManager, XMLStreamWriter writer) { try { writer.writeStartElement("userprofiles"); String profileVersion = getProfileVersion(profileManager.getProfileObjectStoreWriter()); writer.writeAttribute(MetadataManager.PROFILE_FORMAT_VERSION, profileVersion); List<?> usernames = profileManager.getProfileUserNames(); for (Object userName : usernames) { Profile profile = profileManager.getProfile((String) userName); LOG.info("Writing profile: " + profile.getUsername()); long startTime = System.currentTimeMillis(); ProfileBinding.marshal(profile, profileManager.getProductionObjectStore(), writer, profileManager.getVersion(), getClassKeys(profileManager.getProductionObjectStore())); long totalTime = System.currentTimeMillis() - startTime; LOG.info("Finished writing profile: " + profile.getUsername() + " took " + totalTime + "ms."); } TrackManagerBinding.marshal(profileManager.getProfileObjectStoreWriter(), writer); writer.writeEndElement(); } catch (XMLStreamException e) { throw new RuntimeException(e); } }
From source file:org.intermine.api.xml.TagBindingTest.java
public void testMarshal() throws Exception { XMLUnit.setIgnoreWhitespace(true);//www . j a v a 2s . c om StringWriter sw = new StringWriter(); XMLOutputFactory factory = XMLOutputFactory.newInstance(); try { XMLStreamWriter writer = factory.createXMLStreamWriter(sw); writer.writeStartElement("tags"); List<Tag> tags = getTags(); for (Tag tag : tags) { TagBinding.marshal(tag, writer); } writer.writeEndElement(); writer.close(); } catch (XMLStreamException e) { throw new RuntimeException(e); } InputStream is = getClass().getClassLoader().getResourceAsStream("TagBindingTest.xml"); String expectedXml = IOUtils.toString(is); String actualXml = sw.toString().trim(); System.out.println(normalise(actualXml)); assertEquals("actual and expected XML should be the same", normalise(expectedXml), normalise(actualXml)); }
From source file:org.intermine.pathquery.PathQueryBinding.java
/** * Marshal to an XMLStreamWriter./*from w w w .j a v a 2s . co m*/ * * @param query the PathQuery * @param queryName the name of the query * @param modelName the model name * @param writer the xml stream writer to write to * @param version the version number of the xml format, an attribute of the ProfileManager */ public void doMarshal(PathQuery query, String queryName, String modelName, XMLStreamWriter writer, int version) { try { writer.writeStartElement("query"); writer.writeAttribute("name", queryName); writer.writeAttribute("model", modelName); writer.writeAttribute("view", StringUtil.join(query.getView(), " ")); if (query.getDescription() != null) { writer.writeAttribute("longDescription", query.getDescription()); } else { writer.writeAttribute("longDescription", ""); } StringBuilder sort = new StringBuilder(); boolean needComma = false; for (OrderElement oe : query.getOrderBy()) { if (needComma) { sort.append(" "); } needComma = true; sort.append(oe.getOrderPath() + (oe.getDirection().equals(OrderDirection.ASC) ? " asc" : " desc")); } String sortString = sort.toString(); if (!"".equals(sortString)) { writer.writeAttribute("sortOrder", sortString); } String logic = query.getConstraintLogic(); boolean hasMultipleConstraints = false; if ((logic != null) && (logic.length() > 1)) { writer.writeAttribute("constraintLogic", query.getConstraintLogic()); hasMultipleConstraints = true; } marshalPathQueryJoinStyle(query, writer); marshalPathQueryDescriptions(query, writer); marshalPathQueryConstraints(query, writer, hasMultipleConstraints); writer.writeEndElement(); } catch (XMLStreamException e) { throw new RuntimeException(e); } }
From source file:org.intermine.pathquery.PathQueryBinding.java
/** * Create XML for the constraints in a PathQuery. */// ww w . j ava 2s . c om private void marshalPathQueryConstraints(PathQuery query, XMLStreamWriter writer, boolean hasMultipleConstraints) throws XMLStreamException { for (Map.Entry<PathConstraint, String> constraint : query.getConstraints().entrySet()) { boolean emptyElement = true; if (constraint.getKey() instanceof PathConstraintMultiValue) { emptyElement = false; } if (emptyElement) { writer.writeEmptyElement("constraint"); } else { writer.writeStartElement("constraint"); } writer.writeAttribute("path", constraint.getKey().getPath()); if ((constraint.getValue() != null) && (hasMultipleConstraints)) { writer.writeAttribute("code", constraint.getValue()); } doAdditionalConstraintStuff(query, constraint.getKey(), writer); if (constraint.getKey() instanceof PathConstraintAttribute) { writer.writeAttribute("op", "" + constraint.getKey().getOp()); String outputValue = ((PathConstraintAttribute) constraint.getKey()).getValue(); writer.writeAttribute("value", "" + outputValue); } else if (constraint.getKey() instanceof PathConstraintNull) { writer.writeAttribute("op", "" + constraint.getKey().getOp()); } else if (constraint.getKey() instanceof PathConstraintSubclass) { writer.writeAttribute("type", ((PathConstraintSubclass) constraint.getKey()).getType()); } else if (constraint.getKey() instanceof PathConstraintBag) { writer.writeAttribute("op", "" + constraint.getKey().getOp()); writer.writeAttribute("value", ((PathConstraintBag) constraint.getKey()).getBag()); } else if (constraint.getKey() instanceof PathConstraintIds) { writer.writeAttribute("op", "" + constraint.getKey().getOp()); StringBuilder sb = new StringBuilder(); boolean needComma = false; for (Integer id : ((PathConstraintIds) constraint.getKey()).getIds()) { if (needComma) { sb.append(", "); } needComma = true; sb.append("" + id); } writer.writeAttribute("ids", sb.toString()); } else if (constraint.getKey() instanceof PathConstraintMultiValue) { // Includes PathConstraintRange, which is serialised in the exact same manner. writer.writeAttribute("op", "" + constraint.getKey().getOp()); for (String value : ((PathConstraintMultiValue) constraint.getKey()).getValues()) { if (value == null) { writer.writeStartElement("nullValue"); writer.writeEndElement(); } else { if (!value.equals(value.trim())) { throw new XMLStreamException("Value in MultiValue starts or ends with " + "whitespace - this query cannot be represented in XML"); } writer.writeStartElement("value"); writer.writeCharacters(value); writer.writeEndElement(); } } } else if (constraint.getKey() instanceof PathConstraintLoop) { writer.writeAttribute("op", "" + constraint.getKey().getOp()); writer.writeAttribute("loopPath", ((PathConstraintLoop) constraint.getKey()).getLoopPath()); } else if (constraint.getKey() instanceof PathConstraintLookup) { writer.writeAttribute("op", "" + constraint.getKey().getOp()); writer.writeAttribute("value", ((PathConstraintLookup) constraint.getKey()).getValue()); String extraValue = ((PathConstraintLookup) constraint.getKey()).getExtraValue(); if (extraValue != null) { writer.writeAttribute("extraValue", extraValue); } } else { throw new IllegalStateException( "Unrecognised constraint type " + constraint.getKey().getClass().getName()); } if (!emptyElement) { writer.writeEndElement(); } } }
From source file:org.intermine.template.xml.TemplateQueryBinding.java
/** * Convert a TemplateQuery to XML and write XML to given writer. * * @param template the TemplateQuery//from w ww . j ava2 s .c o m * @param writer the XMLStreamWriter to write to * @param version the version number of the XML format */ public void doMarshal(TemplateQuery template, XMLStreamWriter writer, int version) { if (template == null) { throw new NullPointerException("template must not be null"); } if (writer == null) { throw new NullPointerException("writer must not be null"); } try { writer.writeCharacters("\n"); writer.writeStartElement("template"); writer.writeAttribute("name", template.getName()); if (template.getTitle() != null) { writer.writeAttribute("title", template.getTitle()); } if (template.getComment() == null) { writer.writeAttribute("comment", ""); } else { writer.writeAttribute("comment", template.getComment()); } doMarshal(template, template.getName(), template.getModel().getName(), writer, version); writer.writeEndElement(); } catch (XMLStreamException e) { throw new RuntimeException(e); } }