Java Code Examples for org.apache.xmlbeans.XmlCursor#toNextToken()
The following examples show how to use
org.apache.xmlbeans.XmlCursor#toNextToken() .
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: 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 2
Source File: DhlXTeeServiceImpl.java From j-road with Apache License 2.0 | 6 votes |
public void markDocumentsReceived(Collection<String> receivedDocumentIds) { String queryMethod = getDatabase() + "." + MARK_DOCUMENTS_RECEIVED + "." + MARK_DOCUMENTS_RECEIVED_VERSION; MarkDocumentsReceivedRequestType request = MarkDocumentsReceivedRequestType.Factory.newInstance(); byte[] attachmentBody = createMarkDocumentsReceivedAttachmentBody(receivedDocumentIds); request.setDokumendid(null); String cid = AttachmentUtil.getUniqueCid(); XmlCursor cursor = request.newCursor(); cursor.toNextToken(); Element node = (Element) cursor.getDomNode(); node.setAttribute("href", "cid:" + cid); cursor.dispose(); XRoadAttachment attachment = new XRoadAttachment(cid, "{http://www.w3.org/2001/XMLSchema}base64Binary", attachmentBody); log.debug("executing " + queryMethod); try { XRoadMessage<MarkDocumentsReceivedResponseType> response // = send(new XmlBeansXRoadMessage<MarkDocumentsReceivedRequestType>(request, Arrays.asList(attachment)), MARK_DOCUMENTS_RECEIVED, MARK_DOCUMENTS_RECEIVED_VERSION); if (!response.getContent().getStringValue().equalsIgnoreCase("OK")) { throw new RuntimeException("Service didn't respond with 'OK': " + response.getContent().getStringValue()); } } catch (XRoadServiceConsumptionException e) { throw new WrappedXRoadServiceConsumptionException(e); } }
Example 3
Source File: ViisaregisterXTeeServiceImpl.java From j-road with Apache License 2.0 | 6 votes |
public TaotluseAndmedVastus taotluseAndmedNrLiikParing(String taotluseLiik, String taotluseNr) throws XRoadServiceConsumptionException { TaotluseAndmedNrLiikSisend paring = TaotluseAndmedNrLiikSisend.Factory.newInstance(); paring.setTaotluseLiik(com.nortal.jroad.client.viisaregister.types.ee.riik.xtee.viisaregister.producers.producer.viisaregister.TaotluseLiik.Enum.forString(taotluseLiik)); paring.setTaotluseNr(taotluseNr); //Kuna viisaregister tahab kindlasti oma xsi:type atribuute näha, siis paneme need käsitsi külge XmlCursor cursor = paring.newCursor(); while (!cursor.isEnddoc()) { if (cursor.isStart() && cursor.getName().getLocalPart().equals("taotluseLiik")) { cursor.toNextToken(); cursor.insertAttributeWithValue(new QName("http://www.w3.org/2001/XMLSchema-instance", "type", "xsi"), "ns5:taotluseLiik"); } if (cursor.isStart() && cursor.getName().getLocalPart().equals("taotluseNr")) { cursor.toNextToken(); cursor.insertAttributeWithValue(new QName("http://www.w3.org/2001/XMLSchema-instance", "type", "xsi"), "xsd:string"); } cursor.toNextToken(); } return viisaregisterXRoadDatabase.taotlAndmedNrLiikParingV1(paring); }
Example 4
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 5
Source File: EbicsXmlFactory.java From ebics-java-client with GNU Lesser General Public License v2.1 | 6 votes |
/** * Qualifies a valid member of a substitution group. This method tries to use the * built-in {@link XmlObject#substitute(QName, SchemaType)} and if succesful returns * a valid substitution which is usable (not disconnected). If it fails, it uses * low-level {@link XmlCursor} manipulation to qualify the substitution group. Note * that if the latter is the case the resulting document is disconnected and should * no longer be manipulated. Thus, use it as a final step after all markup is included. * * If newType is null, this method will skip {@link XmlObject#substitute(QName, SchemaType)} * and directly use {@link XmlCursor}. This can be used, if you are sure that the substitute * is not in the list of (pre-compiled) valid substitutions (this is the case if a schema * uses another schema's type as a base for elements. E.g. om:Observation uses gml:_Feature * as the base type). * * @param xobj * the abstract element * @param newInstance * the new {@link QName} of the instance * @param newType the new schemaType. if null, cursors will be used and the resulting object * will be disconnected. * @return if successful applied {@link XmlObject#substitute(QName, SchemaType)} a living object with a * type == newType is returned. Otherwise null is returned as you can no longer manipulate the object. */ public static XmlObject qualifySubstitutionGroup(XmlObject xobj, QName newInstance, SchemaType newType) { XmlObject substitute = null; if (newType != null) { substitute = xobj.substitute(newInstance, newType); if (substitute != null && substitute.schemaType() == newType && substitute.getDomNode().getLocalName().equals(newInstance.getLocalPart())) { return substitute; } } XmlCursor cursor = xobj.newCursor(); cursor.setName(newInstance); QName qName = new QName("http://www.w3.org/2001/XMLSchema-instance", "type"); cursor.removeAttribute(qName); cursor.toNextToken(); if (cursor.isNamespace()) { cursor.removeXml(); } cursor.dispose(); return null; }
Example 6
Source File: DefaultEbicsRootElement.java From ebics-java-client with GNU Lesser General Public License v2.1 | 6 votes |
/** * Inserts a schema location to the current ebics root element. * @param namespaceURI the name space URI * @param localPart the local part * @param prefix the prefix * @param value the value */ public void insertSchemaLocation(String namespaceURI, String localPart, String prefix, String value) { XmlCursor cursor; cursor = document.newCursor(); while (cursor.hasNextToken()) { if (cursor.isStart()) { cursor.toNextToken(); cursor.insertAttributeWithValue(new QName(namespaceURI, localPart, prefix), value); break; } else { cursor.toNextToken(); } } }
Example 7
Source File: DefaultEbicsRootElement.java From axelor-open-suite with GNU Affero General Public License v3.0 | 6 votes |
/** * Inserts a schema location to the current ebics root element. * * @param namespaceURI the name space URI * @param localPart the local part * @param prefix the prefix * @param value the value */ public void insertSchemaLocation( String namespaceURI, String localPart, String prefix, String value) { XmlCursor cursor; cursor = document.newCursor(); while (cursor.hasNextToken()) { if (cursor.isStart()) { cursor.toNextToken(); cursor.insertAttributeWithValue(new QName(namespaceURI, localPart, prefix), value); break; } else { cursor.toNextToken(); } } }
Example 8
Source File: XML.java From astor with GNU General Public License v2.0 | 5 votes |
protected void changeNS (String oldURI, String newURI) { XmlCursor curs = newCursor(); while (curs.toParent()) { /* Goto the top of the document */ } TokenType tt = curs.currentTokenType(); if (tt.isStartdoc()) { tt = curs.toFirstContentToken(); } if (tt.isStart()) { do { if (tt.isStart() || tt.isAttr() || tt.isNamespace()) { javax.xml.namespace.QName currQName = curs.getName(); if (oldURI.equals(currQName.getNamespaceURI())) { curs.setName(new javax.xml.namespace.QName(newURI, currQName.getLocalPart())); } } tt = curs.toNextToken(); } while (!tt.isEnddoc() && !tt.isNone()); } curs.dispose(); }
Example 9
Source File: XML.java From astor with GNU General Public License v2.0 | 5 votes |
/** * * @param srcCurs * @param destCurs * @param fDontMoveIfSame * @return */ private boolean moveSrcToDest (XmlCursor srcCurs, XmlCursor destCurs, boolean fDontMoveIfSame) { boolean fMovedSomething = true; TokenType tt; do { if (fDontMoveIfSame && srcCurs.isInSameDocument(destCurs) && (srcCurs.comparePosition(destCurs) == 0)) { // If the source and destination are pointing at the same place then there's nothing to move. fMovedSomething = false; break; } // todo ***TLL*** Use replaceContents (when added) and eliminate children removes (see above todo). if (destCurs.currentTokenType().isStartdoc()) { destCurs.toNextToken(); } // todo ***TLL*** Can Eric support notion of copy instead of me copying then moving??? XmlCursor copyCurs = copy(srcCurs); copyCurs.moveXml(destCurs); copyCurs.dispose(); tt = srcCurs.currentTokenType(); } while (!tt.isStart() && !tt.isEnd() && !tt.isEnddoc()); return fMovedSomething; }
Example 10
Source File: XML.java From astor with GNU General Public License v2.0 | 5 votes |
/** * * @param curs * @return */ private static TokenType skipNonElements (XmlCursor curs) { TokenType tt = curs.currentTokenType(); while (tt.isComment() || tt.isProcinst()) { tt = curs.toNextToken(); } return tt; }
Example 11
Source File: XmlBeansUtil.java From j-road with Apache License 2.0 | 5 votes |
/** * Builds a new {@link XmlString} instance holding the given <code>String</code> value. The instance will also have an * attribute with <code>xsi:type</code> name (where <code>xsi</code> represent a prefix for * <code>http://www.w3.org/2001/XMLSchema-instance</code> namespace) and a <code>xsd:string</code> value (where * <code>xsd</code> represents a prefix for <code>http://www.w3.org/2001/XMLSchema</code> namespace). * * @param value content value * @return constructed {@link XmlString} instance */ public static XmlString getAttributedXmlString(String value) { XmlString xmlString = XmlString.Factory.newInstance(); xmlString.setStringValue(value); XmlCursor cursor = xmlString.newCursor(); cursor.toNextToken(); cursor.insertNamespace("xsd", "http://www.w3.org/2001/XMLSchema"); cursor.insertAttributeWithValue(new QName("http://www.w3.org/2001/XMLSchema-instance", "type", "xsi"), "xsd:string"); return xmlString; }
Example 12
Source File: DefaultEbicsRootElement.java From ebics-java-client with GNU Lesser General Public License v2.1 | 5 votes |
@Override public void addNamespaceDecl(String prefix, String uri) { XmlCursor cursor; cursor = document.newCursor(); while (cursor.hasNextToken()) { if (cursor.isStart()) { cursor.toNextToken(); cursor.insertNamespace(prefix, uri); break; } else { cursor.toNextToken(); } } }
Example 13
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 14
Source File: XML.java From astor with GNU General Public License v2.0 | 4 votes |
/** * */ void normalize() { XmlCursor curs = newCursor(); TokenType tt = curs.currentTokenType(); // Walk through the tokens removing empty text nodes and merging adjacent text nodes. if (tt.isStartdoc()) { tt = curs.toFirstContentToken(); } if (tt.isContainer()) { int nestLevel = 1; String previousText = null; while (nestLevel > 0) { tt = curs.toNextToken(); if (tt == XmlCursor.TokenType.TEXT) { String currentText = curs.getChars().trim(); if (currentText.trim().length() == 0) { // Empty text node, remove. removeToken(curs); curs.toPrevToken(); } else if (previousText == null) { // No previous text node, reset to trimmed version previousText = currentText; } else { // It appears that this case never happens with XBeans. // Previous text node exists, concatenate String newText = previousText + currentText; curs.toPrevToken(); removeToken(curs); removeToken(curs); curs.insertChars(newText); } } else { previousText = null; } if (tt.isStart()) { nestLevel++; } else if (tt.isEnd()) { nestLevel--; } else if (tt.isEnddoc()) { // Shouldn't get here, but just in case. break; } } } curs.dispose(); }
Example 15
Source File: TarnXTeeServiceImpl.java From j-road with Apache License 2.0 | 4 votes |
private void insertTypeAttribute(XmlCursor cursor, String type, boolean toNext) { if (toNext) { cursor.toNextToken(); } insertTypeAttribute(cursor, "http://www.w3.org/2001/XMLSchema-instance", "type", "xsi", type); }
Example 16
Source File: XML.java From astor with GNU General Public License v2.0 | 4 votes |
/** * * @return */ private XMLList matchChildren(XmlCursor.TokenType tokenType, XMLName name) { XMLList result = new XMLList(lib); XmlCursor curs = newCursor(); TokenType tt = curs.currentTokenType(); javax.xml.namespace.QName qname = new javax.xml.namespace.QName(name.uri(), name.localName()); javax.xml.namespace.QName targetProperty = qname; if (tt.isStartdoc()) { tt = curs.toFirstContentToken(); } if (tt.isContainer()) { tt = curs.toFirstContentToken(); while (!tt.isEnd()) { if (tt == tokenType) { // Only try to match names for elements or processing instructions. if (!tt.isStart() && !tt.isProcinst()) { // Not an element or no name specified. result.addToList(findAnnotation(curs)); // Reset target property to null in this case targetProperty = null; } else { // Match names as well if (qnameMatches(name, curs.getName())) { // Add it to the list result.addToList(findAnnotation(curs)); // Set target property if target name is "*", // Otherwise if target property does not match current, then // set to null if (targetProperty != null) { if (targetProperty.getLocalPart().equals("*")) { targetProperty = curs.getName(); } else if (!targetProperty.getLocalPart().equals(curs.getName().getLocalPart())) { // Not a match, unset target property targetProperty = null; } } } } } // Skip over child elements if (tt.isStart()) { tt = curs.toEndToken(); } tt = curs.toNextToken(); } } curs.dispose(); if (tokenType == XmlCursor.TokenType.START) { // Set the targets for this XMLList. result.setTargets(this, targetProperty); } return result; }
Example 17
Source File: XML.java From astor with GNU General Public License v2.0 | 4 votes |
private boolean moveToChild(XmlCursor curs, long index, boolean fFirstChild, boolean fUseStartDoc) { if (index < 0) throw new IllegalArgumentException(); long idxChild = 0; if (!fUseStartDoc && curs.currentTokenType().isStartdoc()) { // We always move to the children of the top node. // todo: This assumes that we want have multiple top-level nodes. Which we should be able tohave. curs.toFirstContentToken(); } TokenType tt = curs.toFirstContentToken(); if (!tt.isNone() && !tt.isEnd()) { while (true) { if (index == idxChild) { return true; } tt = curs.currentTokenType(); if (tt.isText()) { curs.toNextToken(); } else if (tt.isStart()) { // Need to do this we want to be pointing at the text if that after the end token. curs.toEndToken(); curs.toNextToken(); } else if (tt.isComment() || tt.isProcinst()) { continue; } else { break; } idxChild++; } } else if (fFirstChild && index == 0) { // Drill into where first child would be. // curs.toFirstContentToken(); return true; } return false; }
Example 18
Source File: XML.java From astor with GNU General Public License v2.0 | 4 votes |
/** * * @param cursToCopy * @return */ private XmlCursor copy (XmlCursor cursToCopy) { XmlObject xo = XmlObject.Factory.newInstance(); XmlCursor copyCurs = null; if (cursToCopy.currentTokenType().isText()) { try { // Try just as a textnode, to do that we need to wrap the text in a special fragment tag // that is not visible from the XmlCursor. copyCurs = XmlObject.Factory.parse("<x:fragment xmlns:x=\"http://www.openuri.org/fragment\">" + cursToCopy.getChars() + "</x:fragment>").newCursor(); if (!cursToCopy.toNextSibling()) { if (cursToCopy.currentTokenType().isText()) { cursToCopy.toNextToken(); // It's not an element it's text so skip it. } } } catch (Exception ex) { throw ScriptRuntime.typeError(ex.getMessage()); } } else { copyCurs = xo.newCursor(); copyCurs.toFirstContentToken(); if (cursToCopy.currentTokenType() == XmlCursor.TokenType.STARTDOC) { cursToCopy.toNextToken(); } cursToCopy.copyXml(copyCurs); if (!cursToCopy.toNextSibling()) // If element skip element. { if (cursToCopy.currentTokenType().isText()) { cursToCopy.toNextToken(); // It's not an element it's text so skip it. } } } copyCurs.toStartDoc(); copyCurs.toFirstContentToken(); return copyCurs; }
Example 19
Source File: XML.java From astor with GNU General Public License v2.0 | 4 votes |
/** * * @return */ private XMLList matchDescendantAttributes(XMLName xmlName) { XMLList result = new XMLList(lib); XmlCursor curs = newCursor(); TokenType tt = curs.currentTokenType(); // Set the targets for this XMLList. result.setTargets(this, null); if (tt.isStartdoc()) { tt = curs.toFirstContentToken(); } if (tt.isContainer()) { int nestLevel = 1; while (nestLevel > 0) { tt = curs.toNextToken(); // Only try to match names for attributes if (tt.isAttr()) { if (qnameMatches(xmlName, curs.getName())) { result.addToList(findAnnotation(curs)); } } if (tt.isStart()) { nestLevel++; } else if (tt.isEnd()) { nestLevel--; } else if (tt.isEnddoc()) { // Shouldn't get here, but just in case. break; } } } curs.dispose(); return result; }
Example 20
Source File: ViisaregisterXTeeServiceImpl.java From j-road with Apache License 2.0 | 4 votes |
public TaotlusteNimistuVastus taotluseAndmedIsikReisidokumentParing(String eesnimi, String perenimi, Calendar synniaeg, String sugu, String reisiDokLiik, String reisiDokNr, Calendar reisiDokValjastamisKuup, String toimik) throws XRoadServiceConsumptionException { TaotluseAndmedIsikReisidokSisend paring = TaotluseAndmedIsikReisidokSisend.Factory.newInstance(); if (eesnimi != null || perenimi != null || synniaeg != null || sugu != null) { StruktIsikSuguMK isik = paring.addNewIsik(); isik.setEesnimi(eesnimi); isik.setPerenimi(perenimi); isik.setSynniaeg(synniaeg); com.nortal.jroad.client.viisaregister.types.ee.riik.xtee.viisaregister.producers.producer.viisaregister.Sugu.Enum suguEnum = com.nortal.jroad.client.viisaregister.types.ee.riik.xtee.viisaregister.producers.producer.viisaregister.Sugu.Enum.forString(sugu); isik.setSugu(suguEnum); } com.nortal.jroad.client.viisaregister.types.ee.riik.xtee.viisaregister.producers.producer.viisaregister.ReisiDokLiik.Enum reisiDokLiikEnum = com.nortal.jroad.client.viisaregister.types.ee.riik.xtee.viisaregister.producers.producer.viisaregister.ReisiDokLiik.Enum.forString(reisiDokLiik); paring.setReisiDokLiik(reisiDokLiikEnum); paring.setReisiDokNr(reisiDokNr); paring.setReisiDokValjastamisKuup(reisiDokValjastamisKuup); //Kuna viisaregister tahab kindlasti oma xsi:type atribuute näha, siis paneme need käsitsi külge XmlCursor cursor = paring.newCursor(); while (!cursor.isEnddoc()) { if (cursor.isStart() && cursor.getName().getLocalPart().equals("isik")) { cursor.toNextToken(); cursor.insertAttributeWithValue(new QName("http://www.w3.org/2001/XMLSchema-instance", "type", "xsi"), "ns5:struktIsikSuguMK"); } if (cursor.isStart() && cursor.getName().getLocalPart().equals("eesnimi")) { cursor.toNextToken(); cursor.insertAttributeWithValue(new QName("http://www.w3.org/2001/XMLSchema-instance", "type", "xsi"), "xsd:string"); } if (cursor.isStart() && cursor.getName().getLocalPart().equals("perenimi")) { cursor.toNextToken(); cursor.insertAttributeWithValue(new QName("http://www.w3.org/2001/XMLSchema-instance", "type", "xsi"), "xsd:string"); } if (cursor.isStart() && cursor.getName().getLocalPart().equals("synniaeg")) { cursor.toNextToken(); cursor.insertAttributeWithValue(new QName("http://www.w3.org/2001/XMLSchema-instance", "type", "xsi"), "xsd:date"); } if (cursor.isStart() && cursor.getName().getLocalPart().equals("sugu")) { cursor.toNextToken(); cursor.insertAttributeWithValue(new QName("http://www.w3.org/2001/XMLSchema-instance", "type", "xsi"), "ns5:sugu"); } if (cursor.isStart() && cursor.getName().getLocalPart().equals("reisiDokLiik")) { cursor.toNextToken(); cursor.insertAttributeWithValue(new QName("http://www.w3.org/2001/XMLSchema-instance", "type", "xsi"), "ns5:reisiDokLiik"); } if (cursor.isStart() && cursor.getName().getLocalPart().equals("reisiDokNr")) { cursor.toNextToken(); cursor.insertAttributeWithValue(new QName("http://www.w3.org/2001/XMLSchema-instance", "type", "xsi"), "xsd:string"); } if (cursor.isStart() && cursor.getName().getLocalPart().equals("reisiDokValjastamisKuup")) { cursor.toNextToken(); cursor.insertAttributeWithValue(new QName("http://www.w3.org/2001/XMLSchema-instance", "type", "xsi"), "xsd:date"); } cursor.toNextToken(); } return viisaregisterXRoadDatabase.taotlAndmedIsikReisidokParingV1(paring); }