Changeset 3615
- Timestamp:
- 2002-12-03T16:20:18+13:00 (21 years ago)
- Location:
- trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/DocumentStream.java
r3593 r3615 44 44 * @return the next document 45 45 */ 46 public Document nextDocument() ;46 public Document nextDocument() throws Exception ; 47 47 /** 48 48 * Is there a next document? … … 51 51 * @return true if there is a next document 52 52 */ 53 public boolean hasNextDocument() ;53 public boolean hasNextDocument() throws Exception ; 54 54 55 55 } -
trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/FilesDocumentStream.java
r3593 r3615 22 22 import org.w3c.dom.Document; 23 23 import org.xml.sax.InputSource; 24 import javax.xml.transform.Source; 25 import javax.xml.transform.Transformer; 26 import javax.xml.transform.TransformerFactory; 27 import javax.xml.transform.dom.DOMSource; 28 import javax.xml.transform.stream.StreamResult; 29 import org.apache.xerces.dom.DocumentImpl; 30 import org.apache.xerces.dom.ElementImpl; 31 import org.apache.xerces.dom.TextImpl; 32 import org.w3c.dom.Document; 24 33 25 34 // other java classes 26 35 import java.io.File; 36 import java.io.FilenameFilter; 27 37 import java.io.FileReader; 28 38 import java.io.IOException; … … 57 67 File file = new File(filename); 58 68 if (file.exists() && file.isDirectory()) 59 directories.add(file );69 directories.add(file.getAbsolutePath()); 60 70 else 61 files.add(file );71 files.add(file.getAbsolutePath()); 62 72 } 63 73 /** … … 69 79 public FilesDocumentStream(File file) { 70 80 if (file.exists() && file.isDirectory()) 71 directories.add(file );81 directories.add(file.getAbsolutePath()); 72 82 else 73 files.add(file); 83 files.add(file.getAbsolutePath()); 84 } 85 86 /** 87 * A small inner class to filter out those files that don't 88 * end in ".xml" or ".XML" 89 */ 90 class XMLFilenameFilter implements FilenameFilter { 91 /** Tests if a specified file should be included in a file list. */ 92 public boolean accept(File dir, String name) { 93 String target = ".xml"; 94 if (name.length() < target.length()) 95 return false; 96 String extension = name.substring(name.length() - 4, name.length()); 97 //System.out.println(name + " ==>> " + extension); 98 if (extension.equalsIgnoreCase(target)) { 99 return true; 100 } else { 101 return false; 102 } 103 } 74 104 } 75 105 … … 80 110 */ 81 111 protected void expandIfNecessary() { 82 while (files.size() < 1 || directories.size() > 1) { 83 File file = (File) directories.elementAt(directories.size() - 1); 84 directories.removeElementAt(directories.size() - 1); 85 if (!file.exists()) throw new Error ("error in expand: expecting a directory"); 86 File[] fileArray = file.listFiles(); 112 while (directories.size() > 0) { 113 String dirname = (String) directories.elementAt(0); 114 directories.removeElement(dirname); 115 File dir = new File(dirname); 116 if (!dir.exists()) throw new Error ("error in expand: expecting a directory " + dirname); 117 String[] fileArray = dir.list(); 87 118 for (int i=0;i<fileArray.length;i++){ 88 if (fileArray[i].exists() && fileArray[i].isDirectory()) 89 directories.add(fileArray[i]); 90 else 91 files.add(fileArray[i]); 119 File file = new File(dir, fileArray[i]); 120 if (file.exists() && file.isDirectory()) 121 directories.add(file.getPath()); 92 122 } 123 124 fileArray = dir.list(new XMLFilenameFilter()); 125 for (int i=0;i<fileArray.length;i++){ 126 File file = new File(dir, fileArray[i]); 127 if (file.exists() && !file.isDirectory()) 128 files.add(file.getPath()); 129 } 130 if (files.size() > 0) 131 return; 93 132 } 94 133 } 95 134 96 135 /** 97 136 * Returns the next document … … 100 139 * @return the next document 101 140 */ 102 public Document nextDocument() { 103 try { 104 if (!hasNextDocument()) throw new Error("Doesn't have another Document"); 105 File file = (File) files.elementAt(files.size() - 1); 106 files.removeElementAt(files.size() - 1); 107 108 Reader reader = new FileReader(file); 109 InputSource xml_source = new InputSource(reader); 110 111 XMLUtil.getDOMParser().parse(xml_source); 112 Document doc = XMLUtil.getDOMParser().getDocument(); 113 114 return doc; 115 } catch (org.xml.sax.SAXException s) { 116 System.err.println("SAXException in nextDocument()"); 117 throw new Error(s.toString()); 118 } catch (IOException e) { 119 System.err.println("IOException in nextDocument()"); 120 throw new Error(e.toString()); 121 } 141 public Document nextDocument() 142 throws Exception { 143 144 if (!hasNextDocument()) throw new Error("Doesn't have another Document"); 145 146 String filename = (String) files.elementAt(files.size() - 1); 147 files.removeElementAt(files.size() - 1); 148 File file = new File(filename); 149 150 Reader reader = new FileReader(file); 151 InputSource xml_source = new InputSource(reader); 152 153 XMLUtil.getDOMParser().parse(xml_source); 154 Document doc = XMLUtil.getDOMParser().getDocument(); 155 156 return doc; 122 157 } 123 158 … … 129 164 */ 130 165 131 public boolean hasNextDocument() {166 public boolean hasNextDocument() throws Exception { 132 167 expandIfNecessary(); 133 return (files.size() > 1);168 return (files.size() > 0); 134 169 } 135 170 /** … … 141 176 */ 142 177 143 public static void main(String args[]) throws IOException178 public static void main(String args[]) throws Exception 144 179 { 180 181 StreamResult result = new StreamResult(System.out); 182 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 183 184 FilesDocumentStream stream = new FilesDocumentStream("."); 185 while (stream.hasNextDocument()) { 186 Document document = stream.nextDocument(); 187 Transformer transformer = transformerFactory.newTransformer(); 188 Source source = new DOMSource(document); 189 transformer.transform(source,result); 190 System.out.println(); 191 System.out.println(); 192 } 145 193 146 } 147 194 } 148 195 } 149 196 -
trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/GeneratedDocumentStream.java
r3593 r3615 1 1 /* 2 * XMLConverter.java2 * GenerateDocumentStream.java 3 3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org 4 4 * … … 86 86 this.next = next; 87 87 } 88 /** 89 * Custom constructor 90 * 91 * @param first the first letter in the alphabet 92 * @param last the last letter in the alphabet 93 */ 94 public GeneratedDocumentStream(char first, char last) { 95 this.incrementer = new IncrementRange(first,last); 96 } 88 97 89 98 -
trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/QueryDocumentStream.java
r3593 r3615 166 166 * @see org.w3c.dom.Document 167 167 */ 168 public Document nextDocument() {168 public Document nextDocument() throws Exception { 169 169 170 170 if (hasNextDocument() == false) … … 179 179 * @return true if there are more docs 180 180 */ 181 public boolean hasNextDocument() {181 public boolean hasNextDocument() throws Exception { 182 182 lookInStream(); 183 183 return (cached != null); … … 193 193 * @see blank 194 194 */ 195 protected boolean lookInStream() {195 protected boolean lookInStream() throws Exception { 196 196 197 197 while (stream.hasNextDocument() && cached == null) {
Note:
See TracChangeset
for help on using the changeset viewer.