List of usage examples for javax.xml.stream XMLStreamWriter writeEndElement
public void writeEndElement() throws XMLStreamException;
From source file:com.smartbear.jenkins.plugins.testcomplete.TcLogParser.java
private void convertToXML(ZipFile logArchive, TcLogInfo logInfo, XMLStreamWriter writer) throws ParsingException, XMLStreamException { writer.writeStartDocument("utf-8", "1.0"); Node descriptionTopLevelNode = NodeUtils.getRootDocumentNodeFromArchive(logArchive, DESCRIPTION_ENTRY_NAME); if (descriptionTopLevelNode == null) { throw new ParsingException("Unable to obtain description top-level node."); }/*from ww w . j ava2s . co m*/ Node topLevelNode = NodeUtils.getRootDocumentNodeFromArchive(logArchive, NodeUtils.getTextProperty(descriptionTopLevelNode, "root file name")); if (topLevelNode == null) { throw new ParsingException("Unable to obtain root top-level node."); } NodeList rootNodes = topLevelNode.getChildNodes(); Node rootOwnerNode = NodeUtils.findRootOwnerNode(rootNodes); if (rootOwnerNode == null) { throw new ParsingException("Unable to obtain root owner node."); } boolean isSuite = "{00000000-0000-0000-0000-000000000000}" .equals(NodeUtils.getTextProperty(rootOwnerNode, "projectkey")); Node rootOwnerNodeInfo = NodeUtils.getRootDocumentNodeFromArchive(logArchive, NodeUtils.getTextProperty(rootOwnerNode, "filename")); if (rootOwnerNodeInfo == null) { throw new ParsingException("Unable to obtain root owner node info."); } Node rootOwnerNodeInfoSummary = NodeUtils.findNamedNode(rootOwnerNodeInfo.getChildNodes(), "summary"); boolean isSuiteOrProject = rootOwnerNodeInfoSummary != null; writer.writeStartElement("testsuites"); if (isSuite) { List<Node> projects = NodeUtils.findChildNodes(rootOwnerNode, rootOwnerNode.getParentNode().getChildNodes()); for (Node projectNode : projects) { Node projectNodeInfo = NodeUtils.getRootDocumentNodeFromArchive(logArchive, NodeUtils.getTextProperty(projectNode, "filename")); Node projectNodeInfoSummary = NodeUtils.findNamedNode(projectNodeInfo, "summary"); processProject(logArchive, projectNode, projectNodeInfoSummary, writer); } } else if (isSuiteOrProject) { processProject(logArchive, rootOwnerNode, rootOwnerNodeInfoSummary, writer); } else { String testCaseName = NodeUtils.getTextProperty(rootOwnerNode, "name"); String testCaseDuration = Double.toString(logInfo.getTestDuration() / 1000f); writer.writeStartElement("testsuite"); writer.writeAttribute("name", project); writer.writeAttribute("time", testCaseDuration); writer.writeStartElement("testcase"); writer.writeAttribute("name", fixTestCaseName(testCaseName)); writer.writeAttribute("classname", suite + "." + project); writer.writeAttribute("time", testCaseDuration); if (checkFail(NodeUtils.getTextProperty(rootOwnerNode, "status"))) { writer.writeStartElement("failure"); List<String> messages = NodeUtils.getErrorMessages(rootOwnerNodeInfo); if (errorOnWarnings) { messages.addAll(NodeUtils.getWarningMessages(rootOwnerNodeInfo)); } writer.writeAttribute("message", StringUtils.join(messages, "\n\n")); writer.writeEndElement(); //failure } writer.writeEndElement(); //testcase writer.writeEndElement(); //testsuite } writer.writeEndElement(); //testsuites writer.writeEndDocument(); }
From source file:com.fiorano.openesb.application.aps.ServiceInstance.java
public void toJXMLString(XMLStreamWriter writer) throws XMLStreamException, FioranoException { //Start ServiceInstance writer.writeStartElement("ServiceInstance"); //Write Attributes writer.writeAttribute("isManualLaunch", "" + m_isManualLaunch); writer.writeAttribute("isStateful", "" + m_isStateful); writer.writeAttribute("isDelayedLaunch", "" + m_isDelayedLaunch); writer.writeAttribute("delayedPort", "" + m_delayedPortName); writer.writeAttribute("maxRetries", "" + m_maxRetries); writer.writeAttribute("isTransacted", "" + m_isTransacted); writer.writeAttribute("isErrorHandlingEnabled", "" + m_isErrorHandlingEnabled); writer.writeAttribute("isInMemoryLaunch", "" + m_isInMemoryLaunch); writer.writeAttribute("isEndOfWorkflow", "" + m_isEndOfWorkflow); writer.writeAttribute("preferLaunchOnHigherLevelNode", "" + m_bPreferLaunchOnHigherLevelNode); writer.writeAttribute("killPrimaryOnSecondaryLaunch", "" + m_bKillPrimaryOnSecondaryLaunch); writer.writeAttribute("isDebugMode", "" + m_bIsDebugMode); writer.writeAttribute("debugPort", "" + m_iDebugPort); writer.writeAttribute("isTransportLPC", "" + m_isTransportLPC); writer.writeAttribute("profile", "" + m_profile); FioranoStackSerializer.writeElement("ServiceInstanceName", m_servInstName, writer); FioranoStackSerializer.writeElement("ServiceGUID", m_servGUID, writer); FioranoStackSerializer.writeElement("BufferSizePerPort", m_dBufferSizePerPort + "", writer); if (m_version != null) { writer.writeStartElement("Version"); writer.writeAttribute("isLocked", "" + m_isVersionLocked); writer.writeCharacters(m_version); writer.writeEndElement(); }//from www . j a va 2 s .c o m if (m_nodes != null && m_nodes.size() > 0) { Enumeration nodeNameEnum = m_nodes.keys(); Enumeration nodeLevelEnum = m_nodes.elements(); while (nodeNameEnum.hasMoreElements()) { String nodeName = (String) nodeNameEnum.nextElement(); String nodeLevel = (String) nodeLevelEnum.nextElement(); writer.writeStartElement("Node"); writer.writeAttribute("level", nodeLevel); writer.writeCharacters(nodeName); writer.writeEndElement(); } } if (m_eventHandler > 0) { writer.writeStartElement("EventHandler"); writer.writeAttribute("deliveryMode", "" + m_eventDeliveryMode); writer.writeAttribute("expiryTime", "" + m_eventExpiryTime); writer.writeCharacters("" + m_eventHandler); writer.writeEndElement(); } if (m_runtimeDependencies != null && m_runtimeDependencies.size() > 0) { Enumeration enums = m_runtimeDependencies.elements(); while (enums.hasMoreElements()) { RuntimeDependency runtimeDependency = (RuntimeDependency) enums.nextElement(); runtimeDependency.toJXMLString(writer); } } if (m_runtimeArgs != null) { m_runtimeArgs.toJXMLString(writer); } if (m_portInstDescriptor != null) { m_portInstDescriptor.toJXMLString(writer); } if (!StringUtils.isEmpty(m_longDescription)) { FioranoStackSerializer.writeElement("LongDescription", m_longDescription, writer); } if (!StringUtils.isEmpty(m_shortDescription)) { FioranoStackSerializer.writeElement("ShortDescription", m_shortDescription, writer); } //LogManager if (m_logManager != null) { //Start LogMangaer writer.writeStartElement("LogManager"); FioranoStackSerializer.writeElement("Name", m_logManager, writer); if (m_logParams != null && m_logParams.size() > 0) { Enumeration enums = m_logParams.elements(); while (enums.hasMoreElements()) { Param param = (Param) enums.nextElement(); if (!StringUtils.isEmpty(param.getParamName()) && !StringUtils.isEmpty(param.getParamValue())) { param.toJXMLString(writer); } } } //End LogManager writer.writeEndElement(); } if (m_params != null && m_params.size() > 0) { Enumeration enums = m_params.elements(); while (enums.hasMoreElements()) { Param param = (Param) enums.nextElement(); if (!StringUtils.isEmpty(param.getParamName()) && !StringUtils.isEmpty(param.getParamValue())) param.toJXMLString(writer); } } if (m_statusTracking != null) { m_statusTracking.toJXMLString(writer); } if (m_vecEndStates != null) { for (int i = 0; i < m_vecEndStates.size(); i++) { EndState endState = (EndState) m_vecEndStates.get(i); endState.toJXMLString(writer); } } if (m_monitor != null) { m_monitor.toJXMLString(writer); } if (m_logModules != null) { m_logModules.toJXMLString(writer); } //End ServiceInstance writer.writeEndElement(); }
From source file:ca.uhn.fhir.parser.XmlParser.java
private void encodeResourceToXmlStreamWriter(IBaseResource theResource, XMLStreamWriter theEventWriter, boolean theContainedResource, IIdType theResourceId) throws XMLStreamException { if (!theContainedResource) { super.containResourcesForEncoding(theResource); }//from w ww . ja v a 2 s . co m RuntimeResourceDefinition resDef = myContext.getResourceDefinition(theResource); if (resDef == null) { throw new ConfigurationException("Unknown resource type: " + theResource.getClass()); } theEventWriter.writeStartElement(resDef.getName()); theEventWriter.writeDefaultNamespace(FHIR_NS); if (theResource instanceof IAnyResource) { // HL7.org Structures if (theResourceId != null) { writeCommentsPre(theEventWriter, theResourceId); writeOptionalTagWithValue(theEventWriter, "id", theResourceId.getIdPart()); writeCommentsPost(theEventWriter, theResourceId); } encodeCompositeElementToStreamWriter(theResource, theResource, theEventWriter, theContainedResource, new CompositeChildElement(resDef)); } else { if (myContext.getVersion().getVersion().isNewerThan(FhirVersionEnum.DSTU1)) { // DSTU2+ IResource resource = (IResource) theResource; if (theResourceId != null) { writeCommentsPre(theEventWriter, theResourceId); writeOptionalTagWithValue(theEventWriter, "id", theResourceId.getIdPart()); writeCommentsPost(theEventWriter, theResourceId); } InstantDt updated = (InstantDt) resource.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED); IdDt resourceId = resource.getId(); String versionIdPart = resourceId.getVersionIdPart(); if (isBlank(versionIdPart)) { versionIdPart = ResourceMetadataKeyEnum.VERSION.get(resource); } List<BaseCodingDt> securityLabels = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.SECURITY_LABELS); List<? extends IIdType> profiles = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.PROFILES); profiles = super.getProfileTagsForEncoding(resource, profiles); TagList tags = getMetaTagsForEncoding((resource)); if (super.shouldEncodeResourceMeta(resource) && ElementUtil.isEmpty(versionIdPart, updated, securityLabels, tags, profiles) == false) { theEventWriter.writeStartElement("meta"); writeOptionalTagWithValue(theEventWriter, "versionId", versionIdPart); if (updated != null) { writeOptionalTagWithValue(theEventWriter, "lastUpdated", updated.getValueAsString()); } for (IIdType profile : profiles) { theEventWriter.writeStartElement("profile"); theEventWriter.writeAttribute("value", profile.getValue()); theEventWriter.writeEndElement(); } for (BaseCodingDt securityLabel : securityLabels) { theEventWriter.writeStartElement("security"); encodeCompositeElementToStreamWriter(resource, securityLabel, theEventWriter, theContainedResource, null); theEventWriter.writeEndElement(); } if (tags != null) { for (Tag tag : tags) { if (tag.isEmpty()) { continue; } theEventWriter.writeStartElement("tag"); writeOptionalTagWithValue(theEventWriter, "system", tag.getScheme()); writeOptionalTagWithValue(theEventWriter, "code", tag.getTerm()); writeOptionalTagWithValue(theEventWriter, "display", tag.getLabel()); theEventWriter.writeEndElement(); } } theEventWriter.writeEndElement(); } if (theResource instanceof IBaseBinary) { IBaseBinary bin = (IBaseBinary) theResource; writeOptionalTagWithValue(theEventWriter, "contentType", bin.getContentType()); writeOptionalTagWithValue(theEventWriter, "content", bin.getContentAsBase64()); } else { encodeCompositeElementToStreamWriter(theResource, theResource, theEventWriter, theContainedResource, new CompositeChildElement(resDef)); } } else { // DSTU1 if (theResourceId != null && theContainedResource && theResourceId.hasIdPart()) { theEventWriter.writeAttribute("id", theResourceId.getIdPart()); } if (theResource instanceof IBaseBinary) { IBaseBinary bin = (IBaseBinary) theResource; if (bin.getContentType() != null) { theEventWriter.writeAttribute("contentType", bin.getContentType()); } theEventWriter.writeCharacters(bin.getContentAsBase64()); } else { encodeCompositeElementToStreamWriter(theResource, theResource, theEventWriter, theContainedResource, new CompositeChildElement(resDef)); } } } theEventWriter.writeEndElement(); }
From source file:com.predic8.membrane.annot.bean.MCUtil.java
private static void addXML(Object object, String id, XMLStreamWriter xew, SerializationContext sc) throws XMLStreamException { if (object == null) throw new InvalidParameterException("'object' must not be null."); Class<? extends Object> clazz = object.getClass(); MCElement e = clazz.getAnnotation(MCElement.class); if (e == null) throw new IllegalArgumentException("'object' must be @MCElement-annotated."); BeanWrapperImpl src = new BeanWrapperImpl(object); xew.writeStartElement(e.name());//from w w w . j a v a 2s.c om if (id != null) xew.writeAttribute("id", id); HashSet<String> attributes = new HashSet<String>(); for (Method m : clazz.getMethods()) { if (!m.getName().startsWith("set")) continue; String propertyName = AnnotUtils.dejavaify(m.getName().substring(3)); MCAttribute a = m.getAnnotation(MCAttribute.class); if (a != null) { Object value = src.getPropertyValue(propertyName); String str; if (value == null) continue; else if (value instanceof String) str = (String) value; else if (value instanceof Boolean) str = ((Boolean) value).toString(); else if (value instanceof Integer) str = ((Integer) value).toString(); else if (value instanceof Long) str = ((Long) value).toString(); else if (value instanceof Enum<?>) str = value.toString(); else { MCElement el = value.getClass().getAnnotation(MCElement.class); if (el != null) { str = defineBean(sc, value, null, true); } else { str = "?"; sc.incomplete = true; } } if (a.attributeName().length() > 0) propertyName = a.attributeName(); attributes.add(propertyName); xew.writeAttribute(propertyName, str); } } for (Method m : clazz.getMethods()) { if (!m.getName().startsWith("set")) continue; String propertyName = AnnotUtils.dejavaify(m.getName().substring(3)); MCOtherAttributes o = m.getAnnotation(MCOtherAttributes.class); if (o != null) { Object value = src.getPropertyValue(propertyName); if (value instanceof Map<?, ?>) { Map<?, ?> map = (Map<?, ?>) value; for (Map.Entry<?, ?> entry : map.entrySet()) { Object key = entry.getKey(); Object val = entry.getValue(); if (!(key instanceof String) || !(val instanceof String)) { sc.incomplete = true; key = "incompleteAttributes"; val = "?"; } if (attributes.contains(key)) continue; attributes.add((String) key); xew.writeAttribute((String) key, (String) val); } } else { xew.writeAttribute("incompleteAttributes", "?"); sc.incomplete = true; } } } List<Method> childElements = new ArrayList<Method>(); for (Method m : clazz.getMethods()) { if (!m.getName().startsWith("set")) continue; String propertyName = AnnotUtils.dejavaify(m.getName().substring(3)); MCChildElement c = m.getAnnotation(MCChildElement.class); if (c != null) { childElements.add(m); } MCTextContent t = m.getAnnotation(MCTextContent.class); if (t != null) { Object value = src.getPropertyValue(propertyName); if (value == null) { continue; } else if (value instanceof String) { xew.writeCharacters((String) value); } else { xew.writeCharacters("?"); sc.incomplete = true; } } } Collections.sort(childElements, new Comparator<Method>() { @Override public int compare(Method o1, Method o2) { MCChildElement c1 = o1.getAnnotation(MCChildElement.class); MCChildElement c2 = o2.getAnnotation(MCChildElement.class); return c1.order() - c2.order(); } }); for (Method m : childElements) { String propertyName = AnnotUtils.dejavaify(m.getName().substring(3)); Object value = src.getPropertyValue(propertyName); if (value != null) { if (value instanceof Collection<?>) { for (Object item : (Collection<?>) value) addXML(item, null, xew, sc); } else { addXML(value, null, xew, sc); } } } xew.writeEndElement(); }
From source file:de.uni_koblenz.jgralab.utilities.rsa2tg.SchemaGraph2XMI.java
/** * Creates the default values.//from w w w . j a va2s .c om * * @param writer * {@link XMLStreamWriter} of the current XMI file * @param defaultValue * {@link String} the default value of the current * {@link Attribute} * @param id * {@link String} the id of the current {@link Attribute} * @param domain * {@link Domain} the type of the current {@link Attribute} * @throws XMLStreamException */ private void createDefaultValue(XMLStreamWriter writer, String defaultValue, String id, Domain domain) throws XMLStreamException { // start defaultValue if (domain instanceof LongDomain || domain instanceof EnumDomain) { writer.writeEmptyElement(XMIConstants4SchemaGraph2XMI.TAG_DEFAULTVALUE); } else { writer.writeStartElement(XMIConstants4SchemaGraph2XMI.TAG_DEFAULTVALUE); } if (domain instanceof BooleanDomain) { writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.TYPE_VALUE_LITERALBOOLEAN); } else if (domain instanceof IntegerDomain || domain instanceof LongDomain) { writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.TYPE_VALUE_LITERALINTEGER); } else if (domain instanceof EnumDomain) { writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.TYPE_VALUE_INSTANCEVALUE); } else { writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.TYPE_VALUE_OPAQUEEXPRESSION); } writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_ID, id + "_defaultValue"); if (domain instanceof BooleanDomain || domain instanceof IntegerDomain || domain instanceof LongDomain || domain instanceof EnumDomain) { if (domain instanceof BooleanDomain) { writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_VALUE, defaultValue.equals("t") ? "true" : "false"); } else if (domain instanceof EnumDomain) { writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_NAME, defaultValue); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, domain.get_qualifiedName()); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.DEFAULTVALUE_ATTRIBUTE_INSTANCE, domain.get_qualifiedName() + "_" + defaultValue); } else { writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_VALUE, defaultValue); } // create type if (domain instanceof BooleanDomain || domain instanceof IntegerDomain) { createType(writer, domain); } } else { if (domain instanceof StringDomain) { // create type createType(writer, domain); } else { // there has to be created an entry for the current domain in // the package primitiveTypes writer.writeAttribute(XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, domain.get_qualifiedName().replaceAll("\\s", "").replaceAll("<", "_").replaceAll(">", "_")); } // start body writer.writeStartElement(XMIConstants4SchemaGraph2XMI.TAG_BODY); writer.writeCharacters(defaultValue); // end body writer.writeEndElement(); } if (!(domain instanceof LongDomain || domain instanceof EnumDomain)) { // end defaultValue writer.writeEndElement(); } }
From source file:de.uni_koblenz.jgralab.utilities.rsa2tg.SchemaGraph2XMI.java
/** * This method creates the representation of: * <ul>/* w w w .ja v a 2 s .c o m*/ * <li>the {@link Package} <code>pack</code>, if it is not the * defaultPackage,</li> * <li>{@link Comment}s which are attached to <code>pack</code>,</li> * <li>{@link EnumDomain}s,</li> * <li>{@link RecordDomain}s,</li> * <li>{@link GraphElementClass}es and</li> * <li>{@link Package}s</li> * </ul> * contained in <code>pack</code>. The creation of other {@link Domain}s * except {@link EnumDomain}s and {@link RecordDomain}s are explained at * {@link SchemaGraph2XMI#createAttribute(XMLStreamWriter, String, String, Domain, String)} * . * * @param writer * {@link XMLStreamWriter} of the current XMI file * @param pack * {@link Package} the current {@link Package} * @throws XMLStreamException */ private void createPackage(XMLStreamWriter writer, Package pack) throws XMLStreamException { boolean packageTagHasToBeClosed = false; if (!isPackageEmpty(pack)) { if (!pack.get_qualifiedName().equals(de.uni_koblenz.jgralab.schema.Package.DEFAULTPACKAGE_NAME)) { // for the default package there is not created a package tag packageTagHasToBeClosed = pack.getFirstAnnotatesIncidence() != null || pack.getFirstContainsDomainIncidence() != null || pack.getFirstContainsGraphElementClassIncidence() != null || pack.getFirstContainsSubPackageIncidence(EdgeDirection.OUT) != null; if (packageTagHasToBeClosed) { // start package writer.writeStartElement(XMIConstants4SchemaGraph2XMI.TAG_PACKAGEDELEMENT); } else { // create empty package writer.writeEmptyElement(XMIConstants4SchemaGraph2XMI.TAG_PACKAGEDELEMENT); } writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.PACKAGEDELEMENT_TYPE_VALUE_PACKAGE); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_ID, pack.get_qualifiedName()); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_NAME, extractSimpleName(pack.get_qualifiedName())); } // create comments createComments(writer, pack); // create domains for (ContainsDomain cd : pack.getContainsDomainIncidences()) { Domain domain = (Domain) cd.getThat(); if (!domain.get_qualifiedName() .equals(de.uni_koblenz.jgralab.schema.BooleanDomain.BOOLEANDOMAIN_NAME) && !domain.get_qualifiedName() .equals(de.uni_koblenz.jgralab.schema.DoubleDomain.DOUBLEDOMAIN_NAME) && !domain.get_qualifiedName() .equals(de.uni_koblenz.jgralab.schema.IntegerDomain.INTDOMAIN_NAME) && !domain.get_qualifiedName() .equals(de.uni_koblenz.jgralab.schema.LongDomain.LONGDOMAIN_NAME) && !domain.get_qualifiedName() .equals(de.uni_koblenz.jgralab.schema.StringDomain.STRINGDOMAIN_NAME)) { // skip basic domains if (domain instanceof EnumDomain) { createEnum(writer, (EnumDomain) domain); } else if (domain instanceof RecordDomain) { createRecordDomain(writer, (RecordDomain) domain); } } } // create GraphElementClasses for (ContainsGraphElementClass cgec : pack.getContainsGraphElementClassIncidences()) { GraphElementClass gec = (GraphElementClass) cgec.getThat(); createAttributedElementClass(writer, gec); } // create subpackages for (ContainsSubPackage csp : pack.getContainsSubPackageIncidences(EdgeDirection.OUT)) { // create subpackages createPackage(writer, (Package) csp.getThat()); } if (packageTagHasToBeClosed) { // close packagedElement writer.writeEndElement(); } } }
From source file:edu.ucsd.library.dams.api.DAMSAPIServlet.java
private void sparqlQuery(String sparql, TripleStore ts, Map<String, String[]> params, String pathInfo, HttpServletResponse res) throws Exception { if (sparql == null) { Map err = error(SC_BAD_REQUEST, "No query specified.", null); output(err, params, pathInfo, res); return;/*w ww . jav a 2 s .co m*/ } else { log.info("sparql: " + sparql); } // sparql query BindingIterator objs = ts.sparqlSelect(sparql); // start output String sparqlNS = "http://www.w3.org/2005/sparql-results#"; res.setContentType("application/sparql-results+xml"); OutputStream out = res.getOutputStream(); XMLOutputFactory factory = XMLOutputFactory.newInstance(); XMLStreamWriter stream = factory.createXMLStreamWriter(out); stream.setDefaultNamespace(sparqlNS); stream.writeStartDocument(); stream.writeStartElement("sparql"); // output bindings boolean headerWritten = false; while (objs.hasNext()) { Map<String, String> binding = objs.nextBinding(); // write header on first binding if (!headerWritten) { Iterator<String> it = binding.keySet().iterator(); stream.writeStartElement("head"); while (it.hasNext()) { String k = it.next(); stream.writeStartElement("variable"); stream.writeAttribute("name", k); stream.writeEndElement(); } stream.writeEndElement(); stream.writeStartElement("results"); // ordered='false' distinct='false' headerWritten = true; } stream.writeStartElement("result"); Iterator<String> it = binding.keySet().iterator(); while (it.hasNext()) { String k = it.next(); String v = binding.get(k); stream.writeStartElement("binding"); stream.writeAttribute("name", k); String type = null; if (v.startsWith("\"") && v.endsWith("\"")) { type = "literal"; v = v.substring(1, v.length() - 1); } else if (v.startsWith("_:")) { type = "bnode"; v = v.substring(2); } else { type = "uri"; } stream.writeStartElement(type); stream.writeCharacters(v); stream.writeEndElement(); stream.writeEndElement(); } stream.writeEndElement(); } // finish output stream.writeEndElement(); stream.writeEndDocument(); stream.flush(); stream.close(); }
From source file:de.uni_koblenz.jgralab.utilities.rsa2tg.SchemaGraph2XMI.java
/** * Creates the representation of the {@link IncidenceClass} which contains * the information for the {@link VertexClass} which is specified by * <code>qualifiedNameOfVertexClass</code> e.g. <code>otherIncidence</code>.<br/> * The relevant information are: <ui> <li>the rolename,</li> <li>the min * value,</li> <li>the max value and</li> <li>composition or shared * information.</li> </ui> The default rolename for associations is * <code>qualifiedNameOfVertexClass_edgeClass.getQualifiedName()</code> and * <code>""</code> for associationClasses. * //from ww w . j a v a 2 s . c o m * @param writer * {@link XMLStreamWriter} of the current XMI file * @param otherIncidence * {@link IncidenceClass} which contains the information for the * incidence corresponding to the {@link VertexClass} of * <code>qualifiedNameOfVertexClass</code> in the xmi file. * @param connectedVertexClass * {@link VertexClass} at the other end of <code>edgeClass</code> * @param incidence * {@link IncidenceClass} connected to the {@link VertexClass} * specified by <code>qualifiedNameOfVertexClass</code> * @param edgeClass * {@link EdgeClass} which connects the {@link VertexClass} * specified by <code>qualifiedNameOfVertexClass</code> with * <code>connectedVertexClass</code> * @param qualifiedNameOfVertexClass * {@link String} the qualified name of the {@link VertexClass} * to which <code>incidence</code> is connected to. * @param createOwnedEnd * boolean if set to <code>true</code>, the tag ownedEnd is used * instead of ownedAttribute (ownedEnd has to be created, if you * create an incidence at an association.) * @throws XMLStreamException */ private void createIncidence(XMLStreamWriter writer, IncidenceClass otherIncidence, EdgeClass edgeClass, IncidenceClass incidence, VertexClass connectedVertexClass, String qualifiedNameOfVertexClass, boolean createOwnedEnd) throws XMLStreamException { String incidenceId = qualifiedNameOfVertexClass + "_incidence_" + edgeClass.get_qualifiedName() + (incidence.getFirstComesFromIncidence() != null ? "_from" : "_to"); // start ownedattribute writer.writeStartElement(createOwnedEnd ? XMIConstants4SchemaGraph2XMI.TAG_OWNEDEND : XMIConstants4SchemaGraph2XMI.TAG_OWNEDATTRIBUTE); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.OWNEDATTRIBUTE_TYPE_VALUE); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_ID, incidenceId); // set rolenames if (otherIncidence.get_roleName() != null && !otherIncidence.get_roleName().isEmpty()) { writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_NAME, otherIncidence.get_roleName()); } else if (isRoleNameNecessary(edgeClass, connectedVertexClass.get_qualifiedName())) { String newRoleName = createNewUniqueRoleName(connectedVertexClass); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_NAME, newRoleName); // to find already used rolenames set the newly created rolename otherIncidence.set_roleName(newRoleName); } writer.writeAttribute(XMIConstants4SchemaGraph2XMI.OWNEDATTRIBUTE_ATTRIBUTE_VISIBILITY, XMIConstants4SchemaGraph2XMI.OWNEDATTRIBUTE_VISIBILITY_VALUE_PRIVATE); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.PACKAGEDELEMENT_ATTRIBUTE_TYPE, connectedVertexClass.get_qualifiedName()); // set composite or shared if (otherIncidence.get_aggregation() == AggregationKind.SHARED) { writer.writeAttribute(XMIConstants4SchemaGraph2XMI.OWNEDATTRIBUTE_ATTRIBUTE_AGGREGATION, XMIConstants4SchemaGraph2XMI.OWNEDATTRIBUTE_ATTRIBUTE_AGGREGATION_VALUE_SHARED); } else if (otherIncidence.get_aggregation() == AggregationKind.COMPOSITE) { writer.writeAttribute(XMIConstants4SchemaGraph2XMI.OWNEDATTRIBUTE_ATTRIBUTE_AGGREGATION, XMIConstants4SchemaGraph2XMI.OWNEDATTRIBUTE_ATTRIBUTE_AGGREGATION_VALUE_COMPOSITE); } writer.writeAttribute(XMIConstants4SchemaGraph2XMI.PACKAGEDELEMENT_ATTRIBUTE_ASSOCIATION, edgeClass.get_qualifiedName()); // create upperValue writer.writeEmptyElement(XMIConstants4SchemaGraph2XMI.TAG_UPPERVALUE); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.TYPE_VALUE_LITERALUNLIMITEDNATURAL); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_ID, incidenceId + "_uppervalue"); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_VALUE, otherIncidence.get_max() == Integer.MAX_VALUE ? "*" : Integer.toString(otherIncidence.get_max())); // create lowerValue writer.writeEmptyElement(XMIConstants4SchemaGraph2XMI.TAG_LOWERVALUE); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.TYPE_VALUE_LITERALINTEGER); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_ID, incidenceId + "_lowervalue"); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_VALUE, incidence.get_min() == Integer.MAX_VALUE ? "*" : Integer.toString(otherIncidence.get_min())); // close ownedattribute writer.writeEndElement(); }
From source file:de.uni_koblenz.jgralab.utilities.rsa2tg.SchemaGraph2XMI.java
/** * Creates the representation of the {@link AttributedElementClass} * <code>aeclass</code>.<br /> * A {@link GraphClass} is represented as a UML class with stereotype * <code><<graphclass>></code>.<br /> * A {@link VertexClass} is represented as a UML class.<br /> * An {@link EdgeClass} is represented as a UML association, if it has no * attributes and otherwise as a UML associationClass.<br /> * Furthermore the attribute abstract is generated, if the * {@link VertexClass} or {@link EdgeClass} is abstract. {@link Comment}s, * {@link Constraint}s, generalization and {@link Attribute}s are * represented as well.<br />//from ww w . ja v a 2s .c om * The {@link IncidenceClass} representations are created if needed. To get * more information if it is needed, have a look at * {@link SchemaGraph2XMI#createIncidences(XMLStreamWriter, EdgeClass)} and * {@link SchemaGraph2XMI#createIncidences(XMLStreamWriter, VertexClass)}. * * @param writer * {@link XMLStreamWriter} of the current XMI file * @param aeclass * {@link AttributedElementClass} the current * {@link AttributedElementClass} * @throws XMLStreamException */ private void createAttributedElementClass(XMLStreamWriter writer, AttributedElementClass aeclass) throws XMLStreamException { // if aeclass is a GraphElementClass without any attributes, comments // and constraints then an empty tag is created. Furthermore an // EdgeClass could only be empty if the associations are created // bidirectional. boolean isEmptyGraphElementClass = aeclass.getFirstAnnotatesIncidence() == null && aeclass.getFirstHasAttributeIncidence() == null && aeclass.getFirstHasConstraintIncidence() == null && (aeclass instanceof GraphElementClass && !((GraphElementClass) aeclass).is_abstract()) && (aeclass instanceof VertexClass && ((VertexClass) aeclass) .getFirstSpecializesVertexClassIncidence(EdgeDirection.OUT) == null && !hasChildIncidence((VertexClass) aeclass) || aeclass instanceof EdgeClass && ((EdgeClass) aeclass) .getFirstSpecializesEdgeClassIncidence(EdgeDirection.OUT) == null && isBidirectional); // start packagedElement if (isEmptyGraphElementClass) { writer.writeEmptyElement(XMIConstants4SchemaGraph2XMI.TAG_PACKAGEDELEMENT); } else { writer.writeStartElement(XMIConstants4SchemaGraph2XMI.TAG_PACKAGEDELEMENT); } // set type if (aeclass instanceof EdgeClass) { if (aeclass.getFirstHasAttributeIncidence() == null) { writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.PACKAGEDELEMENT_TYPE_VALUE_ASSOCIATION); } else { writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.PACKAGEDELEMENT_TYPE_VALUE_ASSOCIATIONCLASS); } } else { writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_TYPE, XMIConstants4SchemaGraph2XMI.PACKAGEDELEMENT_TYPE_VALUE_CLASS); } writer.writeAttribute(XMIConstants4SchemaGraph2XMI.NAMESPACE_XMI, XMIConstants4SchemaGraph2XMI.XMI_ATTRIBUTE_ID, aeclass.get_qualifiedName()); writer.writeAttribute(XMIConstants4SchemaGraph2XMI.ATTRIBUTE_NAME, extractSimpleName(aeclass.get_qualifiedName())); // set EdgeClass specific memberEnd if (aeclass instanceof EdgeClass) { EdgeClass ec = (EdgeClass) aeclass; writer.writeAttribute(XMIConstants4SchemaGraph2XMI.PACKAGEDELEMENT_ATTRIBUTE_MEMBEREND, ((VertexClass) ((IncidenceClass) ec.getFirstComesFromIncidence().getThat()) .getFirstEndsAtIncidence().getThat()).get_qualifiedName() + "_incidence_" + ec.get_qualifiedName() + "_from " + ((VertexClass) ((IncidenceClass) ec.getFirstGoesToIncidence().getThat()) .getFirstEndsAtIncidence().getThat()).get_qualifiedName() + "_incidence_" + ec.get_qualifiedName() + "_to"); } // set abstract if (aeclass instanceof GraphElementClass && ((GraphElementClass) aeclass).is_abstract()) { writer.writeAttribute(XMIConstants4SchemaGraph2XMI.PACKAGEDELEMENT_ATTRIBUTE_ISABSTRACT, XMIConstants4SchemaGraph2XMI.ATTRIBUTE_VALUE_TRUE); createExtension(writer, aeclass, "abstract"); } // create <<graphclass>> for graph classes if (aeclass instanceof GraphClass) { createExtension(writer, aeclass, "graphclass"); } // create comments createComments(writer, aeclass); // create constraints createConstraints(writer, aeclass); // create generalization if (aeclass instanceof VertexClass) { for (SpecializesVertexClass svc : ((VertexClass) aeclass) .getSpecializesVertexClassIncidences(EdgeDirection.OUT)) { createGeneralization(writer, "generalization_" + aeclass.get_qualifiedName(), ((VertexClass) svc.getThat()).get_qualifiedName()); } } else if (aeclass instanceof EdgeClass) { for (SpecializesEdgeClass svc : ((EdgeClass) aeclass) .getSpecializesEdgeClassIncidences(EdgeDirection.OUT)) { createGeneralization(writer, "generalization_" + aeclass.get_qualifiedName(), ((EdgeClass) svc.getThat()).get_qualifiedName()); } } // create attributes createAttributes(writer, aeclass); // create incidences of EdgeClasses at VertexClass aeclass if (aeclass instanceof VertexClass) { createIncidences(writer, (VertexClass) aeclass); } else if (aeclass instanceof EdgeClass) { createIncidences(writer, (EdgeClass) aeclass); } // close packagedElement if (!isEmptyGraphElementClass) { writer.writeEndElement(); } }