/** * Throws an exception if the VTDNav cursor is not set to an element * starting tag. * * @throws NavException */ private void checkCursorPosition() throws NavException { if (vn.getTokenType(vn.getCurrentIndex()) != VTDNav.TOKEN_STARTING_TAG) throw new IllegalStateException("VTDNav is positioned incorrectly. It must point to a starting tag."); if (!vn.toString(vn.getCurrentIndex()).equals(elementName)) throw new IllegalStateException( "VTDNav is positioned incorrectly. It is expected to be positioned at the starting tag \"" + elementName + "\" but it is set to \"" + vn.toString(vn.getCurrentIndex()) + "\"."); }
@Override protected void beforeParseElement() throws ElementParsingException { // since this parser does not know the element is is used upon, set // it first for the parsing result creation try { elementName = vn.toString(vn.getCurrentIndex()); } catch (NavException e) { throw new ElementParsingException(e); } }
final protected void selectPrecedingNode() { ft = true; depth = vn.getCurrentDepth(); contextCopy = (int[]) vn.context.clone(); if (contextCopy[0] != -1) { for (int i = contextCopy[0] + 1; i < contextCopy.length; i++) { contextCopy[i] = 0; } } // else{ // for (int i=1;i<contextCopy.length;i++){ // contextCopy[i]=0; // } // } iter_type = PRECEDING_NODE; endIndex = vn.getCurrentIndex(); }
final protected void selectPrecedingNode(){ ft = true; depth = vn.getCurrentDepth(); contextCopy = (int[])vn.context.clone(); if (contextCopy[0]!=-1){ for (int i=contextCopy[0]+1;i<contextCopy.length;i++){ contextCopy[i]=0; } }//else{ // for (int i=1;i<contextCopy.length;i++){ // contextCopy[i]=0; // } //} iter_type = PRECEDING_NODE; endIndex = vn.getCurrentIndex(); }
protected String moveToNextStartingTag() throws DocumentParsingException { try { int i = vn.getCurrentIndex(); int tokenType = vn.getTokenType(i); while (tokenType != VTDNav.TOKEN_STARTING_TAG && i < vn.getTokenCount()) ++i; vn.recoverNode(i); return vn.toString(vn.getCurrentIndex()); } catch (NavException e) { throw new DocumentParsingException(e); } }
final protected void selectPrecedingNode(){ ft = true; depth = vn.getCurrentDepth(); contextCopy = (int[])vn.context.clone(); if (contextCopy[0]!=-1){ for (int i=contextCopy[0]+1;i<contextCopy.length;i++){ contextCopy[i]=0; } }//else{ // for (int i=1;i<contextCopy.length;i++){ // contextCopy[i]=0; // } //} iter_type = PRECEDING_NODE; endIndex = vn.getCurrentIndex(); }
/** * Sets the VTDNav cursor the first position after the current element. * * @return The new token index. */ protected int skipElement() { int elementDepth = vn.getCurrentDepth(); int i = vn.getCurrentIndex() + 1; while (i < vn.getTokenCount() && tokenIndexBelongsToElement(i, elementDepth)) { ++i; } return i; }
/** * Add InlineObject for a punctuation text node. * * @param inlineObjects list to add the punct object to * @param text * @throws NavException */ private void collectPunct(List<InlineObject> inlineObjects, String text) throws NavException { int i = nav.getCurrentIndex(); int offset = nav.getTokenOffset(i); // int length = nav.getTokenLength(i); // Make sure we only keep 1 copy of identical punct texts in memory text = dedupe(StringUtil.normalizeWhitespace(text)); // Add the punct to the list inlineObjects.add(new InlineObject(text, offset, InlineObjectType.PUNCTUATION, null)); }
/** * This method will first call getCurrentIndex() to get the cursor index value * then insert the byte value of s after the element * @param s the string whose byte content will be inserted into the master document * */ public void insertAfterElement(String s) throws ModifyException,UnsupportedEncodingException,NavException{ int startTagIndex =md.getCurrentIndex(); int type = md.getTokenType(startTagIndex); if (type!=VTDNav.TOKEN_STARTING_TAG) throw new ModifyException("Token type is not a starting tag"); long l = md.getElementFragment(); int offset = (int)l; int len = (int)(l>>32); insertBytesAt(offset+len,s.getBytes(charSet)); }
/** * This method will first call getCurrentIndex() to get the cursor index value * then insert the byte value of s after the element * @param s the string whose byte content will be inserted into the master document * */ public void insertAfterElement(String s) throws ModifyException,UnsupportedEncodingException,NavException{ int startTagIndex =md.getCurrentIndex(); int type = md.getTokenType(startTagIndex); if (type!=VTDNav.TOKEN_STARTING_TAG) throw new ModifyException("Token type is not a starting tag"); long l = md.getElementFragment(); int offset = (int)l; int len = (int)(l>>32); insertBytesAt(offset+len,s.getBytes(charSet)); }
/** * This method will first call getCurrentIndex() to get the cursor index value * then insert the byte value of s before the element * @param s * */ public void insertBeforeElement(String s) throws ModifyException,UnsupportedEncodingException{ int startTagIndex =md.getCurrentIndex(); int type = md.getTokenType(startTagIndex); if (type!=VTDNav.TOKEN_STARTING_TAG) throw new ModifyException("Token type is not a starting tag"); int offset = md.getTokenOffset(startTagIndex)-1; if (encoding < VTDNav.FORMAT_UTF_16BE) insertBytesAt(offset,s.getBytes(charSet)); else insertBytesAt((offset)<<1,s.getBytes(charSet)); }
/** * This method will first call getCurrentIndex() to get the cursor index value * then insert the byte value of s after the element * @param s the string whose byte content will be inserted into the master document * */ public void insertAfterElement(String s) throws ModifyException,UnsupportedEncodingException,NavException{ int startTagIndex =md.getCurrentIndex(); int type = md.getTokenType(startTagIndex); if (type!=VTDNav.TOKEN_STARTING_TAG) throw new ModifyException("Token type is not a starting tag"); long l = md.getElementFragment(); int offset = (int)l; int len = (int)(l>>32); insertBytesAt(offset+len,s.getBytes(charSet)); }
/** * This method will first call getCurrentIndex() to get the cursor index value * then insert the byte value of s before the element * @param s * */ public void insertBeforeElement(String s) throws ModifyException,UnsupportedEncodingException{ int startTagIndex =md.getCurrentIndex(); int type = md.getTokenType(startTagIndex); if (type!=VTDNav.TOKEN_STARTING_TAG) throw new ModifyException("Token type is not a starting tag"); int offset = md.getTokenOffset(startTagIndex)-1; if (encoding < VTDNav.FORMAT_UTF_16BE) insertBytesAt(offset,s.getBytes(charSet)); else insertBytesAt((offset)<<1,s.getBytes(charSet)); }
private int findIndexAfterElement(int elementDepth, int startIndex) { int index = vn.getCurrentIndex(); int tokenType = vn.getTokenType(index); if (tokenType == VTDNav.TOKEN_STARTING_TAG) index = findIndexAfterStartingTag(index); while (index < vn.getTokenCount() && tokenIndexBelongsToElement(index, elementDepth)) ++index; return index; }
public static String getElementText(VTDNav vn) throws NavException { StringBuilder sb = new StringBuilder(); int depth = vn.getCurrentDepth(); int i = vn.getCurrentIndex(); while (vn.getTokenType(i) == VTDNav.TOKEN_STARTING_TAG) i++; while (vn.getTokenDepth(i) >= depth && !(vn.getTokenType(i) == VTDNav.TOKEN_STARTING_TAG && vn.getTokenDepth(i) == depth) && i < vn.getTokenCount()) { if (vn.getTokenType(i) == VTDNav.TOKEN_CHARACTER_DATA || vn.getTokenType(i) == VTDNav.TOKEN_CDATA_VAL) sb.append(vn.toString(i)); i++; } return sb.toString(); }
/** * Insert a namespace compensated element after cursor element * @param ef (an ElementFragmentNs object) * @throws ModifyException * @throws NavException * */ public void insertAfterElement(ElementFragmentNs ef) throws ModifyException, NavException { int startTagIndex = md.getCurrentIndex(); int type = md.getTokenType(startTagIndex); if (type != VTDNav.TOKEN_STARTING_TAG) throw new ModifyException("Token type is not a starting tag"); long l = md.getElementFragment(); int offset = (int) l; int len = (int) (l >> 32); insertElementFragmentNsAt(offset + len, ef); }
/** * Insert a namespace compensated element after cursor element * @param ef (an ElementFragmentNs object) * @throws ModifyException * @throws NavException * */ public void insertAfterElement(ElementFragmentNs ef) throws ModifyException, NavException { int startTagIndex = md.getCurrentIndex(); int type = md.getTokenType(startTagIndex); if (type != VTDNav.TOKEN_STARTING_TAG) throw new ModifyException("Token type is not a starting tag"); long l = md.getElementFragment(); int offset = (int) l; int len = (int) (l >> 32); insertElementFragmentNsAt(offset + len, ef); }
/** * * @param l * @throws ModifyException */ private void insertEndingTag(long l) throws ModifyException{ int i = md.getCurrentIndex(); int offset = md.getTokenOffset(i); int length = md.getTokenLength(i)&0xffff; byte[] xml = md.getXML().getBytes(); if (md.encoding <VTDNav.FORMAT_UTF_16BE ) insertBytesAt((int)l,xml,offset,length); else insertBytesAt((int)l, xml, offset<<1, length<<1); }
/** * * @param l * @throws ModifyException */ private void insertEndingTag(long l) throws ModifyException{ int i = md.getCurrentIndex(); int offset = md.getTokenOffset(i); int length = md.getTokenLength(i)&0xffff; byte[] xml = md.getXML().getBytes(); if (md.encoding <VTDNav.FORMAT_UTF_16BE ) insertBytesAt((int)l,xml,offset,length); else insertBytesAt((int)l, xml, offset<<1, length<<1); }
/** * * @param l * @throws ModifyException */ private void insertEndingTag(long l) throws ModifyException{ int i = md.getCurrentIndex(); int offset = md.getTokenOffset(i); int length = md.getTokenLength(i)&0xffff; byte[] xml = md.getXML().getBytes(); if (md.encoding <VTDNav.FORMAT_UTF_16BE ) insertBytesAt((int)l,xml,offset,length); else insertBytesAt((int)l, xml, offset<<1, length<<1); }