1
15
16 package gate.corpora;
17
18 import java.io.*;
20 import java.net.URLConnection;
21
22 import javax.xml.parsers.*;
23
24 import org.xml.sax.InputSource;
25 import org.xml.sax.SAXException;
26
27 import gate.*;
28 import gate.creole.ResourceInstantiationException;
29 import gate.event.StatusListener;
30 import gate.util.DocumentFormatException;
31 import gate.util.Out;
32 import gate.xml.*;
33
35
44 public class XmlDocumentFormat extends TextualDocumentFormat
45 {
46
47 private static final boolean DEBUG = false;
48
49
50 public XmlDocumentFormat() { super(); }
51
52
53 public Boolean supportsRepositioning() {
54 return new Boolean(true);
55 }
57
58 public void unpackMarkup(Document doc) throws DocumentFormatException {
59 unpackMarkup(doc, (RepositioningInfo) null, (RepositioningInfo) null);
60 }
62
63
78 public void unpackMarkup(Document doc, RepositioningInfo repInfo,
79 RepositioningInfo ampCodingInfo) throws DocumentFormatException {
80 if( (doc == null) ||
81 (doc.getSourceUrl() == null && doc.getContent() == null)){
82
83 throw new DocumentFormatException(
84 "GATE document is null or no content found. Nothing to parse!");
85 }
87 boolean docHasContentButNoValidURL = false;
88 try{
91 if (doc.getSourceUrl() == null && doc.getContent() != null){
92 docHasContentButNoValidURL = true;
94 }else {URLConnection conn = doc.getSourceUrl().openConnection();}
95 }catch (IOException ex1){
96 if(doc.getContent() == null)
98 throw new DocumentFormatException("The document doesn't have a" +
100 " valid URL and also no content");
101 docHasContentButNoValidURL = true;
102 }
104 StatusListener statusListener = new StatusListener(){
106 public void statusChanged(String text){
107 fireStatusChanged(text);
109 }
110 };
111 GateFormatXmlDocumentHandler gateXmlHandler = null;
112 XmlDocumentHandler xmlDocHandler = null;
113 if (docHasContentButNoValidURL)
114 parseDocumentWithoutURL(doc, repInfo, ampCodingInfo);
115 else try {
116 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
121 saxParserFactory.setValidating(false);
124 saxParserFactory.setNamespaceAware(true);
126 SAXParser xmlParser = saxParserFactory.newSAXParser();
128 if (isGateXmlDocument){
129 gateXmlHandler = new GateFormatXmlDocumentHandler(doc);
131 gateXmlHandler.addStatusListener(statusListener);
133 InputSource is;
134 if(doc instanceof TextualDocument){
136 String docEncoding = ((TextualDocument)doc).getEncoding();
137 Reader docReader = new InputStreamReader(
138 doc.getSourceUrl().openStream(), docEncoding);
139 is = new InputSource(docReader);
140 is.setSystemId(doc.getSourceUrl().toString());
141 }
142 else {
143 is = new InputSource(doc.getSourceUrl().toString());
144 }
145
146 xmlParser.parse(is, gateXmlHandler);
147 gateXmlHandler.removeStatusListener(statusListener);
148 }else{
149 xmlDocHandler = new XmlDocumentHandler( doc,
151 this.markupElementsMap,
152 this.element2StringMap);
153 xmlDocHandler.addStatusListener(statusListener);
155 xmlDocHandler.setRepositioningInfo(repInfo);
157 xmlDocHandler.setAmpCodingInfo(ampCodingInfo);
159
160
164
167 org.xml.sax.XMLReader newxmlParser = xmlParser.getXMLReader();
168 newxmlParser.setFeature("http://xml.org/sax/features/validation", false);
174 newxmlParser.setFeature("http://xml.org/sax/features/namespaces", true);
177 newxmlParser.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
178 newxmlParser.setContentHandler(xmlDocHandler);
179 newxmlParser.setErrorHandler(xmlDocHandler);
180 newxmlParser.setDTDHandler(xmlDocHandler);
181 newxmlParser.setEntityResolver(xmlDocHandler);
182 InputSource is;
184 if(doc instanceof TextualDocument){
185 String docEncoding = ((TextualDocument)doc).getEncoding();
186 Reader docReader = new InputStreamReader(
187 doc.getSourceUrl().openStream(), docEncoding);
188 is = new InputSource(docReader);
189 is.setSystemId(doc.getSourceUrl().toString());
191 }
192 else {
193 is = new InputSource(doc.getSourceUrl().toString());
194 }
195 newxmlParser.parse(is);
196 ((DocumentImpl) doc).setNextAnnotationId(
198 xmlDocHandler.getCustomObjectsId());
199 xmlDocHandler.removeStatusListener(statusListener);
200 } } catch (ParserConfigurationException e){
202 throw
203 new DocumentFormatException("XML parser configuration exception ", e);
204 } catch (SAXException e){
205 doc.getFeatures().put("parsingError", new Boolean(true));
206
207 Boolean bThrow = (Boolean)
208 doc.getFeatures().get(GateConstants.THROWEX_FORMAT_PROPERTY_NAME);
209
210 if(bThrow != null && bThrow.booleanValue()) {
211 throw new DocumentFormatException(e);
213 }
214 else {
215 Out.println("Warning: Document remains unparsed. \n"
216 +"\n Stack Dump: ");
217 e.printStackTrace(Out.getPrintWriter());
218 }
220 } catch (IOException e){
221 throw new DocumentFormatException("I/O exception for " +
222 doc.getSourceUrl().toString(), e);
223 }finally{
224 if(gateXmlHandler != null)
225 gateXmlHandler.removeStatusListener(statusListener);
226 if (xmlDocHandler != null)
227 xmlDocHandler.removeStatusListener(statusListener);
228 } }
231
234 private void parseDocumentWithoutURL(gate.Document aDocument,
235 RepositioningInfo repInfo,
236 RepositioningInfo ampCodingInfo)
237 throws DocumentFormatException {
238 GateFormatXmlDocumentHandler gateXmlHandler = null;
239 XmlDocumentHandler xmlDocHandler = null;
240 StatusListener statusList = new StatusListener(){
242 public void statusChanged(String text){
243 fireStatusChanged(text);
245 }
246 };
247 try{
248 Reader reader = new StringReader(aDocument.getContent().toString());
249 InputSource is = new InputSource(reader);
255
256
257 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
262 saxParserFactory.setValidating(false);
265 saxParserFactory.setNamespaceAware(true);
267 SAXParser xmlParser = saxParserFactory.newSAXParser();
269
270 if (isGateXmlDocument){
273 gateXmlHandler = new GateFormatXmlDocumentHandler(aDocument);
275 gateXmlHandler.addStatusListener(statusList);
277 xmlParser.parse(is, gateXmlHandler);
283 gateXmlHandler.removeStatusListener(statusList);
284 }else{
285 xmlDocHandler = new XmlDocumentHandler(aDocument,
287 this.markupElementsMap,
288 this.element2StringMap);
289 xmlDocHandler.addStatusListener(statusList);
291 xmlDocHandler.setRepositioningInfo(repInfo);
293 xmlDocHandler.setAmpCodingInfo(ampCodingInfo);
295 org.xml.sax.XMLReader newxmlParser = xmlParser.getXMLReader();
298 newxmlParser.setFeature("http://xml.org/sax/features/validation", false);
304 newxmlParser.setFeature("http://xml.org/sax/features/namespaces", true);
307 newxmlParser.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
308 newxmlParser.setContentHandler(xmlDocHandler);
309 newxmlParser.setErrorHandler(xmlDocHandler);
310 newxmlParser.setDTDHandler(xmlDocHandler);
311 newxmlParser.setEntityResolver(xmlDocHandler);
312 newxmlParser.parse(is);
313 ((DocumentImpl) aDocument).setNextAnnotationId(
314 xmlDocHandler.getCustomObjectsId());
315 }
316 } catch (ParserConfigurationException e){
317 throw new DocumentFormatException(
318 "XML parser configuration exception ", e);
319 } catch (SAXException e){
320 throw new DocumentFormatException(e);
321 } catch (IOException e){
322 throw new DocumentFormatException(e);
323 }finally{
324 if(gateXmlHandler != null) gateXmlHandler.removeStatusListener(statusList);
327 if (xmlDocHandler != null) xmlDocHandler.removeStatusListener(statusList);
328
329 } }
332
333 public Resource init() throws ResourceInstantiationException{
334 MimeType mime = new MimeType("text","xml");
336 mimeString2ClassHandlerMap.put(mime.getType()+ "/" + mime.getSubtype(),
338 this);
339 mimeString2mimeTypeMap.put(mime.getType() + "/" + mime.getSubtype(), mime);
341 mimeString2mimeTypeMap.put("application/xml", mime);
343 suffixes2mimeTypeMap.put("xml",mime);
345 suffixes2mimeTypeMap.put("xhtm",mime);
346 suffixes2mimeTypeMap.put("xhtml",mime);
347 magic2mimeTypeMap.put("<?xml",mime);
349 setMimeType(mime);
351 return this;
352 }
354 }