Reads an XML element.

@param aSB
The StringBuilder to write to.
@param sIn
Input String.
@param nStart
Starting position.
@param bSafeMode
Whether to escape unsafe HTML tags or not
@return The new position or -1 if this is no valid XML element.
433480
@CheckForSigned
  public static int readXMLElement (final StringBuilder aSB,
                                    final String sIn,
                                    final int nStart,
                                    final boolean bSafeMode)
  {
    try
    {
      if (sIn.charAt (nStart + 1) == '!')
      {
        aSB.append ("<!");
        return nStart + 1;
      }

      int pos;
      boolean bIsCloseTag;
      if (sIn.charAt (nStart + 1) == '/')
      {
        bIsCloseTag = true;
        pos = nStart + 2;
      }
      else
      {
        bIsCloseTag = false;
        pos = nStart + 1;
      }

      if (bSafeMode)
      {
        final StringBuilder temp = new StringBuilder ();
        pos = _readRawUntil (temp, sIn, pos, ' ', '/', '>');
        if (pos == -1)
          return -1;
        final String tag = temp.toString ().trim ().toLowerCase (Locale.US);
        if (MarkdownHTML.isUnsafeHtmlElement (tag))
        {
          aSB.append ("&lt;");
          if (bIsCloseTag)
            aSB.append ('/');
          aSB.append (temp);
        }
        else
        {
          aSB.append ('<');
          if (bIsCloseTag)
            aSB.append ('/');
          aSB.append (temp);
        }
      }
      else
      {
        aSB.append ('<');
        if (bIsCloseTag)
          aSB.append ('/');
        pos = _readRawUntil (aSB, sIn, pos, ' ', '/', '>');
      }
      if (pos == -1)
        return -1;
      pos = _readRawUntil (aSB, sIn, pos, '/', '>');
      if (sIn.charAt (pos) == '/')
      {
        aSB.append (" /");
        pos = readRawUntil (aSB, sIn, pos + 1, '>');
        if (pos == -1)
          return -1;
      }
      if (sIn.charAt (pos) == '>')
      {
        aSB.append ('>');
        return pos + 1;
      }
    }
    catch (final StringIndexOutOfBoundsException e)
    {
      return -1;
    }
    return -1;
  }
394955
public void startElement(String tag, HashMap h)
    {
        if ( !foundRoot ) {
            if (!tag.equals("xfdf")) {
                throw new RuntimeException("Root element is not Bookmark.");
            }
            else { 
            	foundRoot = true;
            }
        }

        if ( tag.equals("xfdf") ){
    		
    	} else if ( tag.equals("f") ) {
    		fileSpec = (String)h.get( "href" );
    	} else if ( tag.equals("fields") ) {
            fields = new HashMap();		// init it!
            listFields = new HashMap();
    	} else if ( tag.equals("field") ) {
    		String	fName = (String) h.get( "name" );
    		fieldNames.push( fName );
    	} else if ( tag.equals("value") ) {
    		fieldValues.push( "" );
    	}
    }
20762
public void readWriteUpToStartElement(String startTag) throws XMLStreamException {
        boolean startTagIsNext = false;
        while (xmlEventReader.hasNext() && !startTagIsNext) {
            XMLEvent peekedEvent = xmlEventReader.peek();
            switch (peekedEvent.getEventType()) {
                case XMLStreamConstants.START_ELEMENT:
                    String elementName = peekedEvent.asStartElement().getName().getLocalPart();
                    if (elementName.equals(startTag)) {
                        startTagIsNext = true;
                    } else {
                        XMLEvent readEvent = xmlEventReader.nextEvent();
                        add(readEvent);
                    }
                    break;
                default:
                    XMLEvent readEvent = xmlEventReader.nextEvent();
                    add(readEvent);
                    break;
            }
        }
    }
868514
private String getNextElementsValue(String wantedName) throws IOException {
    boolean gotSTART_ELEMENT = false;
    try {
      int eventType = in.getEventType();
      while(true) {
        switch(eventType) {
          case XMLStreamConstants.CHARACTERS: // 4
            if(gotSTART_ELEMENT) {
              // XML returns "\n" instead of empty (zero-length) string
              // for elements like <x></x>
              return in.getText().trim();
            }
            break;
          case XMLStreamConstants.END_DOCUMENT: // 8
            throw new IOException("End of XML while looking for element [" +
              wantedName + "]");
            // break;
          case XMLStreamConstants.START_ELEMENT : // 1
            if(gotSTART_ELEMENT) {
              throw new IOException("START_ELEMENT [" +
                in.getName() +
                " event when expecting CHARACTERS event for [" +
                wantedName + "]");
            } else if(in.getName().toString().equals(wantedName)) {
              gotSTART_ELEMENT = true;
            } else {
              throw new IOException("unexpected element name [" +
                in.getName() + "], was expecting [" +
                wantedName + "]");
            }
            break;
          case XMLStreamConstants.COMMENT:
          case XMLStreamConstants.END_ELEMENT: // 2
          case XMLStreamConstants.SPACE:
          case XMLStreamConstants.START_DOCUMENT: // 7
            // these are in XML but we don't need them
            break;
          // these should never appear in edits XML
          case XMLStreamConstants.ATTRIBUTE:
          case XMLStreamConstants.CDATA:
          case XMLStreamConstants.DTD:
          case XMLStreamConstants.ENTITY_DECLARATION:
          case XMLStreamConstants.ENTITY_REFERENCE:
          case XMLStreamConstants.NAMESPACE:
          case XMLStreamConstants.NOTATION_DECLARATION:
          case XMLStreamConstants.PROCESSING_INSTRUCTION:
          default:
            throw new IOException("Unsupported event type [" +
              eventType + "] (see XMLStreamConstants)");
        }
        if(!in.hasNext()) { break; }
        eventType = in.next();
      }
    } catch(XMLStreamException e) {
      throw new IOException("Error reading XML stream", e);
    }
    throw new IOException(
      "Error reading XML stream, should never reach this line, " +
      "most likely XML does not have elements we are loking for");
  }
