List of usage examples for javax.xml.stream XMLStreamWriter writeNamespace
public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException;
From source file:org.deegree.services.csw.exporthandling.GetCapabilitiesHandler.java
private void export202(XMLStreamWriter writer, Set<Sections> sections, ServiceIdentificationType identification, DeegreeServicesMetadataType mainControllerConf, DeegreeServiceControllerType mainConf) throws XMLStreamException { writer.writeStartElement(CSW_PREFIX, "Capabilities", CSW_202_NS); writer.writeNamespace(CSW_PREFIX, CSW_202_NS); writer.writeNamespace(OWS_PREFIX, OWS_NS); writer.writeNamespace(OGC_PREFIX, OGCNS); writer.writeNamespace(XLINK_PREFIX, XLN_NS); writer.writeNamespace(XSI_PREFIX, XSINS); writer.writeAttribute("version", "2.0.2"); writer.writeAttribute(XSINS, "schemaLocation", CSW_202_NS + " " + CSW_202_DISCOVERY_SCHEMA); // ows:ServiceIdentification if (sections.isEmpty() || sections.contains(Sections.ServiceIdentification)) { gcHelper.exportServiceIdentification(writer, identification, "CSW", "2.0.2", null); }//from w w w . j a va 2s. co m // ows:ServiceProvider if (sections.isEmpty() || sections.contains(Sections.ServiceProvider)) { exportServiceProvider100Old(writer, mainControllerConf.getServiceProvider()); } // ows:OperationsMetadata if (sections.isEmpty() || sections.contains(Sections.OperationsMetadata)) { exportOperationsMetadata(writer, OGCFrontController.getHttpGetURL(), OGCFrontController.getHttpPostURL(), OWS_NS); } // mandatory FilterCapabilitiesExporter.export110(writer); writer.writeEndElement(); writer.writeEndDocument(); }
From source file:org.deegree.services.csw.exporthandling.GetCapabilitiesHelper.java
private void writeTemplateElement(XMLStreamWriter writer, XMLStreamReader inStream, Map<String, String> varToValue) throws XMLStreamException { if (inStream.getEventType() != XMLStreamConstants.START_ELEMENT) { throw new XMLStreamException("Input stream does not point to a START_ELEMENT event."); }/* w w w .jav a 2 s . co m*/ int openElements = 0; boolean firstRun = true; while (firstRun || openElements > 0) { firstRun = false; int eventType = inStream.getEventType(); switch (eventType) { case CDATA: { writer.writeCData(inStream.getText()); break; } case CHARACTERS: { String s = new String(inStream.getTextCharacters(), inStream.getTextStart(), inStream.getTextLength()); // TODO optimize for (String param : varToValue.keySet()) { String value = varToValue.get(param); s = s.replace(param, value); } writer.writeCharacters(s); break; } case END_ELEMENT: { writer.writeEndElement(); openElements--; break; } case START_ELEMENT: { if (inStream.getNamespaceURI() == "" || inStream.getPrefix() == DEFAULT_NS_PREFIX || inStream.getPrefix() == null) { writer.writeStartElement(inStream.getLocalName()); } else { if (writer.getNamespaceContext().getPrefix(inStream.getPrefix()) == "") { // TODO handle special cases for prefix binding, see // http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/javax/xml/namespace/NamespaceContext.html#getNamespaceURI(java.lang.String) writer.setPrefix(inStream.getPrefix(), inStream.getNamespaceURI()); } writer.writeStartElement(inStream.getPrefix(), inStream.getLocalName(), inStream.getNamespaceURI()); } // copy all namespace bindings for (int i = 0; i < inStream.getNamespaceCount(); i++) { String nsPrefix = inStream.getNamespacePrefix(i); String nsURI = inStream.getNamespaceURI(i); writer.writeNamespace(nsPrefix, nsURI); } // copy all attributes for (int i = 0; i < inStream.getAttributeCount(); i++) { String localName = inStream.getAttributeLocalName(i); String nsPrefix = inStream.getAttributePrefix(i); String value = inStream.getAttributeValue(i); String nsURI = inStream.getAttributeNamespace(i); if (nsURI == null) { writer.writeAttribute(localName, value); } else { writer.writeAttribute(nsPrefix, nsURI, localName, value); } } openElements++; break; } default: { break; } } if (openElements > 0) { inStream.next(); } } }
From source file:org.deegree.services.sos.SOSController.java
private void doGetFeatureOfInterest(GetFeatureOfInterest foi, HttpResponseBuffer response) throws IOException, XMLStreamException { XMLStreamWriter xmlWriter = response.getXMLWriter(); List<String> foiIDs = Arrays.asList(foi.getFoiID()); xmlWriter.writeStartElement(SA_PREFIX, "SamplingFeatureCollection", SA_NS); xmlWriter.writeNamespace(SA_PREFIX, SA_NS); xmlWriter.writeNamespace(XSI_PREFIX, XSINS); xmlWriter.writeNamespace(XLINK_PREFIX, XLNNS); xmlWriter.writeNamespace(GML_PREFIX, GMLNS); xmlWriter.writeAttribute(XSI_PREFIX, XSINS, "schemaLocation", "http://www.opengis.net/sampling/1.0 http://schemas.opengis.net/sampling/1.0.0/sampling.xsd"); // TODO a url should be specified in the xlink:href of sampledFeature xmlWriter.writeEmptyElement(SA_PREFIX, "sampledFeature", SA_NS); for (Offering offering : sosService.getAllOfferings()) { for (Procedure procedure : offering.getProcedures()) { if (foiIDs.contains(procedure.getFeatureOfInterestHref())) { Geometry procGeometry = procedure.getLocation(); if (procGeometry instanceof Point) { // TODO check if the procedure can have some other geometries // and if so, // handle them xmlWriter.writeStartElement(SA_PREFIX, "member", SA_NS); xmlWriter.writeStartElement(SA_PREFIX, "SamplingPoint", SA_NS); xmlWriter.writeStartElement(GML_PREFIX, "name", GMLNS); xmlWriter.writeCharacters(procedure.getFeatureOfInterestHref()); // TODO if the GetFeatureOfInterest does not provide a foi but a location instead, search // for all // sensors // inside that BBOX xmlWriter.writeEndElement(); // TODO a url should be specified in the xlink:href of sampledFeature xmlWriter.writeEmptyElement(SA_PREFIX, "sampledFeature", SA_NS); xmlWriter.writeStartElement(SA_PREFIX, "position", SA_NS); // exporting a gml:Point TODO use GML encoder xmlWriter.writeStartElement(GML_PREFIX, "Point", GMLNS); // have the last part of the foiID as the Point id attribute String[] foiParts = procedure.getFeatureOfInterestHref().split(":"); xmlWriter.writeAttribute(GML_PREFIX, GMLNS, "id", foiParts[foiParts.length - 1]); xmlWriter.writeStartElement(GML_PREFIX, "pos", GMLNS); ICRS foiCRS = null;//from w w w. ja v a2 s.co m foiCRS = procGeometry.getCoordinateSystem(); xmlWriter.writeAttribute("srsName", foiCRS.getCode().toString()); Point p = (Point) procGeometry; xmlWriter.writeCharacters(p.get0() + " " + p.get1()); xmlWriter.writeEndElement(); // gml:pos xmlWriter.writeEndElement(); // gml:Point xmlWriter.writeEndElement(); // gml:position xmlWriter.writeEndElement(); // sa:SamplingPoint xmlWriter.writeEndElement(); // sa:member } } } } xmlWriter.writeEndElement(); // sa:SamplingFeatureCollection xmlWriter.writeEndDocument(); xmlWriter.flush(); }
From source file:org.deegree.services.wfs.WebFeatureService.java
@Override public void doSOAP(SOAPEnvelope soapDoc, HttpServletRequest request, HttpResponseBuffer response, List<FileItem> multiParts, SOAPFactory factory) throws ServletException, IOException, org.deegree.services.authentication.SecurityException { LOG.debug("doSOAP"); if (disableBuffering) { super.doSOAP(soapDoc, request, response, multiParts, factory); return;// w ww. ja va2 s .c o m } Version requestVersion = null; try { if (soapDoc.getVersion() instanceof SOAP11Version) { response.setContentType("application/soap+xml"); XMLStreamWriter xmlWriter = response.getXMLWriter(); String soapEnvNS = "http://schemas.xmlsoap.org/soap/envelope/"; String xsiNS = "http://www.w3.org/2001/XMLSchema-instance"; xmlWriter.writeStartElement("soap", "Envelope", soapEnvNS); xmlWriter.writeNamespace("soap", soapEnvNS); xmlWriter.writeNamespace("xsi", xsiNS); xmlWriter.writeAttribute(xsiNS, "schemaLocation", "http://schemas.xmlsoap.org/soap/envelope/ http://schemas.xmlsoap.org/soap/envelope/"); xmlWriter.writeStartElement(soapEnvNS, "Body"); } else { beginSOAPResponse(response); } OMElement body = soapDoc.getBody().getFirstElement().cloneOMElement(); XMLStreamReader bodyXmlStream = body.getXMLStreamReaderWithoutCaching(); String requestName = body.getLocalName(); WFSRequestType requestType = getRequestTypeByName(requestName); // check if requested version is supported and offered (except for GetCapabilities) requestVersion = getVersion(body.getAttributeValue(new QName("version"))); if (requestType != WFSRequestType.GetCapabilities) { requestVersion = checkVersion(requestVersion); // needed for CITE 1.1.0 compliance String serviceAttr = body.getAttributeValue(new QName("service")); if (serviceAttr != null && !("WFS".equals(serviceAttr) || "".equals(serviceAttr))) { throw new OWSException("Wrong service attribute: '" + serviceAttr + "' -- must be 'WFS'.", INVALID_PARAMETER_VALUE, "service"); } } switch (requestType) { case CreateStoredQuery: CreateStoredQueryXMLAdapter createStoredQueryAdapter = new CreateStoredQueryXMLAdapter(); createStoredQueryAdapter.setRootElement(body); CreateStoredQuery createStoredQuery = createStoredQueryAdapter.parse(); storedQueryHandler.doCreateStoredQuery(createStoredQuery, response); break; case DescribeFeatureType: DescribeFeatureTypeXMLAdapter describeFtAdapter = new DescribeFeatureTypeXMLAdapter(); describeFtAdapter.setRootElement(body); DescribeFeatureType describeFt = describeFtAdapter.parse(); Format format = determineFormat(requestVersion, describeFt.getOutputFormat(), "outputFormat"); format.doDescribeFeatureType(describeFt, response, true); break; case DropStoredQuery: DropStoredQueryXMLAdapter dropStoredQueryAdapter = new DropStoredQueryXMLAdapter(); dropStoredQueryAdapter.setRootElement(body); DropStoredQuery dropStoredQuery = dropStoredQueryAdapter.parse(); storedQueryHandler.doDropStoredQuery(dropStoredQuery, response); break; case DescribeStoredQueries: DescribeStoredQueriesXMLAdapter describeStoredQueriesAdapter = new DescribeStoredQueriesXMLAdapter(); describeStoredQueriesAdapter.setRootElement(body); DescribeStoredQueries describeStoredQueries = describeStoredQueriesAdapter.parse(); storedQueryHandler.doDescribeStoredQueries(describeStoredQueries, response); break; case GetCapabilities: GetCapabilitiesXMLAdapter getCapabilitiesAdapter = new GetCapabilitiesXMLAdapter(); getCapabilitiesAdapter.setRootElement(body); GetCapabilities wfsRequest = getCapabilitiesAdapter.parse(requestVersion); doGetCapabilities(wfsRequest, response); break; case GetFeature: GetFeatureXMLAdapter getFeatureAdapter = new GetFeatureXMLAdapter(); getFeatureAdapter.setRootElement(body); GetFeature getFeature = getFeatureAdapter.parse(); format = determineFormat(requestVersion, getFeature.getPresentationParams().getOutputFormat(), "outputFormat"); format.doGetFeature(getFeature, response); break; case GetFeatureWithLock: checkTransactionsEnabled(requestName); GetFeatureWithLockXMLAdapter getFeatureWithLockAdapter = new GetFeatureWithLockXMLAdapter(); getFeatureWithLockAdapter.setRootElement(body); GetFeatureWithLock getFeatureWithLock = getFeatureWithLockAdapter.parse(); format = determineFormat(requestVersion, getFeatureWithLock.getPresentationParams().getOutputFormat(), "outputFormat"); format.doGetFeature(getFeatureWithLock, response); break; case GetGmlObject: GetGmlObjectXMLAdapter getGmlObjectAdapter = new GetGmlObjectXMLAdapter(); getGmlObjectAdapter.setRootElement(body); GetGmlObject getGmlObject = getGmlObjectAdapter.parse(); format = determineFormat(requestVersion, getGmlObject.getOutputFormat(), "outputFormat"); format.doGetGmlObject(getGmlObject, response); break; case GetPropertyValue: GetPropertyValueXMLAdapter getPropertyValueAdapter = new GetPropertyValueXMLAdapter(); getPropertyValueAdapter.setRootElement(body); GetPropertyValue getPropertyValue = getPropertyValueAdapter.parse(); format = determineFormat(requestVersion, getPropertyValue.getPresentationParams().getOutputFormat(), "outputFormat"); format.doGetPropertyValue(getPropertyValue, response); break; case ListStoredQueries: ListStoredQueriesXMLAdapter listStoredQueriesAdapter = new ListStoredQueriesXMLAdapter(); listStoredQueriesAdapter.setRootElement(body); ListStoredQueries listStoredQueries = listStoredQueriesAdapter.parse(); storedQueryHandler.doListStoredQueries(listStoredQueries, response); break; case LockFeature: checkTransactionsEnabled(requestName); LockFeatureXMLAdapter lockFeatureAdapter = new LockFeatureXMLAdapter(); lockFeatureAdapter.setRootElement(body); LockFeature lockFeature = lockFeatureAdapter.parse(); lockFeatureHandler.doLockFeature(lockFeature, response); break; case Transaction: checkTransactionsEnabled(requestName); TransactionXmlReader transactionReader = new TransactionXmlReaderFactory() .createReader(requestVersion); Transaction transaction = transactionReader.read(bodyXmlStream); new TransactionHandler(this, service, transaction, idGenMode).doTransaction(response); break; default: throw new RuntimeException("Internal error: Unhandled request '" + requestName + "'."); } endSOAPResponse(response); } catch (OWSException e) { LOG.debug(e.getMessage(), e); sendSoapException(soapDoc, factory, response, e, request, requestVersion); } catch (XMLParsingException e) { LOG.trace("Stack trace:", e); sendSoapException(soapDoc, factory, response, new OWSException(e.getMessage(), INVALID_PARAMETER_VALUE), request, requestVersion); } catch (MissingParameterException e) { LOG.trace("Stack trace:", e); sendSoapException(soapDoc, factory, response, new OWSException(e), request, requestVersion); } catch (InvalidParameterValueException e) { LOG.trace("Stack trace:", e); sendSoapException(soapDoc, factory, response, new OWSException(e), request, requestVersion); } catch (Throwable e) { LOG.trace("Stack trace:", e); sendSoapException(soapDoc, factory, response, new OWSException(e.getMessage(), NO_APPLICABLE_CODE), request, requestVersion); } }
From source file:org.deegree.services.wps.execute.ExecuteResponseXMLWriter.java
/** * Exports an {@link ExecuteResponse} object as a WPS 1.0.0 ExecuteResponse document. * //w w w. j ava2 s .c o m * @param writer * writer where the XML is written to * @param response * object to be exported * @throws XMLStreamException */ public static void export100(XMLStreamWriter writer, ExecuteResponse response) throws XMLStreamException { // "wps:ExecuteResponse" (minOccurs="1", maxOccurs="1") writer.writeStartElement(WPS_PREFIX, "ExecuteResponse", WPS_NS); writer.writeNamespace(WPS_PREFIX, WPS_NS); writer.writeNamespace(OWS_PREFIX, OWS_NS); writer.writeNamespace(OGC_PREFIX, OGC_NS); writer.writeNamespace("xlink", XLN_NS); writer.writeNamespace("xsi", XSI_NS); writer.writeAttribute("service", "WPS"); writer.writeAttribute("version", "1.0.0"); writer.writeAttribute("xml:lang", "en"); writer.writeAttribute(XSI_NS, "schemaLocation", "http://www.opengis.net/wps/1.0.0 http://schemas.opengis.net/wps/1.0.0/wpsExecute_response.xsd"); // "serviceInstance" attribute (required) writer.writeAttribute("serviceInstance", response.getServiceInstance().toString()); // "statusLocation" attribute (optional) if (response.getStatusLocation() != null) { writer.writeAttribute("statusLocation", "" + response.getStatusLocation().getWebURL()); } // "wps:Process" element (minOccurs="1",maxOccurs="1") exportProcess(writer, response.getProcessDefinition()); // "wps:Status" element (minOccurs="1",maxOccurs="1") exportStatus(writer, response.getExecutionStatus()); // include inputs and output requests in the response? if (response.getLineage()) { // "wps:DataInputs" element (minOccurs="0",maxOccurs="1") exportDataInputs(writer, response); // "wps:OutputDefinitions" element (minOccurs="0",maxOccurs="1") exportOutputDefinitions(writer, response); } // "wps:ProcessOutputs" element (minOccurs="0",maxOccurs="1") if (response.getExecutionStatus().getExecutionState() == ExecutionState.SUCCEEDED) { // if process has finished successfully, include "wps:ProcessOutputs" element exportProcessOutputs(writer, response.getProcessOutputs(), response.getOutputDefinitions()); } writer.writeEndElement(); // ExecuteResponse }
From source file:org.deegree.tools.services.wms.FeatureTypesToLayerTree.java
/** * @param args/*ww w . ja v a 2s . com*/ */ public static void main(String[] args) { Options options = initOptions(); // for the moment, using the CLI API there is no way to respond to a help argument; see // https://issues.apache.org/jira/browse/CLI-179 if (args.length == 0 || (args.length > 0 && (args[0].contains("help") || args[0].contains("?")))) { CommandUtils.printHelp(options, FeatureTypesToLayerTree.class.getSimpleName(), null, null); } XMLStreamWriter out = null; try { CommandLine line = new PosixParser().parse(options, args); String storeFile = line.getOptionValue("f"); String nm = new File(storeFile).getName(); String storeId = nm.substring(0, nm.length() - 4); FileOutputStream os = new FileOutputStream(line.getOptionValue("o")); XMLOutputFactory fac = XMLOutputFactory.newInstance(); out = new IndentingXMLStreamWriter(fac.createXMLStreamWriter(os)); out.setDefaultNamespace(ns); Workspace ws = new DefaultWorkspace(new File("nix")); ws.initAll(); DefaultResourceIdentifier<FeatureStore> identifier = new DefaultResourceIdentifier<FeatureStore>( FeatureStoreProvider.class, "unknown"); ws.add(new DefaultResourceLocation<FeatureStore>(new File(storeFile), identifier)); ws.prepare(identifier); FeatureStore store = ws.init(identifier, null); AppSchema schema = store.getSchema(); // prepare document out.writeStartDocument(); out.writeStartElement(ns, "deegreeWMS"); out.writeDefaultNamespace(ns); out.writeAttribute("configVersion", "0.5.0"); out.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); out.writeAttribute("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", "http://www.deegree.org/services/wms http://schemas.deegree.org/wms/0.5.0/wms_configuration.xsd"); out.writeStartElement(ns, "ServiceConfiguration"); HashSet<FeatureType> visited = new HashSet<FeatureType>(); if (schema.getRootFeatureTypes().length == 1) { writeLayer(visited, out, schema.getRootFeatureTypes()[0], storeId); } else { out.writeCharacters("\n"); out.writeStartElement(ns, "UnrequestableLayer"); XMLAdapter.writeElement(out, ns, "Title", "Root Layer"); for (FeatureType ft : schema.getRootFeatureTypes()) { writeLayer(visited, out, ft, storeId); } out.writeEndElement(); out.writeCharacters("\n"); } out.writeEndElement(); out.writeEndElement(); out.writeEndDocument(); } catch (ParseException exp) { System.err.println(Messages.getMessage("TOOL_COMMANDLINE_ERROR", exp.getMessage())); CommandUtils.printHelp(options, FeatureTypesToLayerTree.class.getSimpleName(), null, null); } catch (ResourceInitException e) { LOG.info("The feature store could not be loaded: '{}'", e.getLocalizedMessage()); LOG.trace("Stack trace:", e); } catch (FileNotFoundException e) { LOG.info("A file could not be found: '{}'", e.getLocalizedMessage()); LOG.trace("Stack trace:", e); } catch (XMLStreamException e) { LOG.info("The XML output could not be written: '{}'", e.getLocalizedMessage()); LOG.trace("Stack trace:", e); } catch (FactoryConfigurationError e) { LOG.info("The XML system could not be initialized: '{}'", e.getLocalizedMessage()); LOG.trace("Stack trace:", e); } finally { if (out != null) { try { out.close(); } catch (XMLStreamException e) { LOG.trace("Stack trace:", e); } } } }
From source file:org.flowable.bpmn.converter.export.DefinitionsRootExport.java
@SuppressWarnings("unchecked") public static void writeRootElement(BpmnModel model, XMLStreamWriter xtw, String encoding) throws Exception { xtw.writeStartDocument(encoding, "1.0"); // start definitions root element xtw.writeStartElement(ELEMENT_DEFINITIONS); xtw.setDefaultNamespace(BPMN2_NAMESPACE); xtw.writeDefaultNamespace(BPMN2_NAMESPACE); xtw.writeNamespace(XSI_PREFIX, XSI_NAMESPACE); xtw.writeNamespace(XSD_PREFIX, SCHEMA_NAMESPACE); xtw.writeNamespace(FLOWABLE_EXTENSIONS_PREFIX, FLOWABLE_EXTENSIONS_NAMESPACE); xtw.writeNamespace(BPMNDI_PREFIX, BPMNDI_NAMESPACE); xtw.writeNamespace(OMGDC_PREFIX, OMGDC_NAMESPACE); xtw.writeNamespace(OMGDI_PREFIX, OMGDI_NAMESPACE); for (String prefix : model.getNamespaces().keySet()) { if (!defaultNamespaces.contains(prefix) && StringUtils.isNotEmpty(prefix)) xtw.writeNamespace(prefix, model.getNamespaces().get(prefix)); }// w w w . j a va2 s. c o m xtw.writeAttribute(TYPE_LANGUAGE_ATTRIBUTE, SCHEMA_NAMESPACE); xtw.writeAttribute(EXPRESSION_LANGUAGE_ATTRIBUTE, XPATH_NAMESPACE); if (StringUtils.isNotEmpty(model.getTargetNamespace())) { xtw.writeAttribute(TARGET_NAMESPACE_ATTRIBUTE, model.getTargetNamespace()); } else { xtw.writeAttribute(TARGET_NAMESPACE_ATTRIBUTE, PROCESS_NAMESPACE); } BpmnXMLUtil.writeCustomAttributes(model.getDefinitionsAttributes().values(), xtw, model.getNamespaces(), defaultAttributes); }
From source file:org.flowable.cmmn.converter.export.DefinitionsRootExport.java
public static void writeRootElement(CmmnModel model, XMLStreamWriter xtw, String encoding) throws Exception { xtw.writeStartDocument(encoding, "1.0"); // start definitions root element xtw.writeStartElement(ELEMENT_DEFINITIONS); xtw.setDefaultNamespace(CMMN_NAMESPACE); xtw.writeDefaultNamespace(CMMN_NAMESPACE); xtw.writeNamespace(XSI_PREFIX, XSI_NAMESPACE); xtw.writeNamespace(FLOWABLE_EXTENSIONS_PREFIX, FLOWABLE_EXTENSIONS_NAMESPACE); xtw.writeNamespace(CMMNDI_PREFIX, CMMNDI_NAMESPACE); xtw.writeNamespace(OMGDC_PREFIX, OMGDC_NAMESPACE); xtw.writeNamespace(OMGDI_PREFIX, OMGDI_NAMESPACE); for (String prefix : model.getNamespaces().keySet()) { if (!defaultNamespaces.contains(prefix) && StringUtils.isNotEmpty(prefix)) xtw.writeNamespace(prefix, model.getNamespaces().get(prefix)); }/*ww w . j a v a 2 s . c om*/ if (StringUtils.isNotEmpty(model.getTargetNamespace())) { xtw.writeAttribute(ATTRIBUTE_TARGET_NAMESPACE, model.getTargetNamespace()); } else { xtw.writeAttribute(ATTRIBUTE_TARGET_NAMESPACE, CASE_NAMESPACE); } }
From source file:org.flowable.cmmn.converter.util.CmmnXmlUtil.java
protected static void writeExtensionElement(ExtensionElement extensionElement, Map<String, String> namespaceMap, XMLStreamWriter xtw) throws Exception { if (StringUtils.isNotEmpty(extensionElement.getName())) { Map<String, String> localNamespaceMap = new HashMap<>(); if (StringUtils.isNotEmpty(extensionElement.getNamespace())) { if (StringUtils.isNotEmpty(extensionElement.getNamespacePrefix())) { xtw.writeStartElement(extensionElement.getNamespacePrefix(), extensionElement.getName(), extensionElement.getNamespace()); if (!namespaceMap.containsKey(extensionElement.getNamespacePrefix()) || !namespaceMap .get(extensionElement.getNamespacePrefix()).equals(extensionElement.getNamespace())) { xtw.writeNamespace(extensionElement.getNamespacePrefix(), extensionElement.getNamespace()); namespaceMap.put(extensionElement.getNamespacePrefix(), extensionElement.getNamespace()); localNamespaceMap.put(extensionElement.getNamespacePrefix(), extensionElement.getNamespace()); }//from w ww. ja v a2 s . c o m } else { xtw.writeStartElement(extensionElement.getNamespace(), extensionElement.getName()); } } else { xtw.writeStartElement(extensionElement.getName()); } for (List<ExtensionAttribute> attributes : extensionElement.getAttributes().values()) { for (ExtensionAttribute attribute : attributes) { if (StringUtils.isNotEmpty(attribute.getName()) && attribute.getValue() != null) { if (StringUtils.isNotEmpty(attribute.getNamespace())) { if (StringUtils.isNotEmpty(attribute.getNamespacePrefix())) { if (!namespaceMap.containsKey(attribute.getNamespacePrefix()) || !namespaceMap .get(attribute.getNamespacePrefix()).equals(attribute.getNamespace())) { xtw.writeNamespace(attribute.getNamespacePrefix(), attribute.getNamespace()); namespaceMap.put(attribute.getNamespacePrefix(), attribute.getNamespace()); } xtw.writeAttribute(attribute.getNamespacePrefix(), attribute.getNamespace(), attribute.getName(), attribute.getValue()); } else { xtw.writeAttribute(attribute.getNamespace(), attribute.getName(), attribute.getValue()); } } else { xtw.writeAttribute(attribute.getName(), attribute.getValue()); } } } } if (extensionElement.getElementText() != null) { xtw.writeCData(extensionElement.getElementText()); } else { for (List<ExtensionElement> childElements : extensionElement.getChildElements().values()) { for (ExtensionElement childElement : childElements) { writeExtensionElement(childElement, namespaceMap, xtw); } } } for (String prefix : localNamespaceMap.keySet()) { namespaceMap.remove(prefix); } xtw.writeEndElement(); } }
From source file:org.flowable.cmmn.converter.util.CmmnXmlUtil.java
/** * write attributes to xtw (except blacklisted) * * @param attributes/*ww w . j a va 2s. c o m*/ * @param xtw * @param namespaceMap * @param blackLists */ public static void writeCustomAttributes(Collection<List<ExtensionAttribute>> attributes, XMLStreamWriter xtw, Map<String, String> namespaceMap, List<ExtensionAttribute>... blackLists) throws XMLStreamException { for (List<ExtensionAttribute> attributeList : attributes) { if (attributeList != null && !attributeList.isEmpty()) { for (ExtensionAttribute attribute : attributeList) { if (!isBlacklisted(attribute, blackLists)) { if (attribute.getNamespacePrefix() == null) { if (attribute.getNamespace() == null) xtw.writeAttribute(attribute.getName(), attribute.getValue()); else { xtw.writeAttribute(attribute.getNamespace(), attribute.getName(), attribute.getValue()); } } else { if (!namespaceMap.containsKey(attribute.getNamespacePrefix())) { namespaceMap.put(attribute.getNamespacePrefix(), attribute.getNamespace()); xtw.writeNamespace(attribute.getNamespacePrefix(), attribute.getNamespace()); } xtw.writeAttribute(attribute.getNamespacePrefix(), attribute.getNamespace(), attribute.getName(), attribute.getValue()); } } } } } }