source: trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/extractor/GMLExtractor.java@ 5800

Last change on this file since 5800 was 5800, checked in by cs025, 20 years ago

Adding gs3build

  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 KB
Line 
1package org.greenstone.gsdl3.gs3build.extractor;
2
3import java.io.FileReader;
4
5import org.xml.sax.XMLReader;
6import org.xml.sax.InputSource;
7import org.xml.sax.SAXException;
8import org.xml.sax.Attributes;
9import org.xml.sax.helpers.XMLReaderFactory;
10import org.xml.sax.helpers.DefaultHandler;
11
12import org.greenstone.gsdl3.gs3build.doctypes.DocumentID;
13import org.greenstone.gsdl3.gs3build.doctypes.DocumentInterface;
14import org.greenstone.gsdl3.gs3build.doctypes.GMLDocument;
15
16public class GMLExtractor implements ExtractorInterface
17{
18 /**
19 * An inner class to handle GML files
20 */
21 class GMLHandler extends DefaultHandler
22 { String file;
23 String label;
24 StringBuffer value;
25 boolean inElement;
26
27 GMLHandler()
28 { super();
29
30 this.file = null;
31 this.label = null;
32 this.value = null;
33 }
34
35 public void startElement(String URI, String localName, String qName, Attributes attributes)
36 { if (localName.equals("Filename"))
37 { this.value = new StringBuffer();
38 }
39 else if (localName.equals("Metadata"))
40 { this.label = attributes.getValue("name");
41 this.value = new StringBuffer();
42 }
43 }
44
45 public void endElement(String URI, String localName, String qName)
46 { if (localName.equals("Filename"))
47 { this.file = this.value.toString();
48 this.value = null;
49 }
50 else if (localName.equals("Metadata"))
51 { GMLExtractor.postMetadata(this.file, this.label, this.value.toString());
52 this.value = null;
53 this.label = null;
54 }
55 }
56
57 public void characters(char c[], int start, int length)
58 { if (this.label != null)
59 { String string = new String(c, start, length);
60 this.value.append(string);
61 }
62 }
63 }
64
65 /**
66 * Construct of extractor
67 */
68 public GMLExtractor()
69 { // Intentionally left blank
70 }
71
72 /**
73 * This extractor doesn't need to do any preparation/completion work,
74 * so this member function is empty.
75 */
76 public void configure(String outputDir)
77 { // Intentionally left blank
78 }
79
80 /**
81 * This extractor doesn't need to do any preparation/completion work,
82 * so this member function is empty.
83 */
84 public void startPass(int passNo)
85 { // Intentionally left blank
86 }
87
88 /**
89 * Process the document - for a GML document, this results in the
90 * decoration of other files, for other documents, it does nothing.
91 */
92 public void extractDocument(DocumentID docID, DocumentInterface document)
93 {
94 if (document.getDocumentType().equals(GMLDocument.GML_DOCUMENT_TYPE))
95 { // Extract the content from the GML file
96 try {
97 XMLReader reader = XMLReaderFactory.createXMLReader();
98 GMLHandler handler = new GMLHandler();
99 reader.setContentHandler(handler);
100 reader.setErrorHandler(handler);
101
102 // A GML document consists of one file only - get it.
103 FileReader fileReader = new FileReader(document.getDocumentFiles().get(0).toString());
104 reader.parse(new InputSource(fileReader));
105 }
106 catch (SAXException saxException)
107 { // TODO: log error
108 }
109 catch (java.io.FileNotFoundException fileException)
110 {
111 }
112 catch (java.io.IOException ioException)
113 {
114 }
115 // for each document post it to the corresponding document
116 }
117 }
118
119 protected static void postMetadata(String file, String value, String label)
120 {
121 }
122
123 /**
124 * This extractor doesn't need to do any preparation/completion work,
125 * so this member function is empty.
126 */
127 public void endPass(int passNo)
128 { // Intentionally left blank
129 }
130
131 /**
132 * This extractor is a simple, single-pass extractor
133 *
134 * @see: org.greenstone.gsdl3.gs3build.extractor.ExtractorInterface:getNumberOfPasses
135 */
136 public int getNumberOfPasses()
137 { return 1;
138 }
139}
Note: See TracBrowser for help on using the repository browser.