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 xmlParser.parse(doc.getSourceUrl().toString(), gateXmlHandler);
135 gateXmlHandler.removeStatusListener(statusListener);
136 }else{
137 xmlDocHandler = new XmlDocumentHandler( doc,
139 this.markupElementsMap,
140 this.element2StringMap);
141 xmlDocHandler.addStatusListener(statusListener);
143 xmlDocHandler.setRepositioningInfo(repInfo);
145 xmlDocHandler.setAmpCodingInfo(ampCodingInfo);
147
148
152
155 org.xml.sax.XMLReader newxmlParser = xmlParser.getXMLReader();
156 newxmlParser.setFeature("http://xml.org/sax/features/validation", false);
162 newxmlParser.setFeature("http://xml.org/sax/features/namespaces", true);
165 newxmlParser.setContentHandler(xmlDocHandler);
166 newxmlParser.setErrorHandler(xmlDocHandler);
167 newxmlParser.setDTDHandler(xmlDocHandler);
168 newxmlParser.setEntityResolver(xmlDocHandler);
169 newxmlParser.parse(doc.getSourceUrl().toString());
170 ((DocumentImpl) doc).setNextAnnotationId(
172 xmlDocHandler.getCustomObjectsId());
173 xmlDocHandler.removeStatusListener(statusListener);
174 } } catch (ParserConfigurationException e){
176 throw
177 new DocumentFormatException("XML parser configuration exception ", e);
178 } catch (SAXException e){
179 doc.getFeatures().put("parsingError", new Boolean(true));
180
181 Boolean bThrow = (Boolean)
182 doc.getFeatures().get(GateConstants.THROWEX_FORMAT_PROPERTY_NAME);
183
184 if(bThrow != null && bThrow.booleanValue()) {
185 throw new DocumentFormatException(e);
187 }
188 else {
189 Out.println("Warning: Document remains unparsed. \n"
190 +"\n Stack Dump: ");
191 e.printStackTrace(Out.getPrintWriter());
192 }
194 } catch (IOException e){
195 throw new DocumentFormatException("I/O exception for " +
196 doc.getSourceUrl().toString());
197 }finally{
198 if(gateXmlHandler != null)
199 gateXmlHandler.removeStatusListener(statusListener);
200 if (xmlDocHandler != null)
201 xmlDocHandler.removeStatusListener(statusListener);
202 } }
205
208 private void parseDocumentWithoutURL(gate.Document aDocument,
209 RepositioningInfo repInfo,
210 RepositioningInfo ampCodingInfo)
211 throws DocumentFormatException {
212
213 XmlDocumentHandler xmlDocHandler = null;
214 StatusListener statusList = new StatusListener(){
216 public void statusChanged(String text){
217 fireStatusChanged(text);
219 }
220 };
221 try{
222 Reader reader = new StringReader(aDocument.getContent().toString());
223 InputSource is = new InputSource(reader);
229
230
231 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
236 saxParserFactory.setValidating(false);
239 saxParserFactory.setNamespaceAware(true);
241 SAXParser xmlParser = saxParserFactory.newSAXParser();
243
244 xmlDocHandler = new XmlDocumentHandler(aDocument,
246 this.markupElementsMap,
247 this.element2StringMap);
248 xmlDocHandler.addStatusListener(statusList);
250 xmlDocHandler.setRepositioningInfo(repInfo);
252 xmlDocHandler.setAmpCodingInfo(ampCodingInfo);
254
258
259 org.xml.sax.XMLReader newxmlParser = xmlParser.getXMLReader();
262 newxmlParser.setFeature("http://xml.org/sax/features/validation", false);
268 newxmlParser.setFeature("http://xml.org/sax/features/namespaces", true);
271 newxmlParser.setContentHandler(xmlDocHandler);
272 newxmlParser.setErrorHandler(xmlDocHandler);
273 newxmlParser.setDTDHandler(xmlDocHandler);
274 newxmlParser.setEntityResolver(xmlDocHandler);
275 newxmlParser.parse(is);
276
278 ((DocumentImpl) aDocument).setNextAnnotationId(
279 xmlDocHandler.getCustomObjectsId());
280 } catch (ParserConfigurationException e){
281 throw new DocumentFormatException(
282 "XML parser configuration exception ", e);
283 } catch (SAXException e){
284 throw new DocumentFormatException(e);
285 } catch (IOException e){
286 throw new DocumentFormatException(e);
287 }finally{
288 xmlDocHandler.removeStatusListener(statusList);
290 } }
293
294 public Resource init() throws ResourceInstantiationException{
295 MimeType mime = new MimeType("text","xml");
297 mimeString2ClassHandlerMap.put(mime.getType()+ "/" + mime.getSubtype(),
299 this);
300 mimeString2mimeTypeMap.put(mime.getType() + "/" + mime.getSubtype(), mime);
302 mimeString2mimeTypeMap.put("application/xml", mime);
304 suffixes2mimeTypeMap.put("xml",mime);
306 suffixes2mimeTypeMap.put("xhtm",mime);
307 suffixes2mimeTypeMap.put("xhtml",mime);
308 magic2mimeTypeMap.put("<?xml",mime);
310 setMimeType(mime);
312 return this;
313 }
315 }