protected void write(OutputStream out) throws IOException { XmlObject rootObject = XmlObject.Factory.newInstance(); XmlCursor rootCursor = rootObject.newCursor(); rootCursor.toNextToken(); rootCursor.beginElement("xml"); for(int i=0; i < _items.size(); i++){ XmlCursor xc = _items.get(i).newCursor(); rootCursor.beginElement(_qnames.get(i)); while(xc.toNextToken() == XmlCursor.TokenType.ATTR) { Node anode = xc.getDomNode(); rootCursor.insertAttributeWithValue(anode.getLocalName(), anode.getNamespaceURI(), anode.getNodeValue()); } xc.toStartDoc(); xc.copyXmlContents(rootCursor); rootCursor.toNextToken(); xc.dispose(); } rootCursor.dispose(); rootObject.save(out, DEFAULT_XML_OPTIONS); }
private XmlObject downloadSensorDescription(String uri) { try { SimpleHttpClient sensorDescriptionDownload = new SimpleHttpClient(); HttpEntity entity = sensorDescriptionDownload.executeGetMethod(uri); if (entity != null) { InputStream inputStream = entity.getContent(); try { return XmlObject.Factory.parse(inputStream); } catch (XmlException e) { LOGGER.error("Could not parse SensorDescription download for procedure '{}'.", procedure, e); } finally { inputStream.close(); } } } catch (ClientProtocolException e) { LOGGER.error("Could not download sensor description for procedure '{}'.", procedure, e); } catch (IOException e) { LOGGER.error("Could not read sensor description for procedure '{}'.", procedure, e); } return XmlObject.Factory.newInstance(); } }
@Override public GenericXMLDataBinding parse(InputStream input, String mimeType, String schema) { XmlObject xmlData = XmlObject.Factory.newInstance(); try { xmlData = XmlObject.Factory.parse(input); } catch (XmlException e) { LOGGER.error("Could not parse inputstream as XMLObject.", e); } catch (IOException e) { LOGGER.error("Could not parse inputstream as XMLObject.", e); } GenericXMLDataBinding xmlDataBinding = new GenericXMLDataBinding(xmlData); return xmlDataBinding; }
XmlObject doc = XmlObject.Factory.newInstance(); curs = doc.newCursor(); XmlObject doc = XmlObject.Factory.newInstance(); curs = doc.newCursor();
XmlObject obj = XmlObject.Factory.newInstance();
/** * 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) : ""; }
protected void write(OutputStream out) throws IOException { XmlObject rootObject = XmlObject.Factory.newInstance(); XmlCursor rootCursor = rootObject.newCursor(); rootCursor.toNextToken(); rootCursor.beginElement("xml"); for(int i=0; i < _items.size(); i++){ XmlCursor xc = _items.get(i).newCursor(); rootCursor.beginElement(_qnames.get(i)); while(xc.toNextToken() == XmlCursor.TokenType.ATTR) { Node anode = xc.getDomNode(); rootCursor.insertAttributeWithValue(anode.getLocalName(), anode.getNamespaceURI(), anode.getNodeValue()); } xc.toStartDoc(); xc.copyXmlContents(rootCursor); rootCursor.toNextToken(); xc.dispose(); } rootCursor.dispose(); XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS); xmlOptions.setSavePrettyPrint(); HashMap<String, String> map = new HashMap<String, String>(); map.put("urn:schemas-microsoft-com:vml", "v"); map.put("urn:schemas-microsoft-com:office:office", "o"); map.put("urn:schemas-microsoft-com:office:excel", "x"); xmlOptions.setSaveSuggestedPrefixes(map); rootObject.save(out, xmlOptions); }
private Persistence fromXmlObject(XmlObject xmlObject) throws JAXBException { /* * To avoid illegal exception in JAXB. Convert * * <xml-fragment> ... </xml-fragment> * to * <persistence xmlns="http://java.sun.com/xml/ns/persistence"> ... </persistence> * * before unmarshalling it */ XmlObject newXmlObject=XmlObject.Factory.newInstance(); XmlCursor newXmlCursor=newXmlObject.newCursor(); newXmlCursor.toNextToken(); newXmlCursor.beginElement(PERSISTENCE_QNAME); XmlCursor oldXmlCursor=xmlObject.newCursor(); oldXmlCursor.copyXmlContents(newXmlCursor); oldXmlCursor.dispose(); newXmlCursor.dispose(); XMLStreamReader reader = newXmlObject.newXMLStreamReader(); JAXBContext context = JAXBContextFactory.newInstance(Persistence.class); Unmarshaller unmarshaller = context.createUnmarshaller(); return (Persistence) unmarshaller.unmarshal(reader); }
/** * * @param qname * @param value * @return */ static XML createTextElement(XMLLibImpl lib, javax.xml.namespace.QName qname, String value) { XScriptAnnotation anno; XmlObject xo = XmlObject.Factory.newInstance(); XmlCursor cursor = xo.newCursor(); try { cursor.toNextToken(); cursor.beginElement(qname.getLocalPart(), qname.getNamespaceURI()); //if(namespace.length() > 0) // cursor.insertNamespace("", namespace); cursor.insertChars(value); cursor.toStartDoc(); cursor.toNextToken(); anno = new XScriptAnnotation(cursor); cursor.setBookmark(anno); } finally { cursor.dispose(); } return new XML(lib, anno); }
/** * 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); }
public static String createSampleForType(SchemaType sType) { XmlObject object = XmlObject.Factory.newInstance(); XmlCursor cursor = object.newCursor(); // Skip the document node cursor.toNextToken(); // Using the type and the cursor, call the utility method to get a // sample XML payload for that Schema element new SampleXmlUtil(false).createSampleForType(sType, cursor); // Cursor now contains the sample payload // Pretty print the result. Note that the cursor is positioned at the // end of the doc so we use the original xml object that the cursor was // created upon to do the xmlText() against. XmlOptions options = new XmlOptions(); options.put(XmlOptions.SAVE_PRETTY_PRINT); options.put(XmlOptions.SAVE_PRETTY_PRINT_INDENT, 2); options.put(XmlOptions.SAVE_AGGRESSIVE_NAMESPACES); String result = object.xmlText(options); return result; }
protected void write(OutputStream out) throws IOException { XmlObject rootObject = XmlObject.Factory.newInstance(); XmlCursor rootCursor = rootObject.newCursor(); rootCursor.toNextToken(); rootCursor.beginElement("xml"); for(int i=0; i < _items.size(); i++){ XmlCursor xc = _items.get(i).newCursor(); rootCursor.beginElement(_qnames.get(i)); while(xc.toNextToken() == XmlCursor.TokenType.ATTR) { Node anode = xc.getDomNode(); rootCursor.insertAttributeWithValue(anode.getLocalName(), anode.getNamespaceURI(), anode.getNodeValue()); } xc.toStartDoc(); xc.copyXmlContents(rootCursor); rootCursor.toNextToken(); xc.dispose(); } rootCursor.dispose(); rootObject.save(out, DEFAULT_XML_OPTIONS); }
public static String createSampleForType(SchemaType sType) { XmlObject object = XmlObject.Factory.newInstance(); XmlCursor cursor = object.newCursor(); // Skip the document node cursor.toNextToken(); // Using the type and the cursor, call the utility method to get a // sample XML payload for that Schema element new SampleXmlUtil(false).createSampleForType(sType, cursor); // Cursor now contains the sample payload // Pretty print the result. Note that the cursor is positioned at the // end of the doc so we use the original xml object that the cursor was // created upon to do the xmlText() against. XmlOptions options = new XmlOptions(); options.put(XmlOptions.SAVE_PRETTY_PRINT); options.put(XmlOptions.SAVE_PRETTY_PRINT_INDENT, 2); options.put(XmlOptions.SAVE_AGGRESSIVE_NAMESPACES); String result = object.xmlText(options); return result; }
private XmlObject ensureStore() { if ((_flags & FLAG_STORE) != 0) return this; check_dated(); String value = (_flags & FLAG_NIL) != 0 ? "" : compute_text( has_store() ? get_store() : null ); XmlOptions options = new XmlOptions().setDocumentType(schemaType()); XmlObject x = XmlObject.Factory.newInstance( options ); XmlCursor c = x.newCursor(); c.toNextToken(); c.insertChars( value ); return x; }
private XmlObject ensureStore() { if ((_flags & FLAG_STORE) != 0) return this; check_dated(); String value = (_flags & FLAG_NIL) != 0 ? "" : compute_text( has_store() ? get_store() : null ); XmlOptions options = new XmlOptions().setDocumentType(schemaType()); XmlObject x = XmlObject.Factory.newInstance( options ); XmlCursor c = x.newCursor(); c.toNextToken(); c.insertChars( value ); return x; }
@Override public InputStream generateStream(IData data, String mimeType, String schema) throws IOException { if(data instanceof GenericXMLDataBinding){ XmlObject xmlData = ((GenericXMLDataBinding)data).getPayload(); XmlOptions xmlOptions = new XmlOptions(); xmlOptions.setSaveNoXmlDecl(); return xmlData.newInputStream(xmlOptions); } return XmlObject.Factory.newInstance().newInputStream(); }
/** * * @param curs */ protected void removeToken (XmlCursor curs) { XmlObject xo = XmlObject.Factory.newInstance(); // Don't delete anything move to another document so it gets orphaned nicely. XmlCursor tmpCurs = xo.newCursor(); tmpCurs.toFirstContentToken(); curs.moveXml(tmpCurs); tmpCurs.dispose(); }
static XML createEmptyXML(XMLLibImpl lib) { XScriptAnnotation anno; XmlObject xo = XmlObject.Factory.newInstance(); XmlCursor curs = xo.newCursor(); try { anno = new XScriptAnnotation(curs); curs.setBookmark(anno); } finally { curs.dispose(); } return new XML(lib, anno); }
public static String createSampleForType(SchemaType sType) { XmlObject object = XmlObject.Factory.newInstance(); XmlCursor cursor = object.newCursor(); // Skip the document node cursor.toNextToken(); // Using the type and the cursor, call the utility method to get a // sample XML payload for that Schema element new SampleXmlUtil(false).createSampleForType(sType, cursor); // Cursor now contains the sample payload // Pretty print the result. Note that the cursor is positioned at the // end of the doc so we use the original xml object that the cursor was // created upon to do the xmlText() against. XmlOptions options = new XmlOptions(); options.put(XmlOptions.SAVE_PRETTY_PRINT); options.put(XmlOptions.SAVE_PRETTY_PRINT_INDENT, 2); options.put(XmlOptions.SAVE_AGGRESSIVE_NAMESPACES); String result = object.xmlText(options); return result; }
private XmlObject ensureStore() { if ((_flags & FLAG_STORE) != 0) return this; check_dated(); String value = (_flags & FLAG_NIL) != 0 ? "" : compute_text( has_store() ? get_store() : null ); XmlOptions options = new XmlOptions().setDocumentType(schemaType()); XmlObject x = XmlObject.Factory.newInstance( options ); XmlCursor c = x.newCursor(); c.toNextToken(); c.insertChars( value ); return x; }