This class provides support for reading and writing GATE XML format using StAX (the Streaming API for XML).
| 67 | * using StAX (the Streaming API for XML). |
| 68 | */ |
| 69 | public class DocumentStaxUtils { |
| 70 | |
| 71 | private static XMLInputFactory inputFactory = null; |
| 72 | |
| 73 | /** |
| 74 | * The char used to replace characters in text content that are |
| 75 | * illegal in XML. |
| 76 | */ |
| 77 | public static final char INVALID_CHARACTER_REPLACEMENT = ' '; |
| 78 | |
| 79 | public static final String GATE_XML_VERSION = "3"; |
| 80 | |
| 81 | /** |
| 82 | * The number of < signs after which we encode a string using CDATA |
| 83 | * rather than writeCharacters. |
| 84 | */ |
| 85 | public static final int LT_THRESHOLD = 5; |
| 86 | |
| 87 | /** |
| 88 | * Reads GATE XML format data from the given XMLStreamReader and puts |
| 89 | * the content and annotation sets into the given Document, replacing |
| 90 | * its current content. The reader must be positioned on the opening |
| 91 | * GateDocument tag (i.e. the last event was a START_ELEMENT for which |
| 92 | * getLocalName returns "GateDocument"), and when the method returns |
| 93 | * the reader will be left positioned on the corresponding closing |
| 94 | * tag. |
| 95 | * |
| 96 | * @param xsr the source of the XML to parse |
| 97 | * @param doc the document to update |
| 98 | * @throws XMLStreamException |
| 99 | */ |
| 100 | public static void readGateXmlDocument(XMLStreamReader xsr, Document doc) |
| 101 | throws XMLStreamException { |
| 102 | readGateXmlDocument(xsr, doc, null); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Reads GATE XML format data from the given XMLStreamReader and puts |
| 107 | * the content and annotation sets into the given Document, replacing |
| 108 | * its current content. The reader must be positioned on the opening |
| 109 | * GateDocument tag (i.e. the last event was a START_ELEMENT for which |
| 110 | * getLocalName returns "GateDocument"), and when the method returns |
| 111 | * the reader will be left positioned on the corresponding closing |
| 112 | * tag. |
| 113 | * |
| 114 | * @param xsr the source of the XML to parse |
| 115 | * @param doc the document to update |
| 116 | * @param statusListener optional status listener to receive status |
| 117 | * messages |
| 118 | * @throws XMLStreamException |
| 119 | */ |
| 120 | public static void readGateXmlDocument(XMLStreamReader xsr, Document doc, |
| 121 | StatusListener statusListener) throws XMLStreamException { |
| 122 | DocumentContent savedContent = null; |
| 123 | |
| 124 | // check the precondition |
| 125 | xsr.require(XMLStreamConstants.START_ELEMENT, null, "GateDocument"); |
| 126 |