Here you can find the source of readTextWithNodes(XMLStreamReader xsr, Map
Parameter | Description |
---|---|
xsr | a parameter |
nodeIdToOffsetMap | a parameter |
public static String readTextWithNodes(XMLStreamReader xsr, Map<Integer, Long> nodeIdToOffsetMap) throws XMLStreamException
//package com.java2s; /*// w ww.j ava2 s .c o m * DocumentStaxUtils.java * * Copyright (c) 1995-2012, The University of Sheffield. See the file * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt * * This file is part of GATE (see http://gate.ac.uk/), and is free * software, licenced under the GNU Library General Public License, * Version 2, June 1991 (in the distribution as file licence.html, * and also available at http://gate.ac.uk/gate/licence.html). * * Ian Roberts, 20/Jul/2006 * * $Id$ */ import java.util.Map; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; public class Main { /** * Processes the TextWithNodes element from this XMLStreamReader, * returning the text content of the document. The supplied map is * updated with the offset of each Node element encountered. The * reader must be positioned on the starting TextWithNodes tag and * will be returned positioned on the corresponding closing tag. * * @param xsr * @param nodeIdToOffsetMap * @return the text content of the document */ public static String readTextWithNodes(XMLStreamReader xsr, Map<Integer, Long> nodeIdToOffsetMap) throws XMLStreamException { StringBuffer textBuf = new StringBuffer(20480); int eventType; while ((eventType = xsr.next()) != XMLStreamConstants.END_ELEMENT) { switch (eventType) { case XMLStreamConstants.CHARACTERS: textBuf.append(xsr.getTextCharacters(), xsr.getTextStart(), xsr.getTextLength()); break; case XMLStreamConstants.START_ELEMENT: // only Node elements allowed xsr.require(XMLStreamConstants.START_ELEMENT, null, "Node"); String idString = xsr.getAttributeValue(null, "id"); if (idString == null) { throw new XMLStreamException("Node element has no id", xsr.getLocation()); } try { Integer id = Integer.valueOf(idString); Long offset = new Long(textBuf.length()); nodeIdToOffsetMap.put(id, offset); } catch (NumberFormatException nfe) { throw new XMLStreamException("Node element must have " + "integer id", xsr.getLocation()); } // Node element must be empty if (xsr.next() != XMLStreamConstants.END_ELEMENT) { throw new XMLStreamException("Node element within TextWithNodes " + "must be empty.", xsr.getLocation()); } break; default: // do nothing - ignore comments, PIs... } } return textBuf.toString(); } }