Java Code Examples for org.apache.xmlbeans.XmlObject#newCursor()
The following examples show how to use
org.apache.xmlbeans.XmlObject#newCursor() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: N52XmlHelper.java From arctic-sea with Apache License 2.0 | 6 votes |
public static Set<String> getNamespaces(XmlObject xmlObject) { Set<String> namespaces = Sets.newHashSet(); XmlCursor newCursor = xmlObject.newCursor(); while (newCursor.hasNextToken()) { TokenType evt = newCursor.toNextToken(); if (evt == TokenType.START) { QName qn = newCursor.getName(); if (qn != null) { namespaces.add(qn.getNamespaceURI()); } } else if (evt == TokenType.NAMESPACE) { namespaces.add(newCursor.getName().getNamespaceURI()); } } return namespaces; }
Example 2
Source File: XMLLibImpl.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Escapes the reserved characters in a value of a text node * * @param value Unescaped text * @return The escaped text */ public String escapeTextValue(Object value) { if (value instanceof XMLObjectImpl) { return ((XMLObjectImpl)value).toXMLString(0); } String text = ScriptRuntime.toString(value); if (text.length() == 0) return text; XmlObject xo = XmlObject.Factory.newInstance(); XmlCursor cursor = xo.newCursor(); cursor.toNextToken(); cursor.beginElement("a"); cursor.insertChars(text); cursor.dispose(); String elementText = xo.toString(); int begin = elementText.indexOf('>') + 1; int end = elementText.lastIndexOf('<'); return (begin < end) ? elementText.substring(begin, end) : ""; }
Example 3
Source File: XmlHelper.java From arctic-sea with Apache License 2.0 | 6 votes |
/** * Utility method to append the contents of the child docment to the end of * the parent XmlObject. This is useful when dealing with elements without * generated methods (like elements with xs:any) * * @param parent * Parent to append contents to * @param childDoc * Xml document containing contents to be appended */ public static void append(final XmlObject parent, final XmlObject childDoc) { final XmlCursor parentCursor = parent.newCursor(); parentCursor.toEndToken(); final XmlCursor childCursor = childDoc.newCursor(); childCursor.toFirstChild(); childCursor.moveXml(parentCursor); parentCursor.dispose(); childCursor.dispose(); }
Example 4
Source File: XmlHelper.java From arctic-sea with Apache License 2.0 | 6 votes |
public static void fixNamespaceForXsiType(XmlObject content, Map<?, ?> namespaces) { final XmlCursor cursor = content.newCursor(); while (cursor.hasNextToken()) { if (cursor.toNextToken().isStart()) { final String xsiType = cursor.getAttributeText(W3CConstants.QN_XSI_TYPE); if (xsiType != null) { final String[] toks = xsiType.split(":"); if (toks.length > 1) { String prefix = toks[0]; String localName = toks[1]; if (namespaces.containsKey(prefix)) { cursor.setAttributeText( W3CConstants.QN_XSI_TYPE, Joiner.on(":").join( XmlHelper.getPrefixForNamespace(content, (String) namespaces.get(prefix)), localName)); } } } } } cursor.dispose(); }
Example 5
Source File: XML.java From astor with GNU General Public License v2.0 | 6 votes |
static XML createFromXmlObject(XMLLibImpl lib, XmlObject xo) { XScriptAnnotation anno; XmlCursor curs = xo.newCursor(); if (curs.currentTokenType().isStartdoc()) { curs.toFirstContentToken(); } try { anno = new XScriptAnnotation(curs); curs.setBookmark(anno); } finally { curs.dispose(); } return new XML(lib, anno); }
Example 6
Source File: XMLLibImpl.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Escapes the reserved characters in a value of an attribute * * @param value Unescaped text * @return The escaped text */ public String escapeAttributeValue(Object value) { String text = ScriptRuntime.toString(value); if (text.length() == 0) return ""; XmlObject xo = XmlObject.Factory.newInstance(); XmlCursor cursor = xo.newCursor(); cursor.toNextToken(); cursor.beginElement("a"); cursor.insertAttributeWithValue("a", text); cursor.dispose(); String elementText = xo.toString(); int begin = elementText.indexOf('"'); int end = elementText.lastIndexOf('"'); return elementText.substring(begin + 1, end); }
Example 7
Source File: AbstractOmV20XmlStreamWriter.java From arctic-sea with Apache License 2.0 | 5 votes |
/** * write om:result to stream * * @throws XMLStreamException * If an error occurs when writing to stream * @throws EncodingException * If an error occurs when creating elements to be written */ protected void writeResult() throws XMLStreamException, EncodingException { OmObservation observation = getElement(); if (observation.getValue() instanceof AbstractObservationValue<?>) { ((AbstractObservationValue<?>) observation.getValue()).setValuesForResultEncoding(observation); } XmlObject createResult = (XmlObject) getEncoder(getEncodeNamespace().orElse(OmConstants.NS_OM_2), observation.getValue()).encode(observation.getValue()); if (createResult != null) { if (createResult.xmlText().contains(XML_FRAGMENT)) { XmlObject set = OMObservationType.Factory.newInstance(getXmlOptions()).addNewResult().set(createResult); writeXmlObject(set, OmConstants.QN_OM_20_RESULT); } else { if (checkResult(createResult)) { QName name = createResult.schemaType().getName(); String prefix = name.getPrefix(); if (Strings.isNullOrEmpty(prefix)) { XmlCursor newCursor = createResult.newCursor(); prefix = newCursor.prefixForNamespace(name.getNamespaceURI()); newCursor.setAttributeText(W3CConstants.QN_XSI_TYPE, prefix + ":" + name.getLocalPart()); newCursor.dispose(); } writeXmlObject(createResult, OmConstants.QN_OM_20_RESULT); } else { start(OmConstants.QN_OM_20_RESULT); writeXmlObject(createResult, OmConstants.QN_OM_20_RESULT); end(OmConstants.QN_OM_20_RESULT); } } } else { empty(OmConstants.QN_OM_20_RESULT); } }
Example 8
Source File: AbstractSwesXmlStreamWriter.java From arctic-sea with Apache License 2.0 | 5 votes |
protected void writeExtension(SweAbstractDataComponent sweAbstractDataComponent) throws EncodingException, XMLStreamException { EncodingContext ctx = EncodingContext.of(XmlBeansEncodingFlags.PROPERTY_TYPE, "true"); XmlObject extension = encodeSwe(ctx, sweAbstractDataComponent); if (extension.xmlText().contains(XML_FRAGMENT)) { XmlObject set = ExtensibleResponseType.Factory.newInstance(getXmlOptions()) .addNewExtension().set(extension); writeXmlObject(set, SwesStreamingConstants.QN_EXTENSION); } else { if (checkExtension(extension)) { QName name = extension.schemaType().getName(); String prefix = name.getPrefix(); if (Strings.isNullOrEmpty(prefix)) { XmlCursor newCursor = extension.newCursor(); prefix = newCursor.prefixForNamespace(name.getNamespaceURI()); newCursor.setAttributeText(W3CConstants.QN_XSI_TYPE, prefix + ":" + name.getLocalPart()); newCursor.dispose(); } writeXmlObject(extension, SwesStreamingConstants.QN_EXTENSION); } else { start(SwesStreamingConstants.QN_EXTENSION); writeXmlObject(extension, SwesStreamingConstants.QN_EXTENSION); end(SwesStreamingConstants.QN_EXTENSION); } } }
Example 9
Source File: DhlXTeeServiceImpl.java From j-road with Apache License 2.0 | 5 votes |
private XRoadAttachment setDokumendidHrefToAttachment(byte[] base64Attachment, XmlObject request) { final String cid = AttachmentUtil.getUniqueCid(); final XRoadAttachment attachment = new XRoadAttachment(cid, "{http://www.w3.org/2001/XMLSchema}base64Binary", base64Attachment); XmlCursor cursor = request.newCursor(); cursor.toNextToken(); Element node = (Element) cursor.getDomNode(); node.setAttribute("href", "cid:" + cid); cursor.dispose(); return attachment; }
Example 10
Source File: XmlHelper.java From arctic-sea with Apache License 2.0 | 5 votes |
/** * Remove namespace declarations from an xml fragment (useful for moving all * declarations to a document root * * @param x * The fragment to localize */ public static void removeNamespaces(final XmlObject x) { final XmlCursor c = x.newCursor(); while (c.hasNextToken()) { if (c.isNamespace()) { c.removeXml(); } else { c.toNextToken(); } } c.dispose(); }
Example 11
Source File: XmlPath.java From mdw with Apache License 2.0 | 5 votes |
public static String evaluate(XmlObject xmlbean, String path) { XmlCursor cursor = xmlbean.newCursor(); String value; try { XmlPath matcher = new XmlPath(path); value = matcher.evaluate_segment(cursor, matcher.path_seg); } catch (XmlException e) { value = null; // xpath syntax error - treated as no match } cursor.dispose(); return value; }
Example 12
Source File: DhlXTeeService.java From j-road with Apache License 2.0 | 5 votes |
protected Map<Class<? extends XmlObject>, List<? extends XmlObject>> parse(XmlObject xmlObject) { if (log.isTraceEnabled()) { log.trace("starting to parse xmlObject:\n" + xmlObject + "\n\n"); } XmlCursor cursor = xmlObject.newCursor(); cursor.toFirstChild();// move before the root element cursor.toFirstChild();// move to the first child of the root Map<Class<? extends XmlObject>, List<? extends XmlObject>> subElementsList = new HashMap<Class<? extends XmlObject>, List<? extends XmlObject>>(); try { do { XmlObject object = XmlObject.Factory.parse(cursor.getDomNode()); @SuppressWarnings("unchecked") // to be able to add object that is in fact a subclass of XmlObject, but referenced using XmlObject List<XmlObject> list = (List<XmlObject>) subElementsList.get(object.getClass()); if (list == null) { list = new ArrayList<XmlObject>(); } list.add(object); subElementsList.put(object.getClass(), list); log.debug("adding node: '" + cursor.getDomNode().getLocalName() + "' (" + cursor.getDomNode().getNamespaceURI() + "),\nclass: " + object.getClass() + ", cursor: \n" + object); } while (cursor.toNextSibling()); } catch (XmlException e) { throw new RuntimeException("Failed to parse xmlObject:\n" + xmlObject, e); } return subElementsList; }
Example 13
Source File: XmlPath.java From mdw with Apache License 2.0 | 4 votes |
public static String getRootNodeValue(XmlObject xmlbean) { XmlCursor cursor = xmlbean.newCursor(); cursor.toFirstChild(); return cursor.getTextValue(); }
Example 14
Source File: XML.java From astor with GNU General Public License v2.0 | 4 votes |
/** * * @param curs * @param xmlToInsert */ private void insertChild(XmlCursor curs, Object xmlToInsert) { if (xmlToInsert == null || xmlToInsert instanceof Undefined) { // Do nothing } else if (xmlToInsert instanceof XmlCursor) { moveSrcToDest((XmlCursor)xmlToInsert, curs, true); } else if (xmlToInsert instanceof XML) { XML xmlValue = (XML) xmlToInsert; // If it's an attribute, then change to text node if (xmlValue.tokenType() == XmlCursor.TokenType.ATTR) { insertChild(curs, xmlValue.toString()); } else { XmlCursor cursToInsert = ((XML) xmlToInsert).newCursor(); moveSrcToDest(cursToInsert, curs, true); cursToInsert.dispose(); } } else if (xmlToInsert instanceof XMLList) { XMLList list = (XMLList) xmlToInsert; for (int i = 0; i < list.length(); i++) { insertChild(curs, list.item(i)); } } else { // Convert to string and make XML out of it String xmlStr = ScriptRuntime.toString(xmlToInsert); XmlObject xo = XmlObject.Factory.newInstance(); // Create an empty document. XmlCursor sourceCurs = xo.newCursor(); sourceCurs.toNextToken(); // To hold the text. sourceCurs.insertChars(xmlStr); sourceCurs.toPrevToken(); // Call us again with the cursor. moveSrcToDest(sourceCurs, curs, true); } }
Example 15
Source File: XmlPath.java From mdw with Apache License 2.0 | 4 votes |
public static String getRootNodeName(XmlObject xmlbean) { XmlCursor cursor = xmlbean.newCursor(); cursor.toFirstChild(); return cursor.getName().getLocalPart(); }
Example 16
Source File: PdfGenerator.java From SensorWebClient with GNU General Public License v2.0 | 4 votes |
private MetadataType buildUpMetadata(String sosURL, String procedureID) throws Exception { SOSMetadata metadata = ConfigurationContext.getSOSMetadata(sosURL); String sosVersion = metadata.getSosVersion(); String smlVersion = metadata.getSensorMLVersion(); ParameterContainer paramCon = new ParameterContainer(); paramCon.addParameterShell(ISOSRequestBuilder.DESCRIBE_SENSOR_SERVICE_PARAMETER, "SOS"); paramCon.addParameterShell(ISOSRequestBuilder.DESCRIBE_SENSOR_VERSION_PARAMETER, sosVersion); paramCon.addParameterShell(ISOSRequestBuilder.DESCRIBE_SENSOR_PROCEDURE_PARAMETER, procedureID); if (SosUtil.isVersion100(sosVersion)) { paramCon.addParameterShell(ISOSRequestBuilder.DESCRIBE_SENSOR_OUTPUT_FORMAT, smlVersion); } else if (SosUtil.isVersion200(sosVersion)) { paramCon.addParameterShell(ISOSRequestBuilder.DESCRIBE_SENSOR_PROCEDURE_DESCRIPTION_FORMAT, smlVersion); } else { throw new IllegalStateException("SOS Version (" + sosVersion + ") is not supported!"); } Operation descSensorOperation = new Operation(SOSAdapter.DESCRIBE_SENSOR, sosURL, sosURL); SOSAdapter adapter = SosAdapterFactory.createSosAdapter(metadata); OperationResult opResult = adapter.doOperation(descSensorOperation, paramCon); // parse resulting SensorML doc and store information in the // MetadataType object: XmlOptions xmlOpts = new XmlOptions(); xmlOpts.setCharacterEncoding(ENCODING); XmlObject xmlObject = XmlObject.Factory.parse(opResult.getIncomingResultAsStream(), xmlOpts); MetadataType metadataType = MetadataType.Factory.newInstance(); String namespaceDecl = "declare namespace sml='http://www.opengis.net/sensorML/1.0'; "; //$NON-NLS-1$ for (XmlObject termObj : xmlObject.selectPath(namespaceDecl + "$this//sml:Term")) { //$NON-NLS-1$ String attributeVal = termObj.selectAttribute(new QName("definition")).newCursor() //$NON-NLS-1$ .getTextValue(); String name = null; String value; if (attributeVal.equals("urn:ogc:identifier:stationName")) { name = "Station"; //$NON-NLS-1$ } if (attributeVal.equals("urn:ogc:identifier:operator")) { name = "Operator"; //$NON-NLS-1$ } if (attributeVal.equals("urn:ogc:identifier:stationID")) { name = "ID"; //$NON-NLS-1$ } if (attributeVal.equals("urn:ogc:identifier:sensorType")) { name = "Sensor"; //$NON-NLS-1$ } XmlCursor cursor = termObj.newCursor(); cursor.toChild("value"); //$NON-NLS-1$ value = cursor.getTextValue(); if (name != null) { GenericMetadataPair genMetaPair = metadataType.addNewGenericMetadataPair(); genMetaPair.setName(name); genMetaPair.setValue(value); } } return metadataType; }
Example 17
Source File: DescribeSensorParser.java From SensorWebClient with GNU General Public License v2.0 | 4 votes |
public static SensorMLDocument unwrapSensorMLFrom(XmlObject xmlObject) throws XmlException, XMLHandlingException, IOException { if (SoapUtil.isSoapEnvelope(xmlObject)) { xmlObject = SoapUtil.stripSoapEnvelope(xmlObject); } if (xmlObject instanceof SensorMLDocument) { return (SensorMLDocument) xmlObject; } if (xmlObject instanceof DescribeSensorResponseDocument) { DescribeSensorResponseDocument responseDoc = (DescribeSensorResponseDocument) xmlObject; DescribeSensorResponseType response = responseDoc.getDescribeSensorResponse(); DescribeSensorResponseType.Description[] descriptionArray = response.getDescriptionArray(); if (descriptionArray.length == 0) { LOGGER.warn("No SensorDescription available in response!"); } else { for (DescribeSensorResponseType.Description description : descriptionArray) { SensorDescriptionType.Data dataDescription = description.getSensorDescription().getData(); String namespace = "declare namespace gml='http://www.opengis.net/gml'; "; for (XmlObject xml : dataDescription.selectPath(namespace + "$this//*/@gml:id")) { XmlCursor cursor = xml.newCursor(); String gmlId = cursor.getTextValue(); if ( !NcNameResolver.isNCName(gmlId)) { cursor.setTextValue(NcNameResolver.fixNcName(gmlId)); } } XmlObject object = XmlObject.Factory.parse(dataDescription.xmlText()); if (object instanceof SystemDocumentImpl) { SensorMLDocument smlDoc = SensorMLDocument.Factory.newInstance(); SensorMLDocument.SensorML.Member member = smlDoc.addNewSensorML().addNewMember(); member.set(XMLBeansParser.parse(object.newInputStream())); return smlDoc; } return SensorMLDocument.Factory.parse(dataDescription.newInputStream()); } } } LOGGER.warn("Failed to unwrap SensorML from '{}'. Return an empty description.", xmlObject.xmlText()); return SensorMLDocument.Factory.newInstance(); }
Example 18
Source File: SosDecoderv100.java From arctic-sea with Apache License 2.0 | 4 votes |
@Override public OwsServiceCommunicationObject decode(XmlObject xmlObject) throws DecodingException { OwsServiceCommunicationObject request = null; LOGGER.debug("REQUESTTYPE:" + xmlObject.getClass()); /* * Add O&M 1.0.0 namespace to GetObservation document. XmlBeans removes * the namespace from the document because there are no om:... elements * in the document. But the validation fails if the <resultModel> * element is set with e.g. om:Measurement. */ if (xmlObject instanceof GetObservationDocument) { XmlCursor cursor = xmlObject.newCursor(); cursor.toFirstChild(); cursor.insertNamespace(OmConstants.NS_OM_PREFIX, OmConstants.NS_OM); cursor.dispose(); } // validate document XmlHelper.validateDocument(xmlObject); if (xmlObject instanceof GetCapabilitiesDocument) { // getCapabilities request GetCapabilitiesDocument getCapsDoc = (GetCapabilitiesDocument) xmlObject; request = parseGetCapabilities(getCapsDoc); } else if (xmlObject instanceof DescribeSensorDocument) { // DescribeSensor request (still SOS 1.0 NS_URI DescribeSensorDocument descSensorDoc = (DescribeSensorDocument) xmlObject; request = parseDescribeSensor(descSensorDoc); } else if (xmlObject instanceof GetObservationDocument) { // getObservation request GetObservationDocument getObsDoc = (GetObservationDocument) xmlObject; request = parseGetObservation(getObsDoc); } else if (xmlObject instanceof GetFeatureOfInterestDocument) { // getFeatureOfInterest request GetFeatureOfInterestDocument getFoiDoc = (GetFeatureOfInterestDocument) xmlObject; request = parseGetFeatureOfInterest(getFoiDoc); } else if (xmlObject instanceof GetObservationByIdDocument) { // getObservationById request GetObservationByIdDocument getObsByIdDoc = (GetObservationByIdDocument) xmlObject; request = parseGetObservationById(getObsByIdDoc); } else { throw new UnsupportedDecoderXmlInputException(this, xmlObject); } return request; }
Example 19
Source File: XmlHelper.java From arctic-sea with Apache License 2.0 | 3 votes |
/** * Remove the element from XML document * * @param element * Element to remove * @return <code>true</code>, if element is removed */ public static boolean removeElement(XmlObject element) { XmlCursor cursor = element.newCursor(); boolean removed = cursor.removeXml(); cursor.dispose(); return removed; }
Example 20
Source File: N52XmlHelper.java From arctic-sea with Apache License 2.0 | 3 votes |
/** * Sets the schema location to a XmlObject * * @param document * XML document * @param schemaLocations * schema location */ public static void setSchemaLocationToDocument(XmlObject document, String schemaLocations) { XmlCursor cursor = document.newCursor(); if (cursor.toFirstChild()) { cursor.setAttributeText(getSchemaLocationQName(), schemaLocations); } cursor.dispose(); }