1145928
protected XMLEvent readStartTag(XMLEventReader reader, QName tagName)
            throws XMLStreamException, JournalException {
        XMLEvent event = reader.nextTag();
        if (!isStartTagEvent(event, tagName)) {
            throw getNotStartTagException(tagName, event);
        }
        return event;
    }
1127545
@Override
  public XMLObject unmarshall(Element domElement) throws UnmarshallingException {

    Document newDocument = null;

    Node childNode = domElement.getFirstChild();
    while (childNode != null) {
      if (childNode.getNodeType() != Node.TEXT_NODE) {
        // We skip everything except for a text node.
        log.info("Ignoring node {} - it is not a text node", childNode.getNodeName());
      }
      else {
        newDocument = parseContents((Text) childNode, domElement);
        if (newDocument != null) {
          break;
        }
      }
      childNode = childNode.getNextSibling();
    }

    return super.unmarshall(newDocument != null ? newDocument.getDocumentElement() : domElement);
  }
125509
public boolean nextStartElement() throws XMLException, IOException {
		try {
			do {
				next();
			} while (!Type.START_ELEMENT.equals(event.type));
			return true;
		} catch (EOFException e) {
			return false;
		}
	}
607457
private Node tryFoldStringSubstr(Node n, Node stringNode, Node arg1) {
    checkArgument(n.isCall());
    checkArgument(stringNode.isString());
    checkArgument(arg1 != null);

    int start;
    int length;
    String stringAsString = stringNode.getString();

    Double maybeStart = NodeUtil.getNumberValue(arg1);
    if (maybeStart != null) {
      start = maybeStart.intValue();
    } else {
      return n;
    }

    Node arg2 = arg1.getNext();
    if (arg2 != null) {
      Double maybeLength = NodeUtil.getNumberValue(arg2);
      if (maybeLength != null) {
        length = maybeLength.intValue();
      } else {
        return n;
      }

      if (arg2.getNext() != null) {
        // If we got more args than we expected, bail out.
        return n;
      }
    } else {
      // parameter 2 not passed
      length = stringAsString.length() - start;
    }

    // Don't handle these cases. The specification actually does
    // specify the behavior in some of these cases, but we haven't
    // done a thorough investigation that it is correctly implemented
    // in all browsers.
    if ((start + length) > stringAsString.length() ||
        (length < 0) ||
        (start < 0)) {
      return n;
    }

    String result = stringAsString.substring(start, start + length);
    Node resultNode = IR.string(result);

    Node parent = n.getParent();
    parent.replaceChild(n, resultNode);
    reportChangeToEnclosingScope(parent);
    return resultNode;
  }
897276
public static NodeList getElementsB(Node node, XPathExpression xPath)
            throws XPathExpressionException {
        return (NodeList) xPath.evaluate(node, XPathConstants.NODESET);
    }
22989
public AsyncWork<UnprotectedStringBuffer, Exception> readInnerText() {
		if (!Type.START_ELEMENT.equals(event.type))
			return new AsyncWork<>(null, new Exception("Invalid call of readInnerText: it must be called on a start element"));
		if (event.isClosed)
			return new AsyncWork<>(new UnprotectedStringBuffer(), null);
		UnprotectedStringBuffer innerText = new UnprotectedStringBuffer();
		AsyncWork<UnprotectedStringBuffer, Exception> result = new AsyncWork<>();
		readInnerText(innerText, result);
		return result;
	}
30252
function getStartToken(elementNode) {
                if (!elementNode) {
                    return null
                }
                if (elementNode.type !== "HTMLElement") {
                    return null
                }
                if (isDummyElement(elementNode)) {
                    return null
                }
                return elementNode.startTag || elementNode
            }
