Changeset 3593
- Timestamp:
- 2002-11-30T15:07:07+13:00 (21 years ago)
- Location:
- trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/DocumentStream.java
r3548 r3593 23 23 24 24 // other java classes 25 import java.io.Serializable; 25 26 26 27 … … 35 36 * 36 37 */ 37 interface DocumentStream{38 public interface DocumentStream extends Cloneable, Serializable { 38 39 39 40 /** … … 43 44 * @return the next document 44 45 */ 45 public Document nextDocument() throws Exception;46 public Document nextDocument(); 46 47 /** 47 48 * Is there a next document? … … 50 51 * @return true if there is a next document 51 52 */ 52 public boolean hasNextDocument() throws Exception;53 public boolean hasNextDocument(); 53 54 54 55 } -
trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/FilesDocumentStream.java
r3588 r3593 41 41 * @see DocumentStream 42 42 */ 43 class DocumentStreamFromFiles implements DocumentStream { 43 public class FilesDocumentStream 44 implements DocumentStream { 44 45 /** the files we've found so far */ 45 46 Vector files = new Vector(); … … 53 54 * @param filename the filename of the file or directory 54 55 */ 55 public DocumentStreamFromFiles(String filename) throws IOException{56 public FilesDocumentStream(String filename) { 56 57 File file = new File(filename); 57 58 if (file.exists() && file.isDirectory()) … … 66 67 * @param file a handle to the file 67 68 */ 68 public DocumentStreamFromFiles(File file) {69 public FilesDocumentStream(File file) { 69 70 if (file.exists() && file.isDirectory()) 70 71 directories.add(file); … … 78 79 * @exception java.io.IOException when the underlying file access does 79 80 */ 80 protected void expandIfNecessary() throws IOException{81 protected void expandIfNecessary() { 81 82 while (files.size() < 1 || directories.size() > 1) { 82 83 File file = (File) directories.elementAt(directories.size() - 1); 83 84 directories.removeElementAt(directories.size() - 1); 84 if (!file.exists()) throw new IOException("error in expand: expecting a directory");85 if (!file.exists()) throw new Error ("error in expand: expecting a directory"); 85 86 File[] fileArray = file.listFiles(); 86 87 for (int i=0;i<fileArray.length;i++){ … … 99 100 * @return the next document 100 101 */ 101 public Document nextDocument() throws Exception { 102 if (!hasNextDocument()) throw new Error("Doesn't have another Document"); 103 File file = (File) files.elementAt(files.size() - 1); 104 files.removeElementAt(files.size() - 1); 105 106 Reader reader = new FileReader(file); 107 InputSource xml_source = new InputSource(reader); 108 109 XMLUtil.getDOMParser().parse(xml_source); 110 Document doc = XMLUtil.getDOMParser().getDocument(); 111 112 return doc; 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 } 113 122 } 114 123 … … 120 129 */ 121 130 122 public boolean hasNextDocument() throws IOException{131 public boolean hasNextDocument() { 123 132 expandIfNecessary(); 124 133 return (files.size() > 1); -
trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/GeneratedDocumentStream.java
r3548 r3593 95 95 * @see org.w3c.dom.Document 96 96 */ 97 public Document nextDocument() throws Exception{97 public Document nextDocument() { 98 98 99 99 -
trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/Increment.java
r3548 r3593 46 46 * @see Increment 47 47 */ 48 abstract String incrementString(String str) throws Exception;48 abstract String incrementString(String str); 49 49 50 50 /** … … 56 56 * @see Increment 57 57 */ 58 abstract long incrementStream(Reader rdr, Writer wtr) throws Exception;58 abstract long incrementStream(Reader rdr, Writer wtr); 59 59 60 60 /** -
trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/IncrementRange.java
r3548 r3593 42 42 implements Cloneable, Serializable 43 43 { 44 /**44 /** 45 45 * Create an IncrementRange which counts between first and last inclusive 46 46 * … … 83 83 * Increment a stream of characters, reading from the reader and writing the incremented characters to the writer 84 84 * 85 * @exception java.io.E xceptionif any of the characters in the string are outside the range of first-last85 * @exception java.io.Error if any of the characters in the string are outside the range of first-last 86 86 * @param str the string to be incremented 87 87 * @return the incremented string 88 88 * @see Increment 89 89 */ 90 long incrementStream(Reader rdr, Writer wtr) 91 t hrows Exception{92 long count = 0; // the return value90 long incrementStream(Reader rdr, Writer wtr) { 91 try { 92 long count = 0; // the return value 93 93 94 int i = rdr.read();95 boolean carry = true;94 int i = rdr.read(); 95 boolean carry = true; 96 96 97 while (i != -1) {98 97 while (i != -1) { 98 char c = (char) i; 99 99 100 101 102 throw new Exception("Character '" + c + "' is ouside range");100 // check the validity of the character we're about to increment 101 if (c < first || c > last) 102 throw new Error("Character '" + c + "' is ouside range"); 103 103 104 if (c == last) { 105 if (carry == true) { 106 wtr.write(first); 107 carry = true; 104 if (c == last) { 105 if (carry == true) { 106 wtr.write(first); 107 carry = true; 108 } else { 109 wtr.write(c); 110 carry = false; 111 } 108 112 } else { 109 wtr.write(c); 110 carry = false; 113 if (carry == true) { 114 wtr.write(++c); 115 carry = false; 116 } else { 117 wtr.write(c); 118 carry = false; 119 } 111 120 } 112 } else { 113 if (carry == true) { 114 wtr.write(++c); 115 carry = false; 116 } else { 117 wtr.write(c); 118 carry = false; 119 } 120 } 121 count++; 122 123 // read the next character 124 i = rdr.read(); 125 } 126 if (carry == true) { 127 wtr.write(first); 128 count++; 129 } 130 return count; 121 count++; 122 123 // read the next character 124 i = rdr.read(); 125 } 126 if (carry == true) { 127 wtr.write(first); 128 count++; 129 } 130 return count; 131 } catch (IOException e) { 132 System.err.println("IOException in incrementStream()"); 133 throw new Error(e.toString()); 134 } 131 135 } 132 136 … … 134 138 * Increment a string of characters 135 139 * 136 * @exception java. io.Exceptionif any of the characters in the string are outside the range of first-last140 * @exception java.lang.Error if any of the characters in the string are outside the range of first-last 137 141 * @param str the string to be incremented 138 142 * @return the incremented string 139 143 * @see Increment 140 144 */ 141 public final String incrementString(String str) 142 throws Exception { 145 public final String incrementString(String str) { 143 146 StringBuffer buffer = new StringBuffer(); 144 147 if (str == "") { … … 150 153 char c = str.charAt(i); 151 154 if (c < first || c > last) 152 throw new E xception("Character '" + c + "' is ouside range");155 throw new Error("Character '" + c + "' is ouside range"); 153 156 154 157 if (c == last) { -
trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/QueryDocumentStream.java
r3592 r3593 1 1 /* 2 * QueryDocumentStream.java .java2 * QueryDocumentStream.java 3 3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org 4 4 * … … 25 25 import javax.xml.transform.TransformerException; 26 26 import javax.xml.transform.TransformerFactory; 27 import javax.xml.transform.Result;28 import javax.xml.transform.Source;29 27 import javax.xml.transform.dom.DOMResult; 30 28 import javax.xml.transform.dom.DOMSource; 31 29 import javax.xml.transform.stream.StreamResult; 32 30 import javax.xml.transform.stream.StreamSource; 33 import org.apache.xalan.xslt.*;34 import org.apache.xerces.dom.DocumentImpl;35 import org.apache.xerces.dom.ElementImpl;36 import org.apache.xerces.dom.TextImpl;37 31 import org.w3c.dom.Document; 38 import org.w3c.dom.Node;39 import org.w3c.dom.NodeList;40 32 41 33 // other java classes … … 44 36 /** 45 37 * QueryDocumentStream takes another document stream and removes 46 * from the stream all documents which fail to match a XSLT query 47 * 38 * from the stream all documents which fail to match a XSLT query. 39 * The default query returns only those documents containing the 40 * string "the". 48 41 * 49 42 * @author <a href="http://www.cs.waikato.ac.nz/~say1/">stuart yeates</a> (<a href="mailto:[email protected]">s … … 67 60 " </xsl:template>" + 68 61 "</xsl:stylesheet>"; 69 62 /** The first part of the default quesy for a string other than "the" */ 70 63 public static final String DEFAULT_QUERY_STRING_1_1 = 71 64 "<xsl:stylesheet version=\"1.0\" " + … … 76 69 " <xsl:if test=\"contains(.,'"; 77 70 71 /** The second part of the default quesy for a string other than "the" */ 78 72 public static final String DEFAULT_QUERY_STRING_1_2 = 79 73 "')\">" + … … 84 78 85 79 86 /** The next string*/80 /** The query */ 87 81 protected StreamSource query = new StreamSource(new StringReader(DEFAULT_QUERY_STRING)); 88 82 … … 92 86 /** A cached Document */ 93 87 protected Document cached = null; 88 89 /** The transformer factory */ 94 90 TransformerFactory transformerFactory = null; 95 Transformer transformer = null; 91 92 /** The transformer */ 93 Transformer transformer = null; 96 94 97 95 /** Uses the default query (looking for the) over the default DocumentStream (all ascii strings) */ … … 161 159 } 162 160 } 163 164 165 166 167 161 /** 168 162 * Generates the nest document in the sequence … … 172 166 * @see org.w3c.dom.Document 173 167 */ 174 public Document nextDocument() throws Exception{168 public Document nextDocument() { 175 169 176 170 if (hasNextDocument() == false) 177 throw new E xception("no more docs");171 throw new Error("no more docs"); 178 172 Document result = cached; 179 173 cached = null; … … 185 179 * @return true if there are more docs 186 180 */ 187 public boolean hasNextDocument() throws Exception{188 lookInStream() 189 181 public boolean hasNextDocument() { 182 lookInStream(); 183 return (cached != null); 190 184 } 191 185 /** … … 199 193 * @see blank 200 194 */ 201 protected boolean lookInStream() throws Exception{195 protected boolean lookInStream() { 202 196 203 197 while (stream.hasNextDocument() && cached == null) { … … 210 204 try { 211 205 212 transformer.transform(input, output); 213 214 // if the transformed document is not empty don't cache it 215 if (output.getNode().getFirstChild() != null) 216 cached = (Document) output.getNode(); 217 206 transformer.transform(input, output); 207 208 // if the transformed document is not empty don't cache it 209 if (output.getNode().getFirstChild() != null) 210 cached = (Document) output.getNode(); 211 212 } catch (TransformerConfigurationException e) { 213 System.err.println("XMLTransformer: couldn't create transformer object: "+e.getMessage()); 218 214 } catch (TransformerException e) { 219 215 System.err.println("XMLTransformer: couldn't transform the source: " + e.getMessage()); … … 228 224 if (false) { // debugging info 229 225 230 StreamResult result = new StreamResult(System.out); 231 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 232 Transformer transformer = transformerFactory.newTransformer(); 233 234 Source source = new DOMSource(candidate); 235 transformer.transform(source,result); 236 237 System.out.print(" ==> "); 238 239 if (cached != null) { 240 Source source2 = new DOMSource(cached); 241 transformer.transform(source2,result); 242 } 243 System.out.println(); 244 System.out.println("======================================================"); 226 try { 227 StreamResult result = new StreamResult(System.out); 228 Transformer transformer = transformerFactory.newTransformer(); 229 230 Source source = new DOMSource(candidate); 231 transformer.transform(source,result); 232 233 System.out.print(" ==> "); 234 235 if (cached != null) { 236 Source source2 = new DOMSource(cached); 237 transformer.transform(source2,result); 238 } 239 System.out.println(); 240 System.out.println("======================================================"); 241 242 } catch (TransformerConfigurationException e) { 243 System.err.println("XMLTransformer: couldn't create transformer object: "+e.getMessage()); 244 } catch (TransformerException e) { 245 System.err.println("XMLTransformer: couldn't transform the source: " + e.getMessage()); 246 cached = null; 247 return false; 248 } catch (Exception e) { 249 System.err.println("Exception: " + e.getMessage()); 250 cached = null; 251 return false; 252 } 245 253 246 254 } 247 255 } 248 return cached == null; 249 256 return cached == null; 250 257 } 251 258
Note:
See TracChangeset
for help on using the changeset viewer.