628269
@Override
    public final int readArrayB(DeMember member, byte[] typevals, Decodeable decoder) {
        char ch = nextGoodChar();
        if (ch == '[' || ch == '{') return SIGN_NOLENGTH;
        if (ch == 'n' && nextChar() == 'u' && nextChar() == 'l' && nextChar() == 'l') return SIGN_NULL;
        if (ch == 'N' && nextChar() == 'U' && nextChar() == 'L' && nextChar() == 'L') return SIGN_NULL;
        StringBuilder sb = new StringBuilder();
        sb.append(ch);
        char one;
        try {
            while ((one = nextChar()) != 0) sb.append(one);
        } catch (Exception e) {
        }
        throw new ConvertException("a json array text must begin with '[' (position = " + position + ") but '" + ch + "' in (" + sb + ")");
    }
1329976
protected final void readToEndElement(XMLEventReader eventReader, StartElement event)
            throws XMLStreamException {
        final Deque<QName> elementStack = new LinkedList<QName>();
        elementStack.push(event.getName());

        while (!elementStack.isEmpty()) {
            final XMLEvent nextEvent = eventReader.nextEvent();
            if (nextEvent.isStartElement()) {
                final StartElement startElement = nextEvent.asStartElement();
                elementStack.push(startElement.getName());
            } else if (nextEvent.isEndElement()) {
                final QName lastStart = elementStack.pop();
                final EndElement endElement = nextEvent.asEndElement();
                if (!lastStart.equals(endElement.getName())) {
                    throw new XMLStreamException(
                            "Invalid XML Structure, expected EndElement "
                                    + lastStart
                                    + " but found EndElment "
                                    + endElement.getName());
                }
            }
        }
    }
5640
public XMLSerializer text(char[] buf, int start, int len) throws IOException {
		if (startTagIncomplete || setPrefixCalled)
			closeStartTag();
		if (doIndent && seenTag)
			seenTag = false;
		writeElementContent(buf, start, len, out);
		return this;
	}
848103
public StringBuffer addLinkTag(StringBuffer sb)
    {
        int iIndex = sb.lastIndexOf(Utility.endTag(this.getTableNames(false)));
        if (iIndex != -1)
        {
            String strLink = Utility.encodeXML(this.getLink());
            String strHelpLink = strLink;
            if (strLink.indexOf('?') != -1)   // Always
                strHelpLink = "?" + DBParams.HELP + "=" + "&amp;" + strLink.substring(strLink.indexOf('?') + 1);
            sb.insert(iIndex,
                    Utility.startTag(XMLTags.LINK) +
                        strLink +
                    Utility.endTag(XMLTags.LINK) + XmlUtilities.NEWLINE +
                    Utility.startTag(XMLTags.HELPLINK) +
                        strHelpLink +
                    Utility.endTag(XMLTags.HELPLINK) + XmlUtilities.NEWLINE);
        }
        return sb;
    }
102135
public static boolean isStartElement(
            final XMLStreamReader reader,
            final String namespace,
            final String localName) {
        return reader.getEventType() == XMLStreamConstants.START_ELEMENT
                && nameEquals(reader, namespace, localName);
    }
819790
private final void handleStartElem(char c)
        throws XMLStreamException
    {
        mTokenState = TOKEN_FULL_COALESCED;
        boolean empty;

        if (mCfgNsEnabled) {
            String str = parseLocalName(c);
            c = (mInputPtr < mInputEnd) ?
                mInputBuffer[mInputPtr++] : getNextCharFromCurrent(SUFFIX_EOF_EXP_NAME);
            if (c == ':') { // Ok, got namespace and local name
                c = (mInputPtr < mInputEnd) ?
                    mInputBuffer[mInputPtr++] : getNextCharFromCurrent(SUFFIX_EOF_EXP_NAME);
                mElementStack.push(str, parseLocalName(c));
                c = (mInputPtr < mInputEnd) ?
                    mInputBuffer[mInputPtr++] : getNextCharFromCurrent(SUFFIX_IN_ELEMENT);
            } else {
                mElementStack.push(null, str);
                // c is fine as
            }
            /* Enough about element name itself; let's then parse attributes
             * and namespace declarations. Split into another method for clarity,
             * and so that maybe JIT has easier time to optimize it separately.
             */
             /* 04-Jul-2005, TSa: But hold up: we can easily check for a fairly
              *   common case of no attributes showing up, and us getting the
              *   closing '>' right away. Let's do that, since it can save
              *   a call to a rather long method.
              */
            empty = (c == '>') ? false : handleNsAttrs(c);
        } else { // Namespace handling not enabled:
            mElementStack.push(null, parseFullName(c));
            c = (mInputPtr < mInputEnd) ?
                mInputBuffer[mInputPtr++] : getNextCharFromCurrent(SUFFIX_IN_ELEMENT);
            empty = (c == '>') ? false : handleNonNsAttrs(c);
        }
        if (!empty) {
            ++mCurrDepth; // needed to match nesting with entity expansion
        }
        mStEmptyElem = empty;

        /* 27-Feb-2009, TSa: [WSTX-191]: We used to validate virtual
         *   end element here for empty elements, but it really should
         *   occur later on when actually returning that end element.
         */
        int vld = mElementStack.resolveAndValidateElement();
        mVldContent = vld;
        mValidateText = (vld == XMLValidator.CONTENT_ALLOW_VALIDATABLE_TEXT);
    }