1 | /*
|
---|
2 | * GenerateDocumentStream.java
|
---|
3 | * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 | package org.greenstone.gsdl3.selfContained;
|
---|
20 |
|
---|
21 | // XML classes
|
---|
22 | import javax.xml.transform.Source;
|
---|
23 | import javax.xml.transform.Transformer;
|
---|
24 | import javax.xml.transform.TransformerFactory;
|
---|
25 | import javax.xml.transform.dom.DOMSource;
|
---|
26 | import javax.xml.transform.stream.StreamResult;
|
---|
27 | import org.apache.xerces.dom.DocumentImpl;
|
---|
28 | import org.apache.xerces.dom.ElementImpl;
|
---|
29 | import org.apache.xerces.dom.TextImpl;
|
---|
30 | import org.w3c.dom.Document;
|
---|
31 |
|
---|
32 |
|
---|
33 | // other java classes
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * GeneratedDocumentStream generates an unbounded stream of XML documents
|
---|
37 | * Each containing a single string.
|
---|
38 | *
|
---|
39 | * Based as a wrapper around an Increment object
|
---|
40 | *
|
---|
41 | * @author <a href="http://www.cs.waikato.ac.nz/~say1/">stuart yeates</a> (<a href="mailto:[email protected]">s
|
---|
42 | [email protected]</a>) at the <a href="http://www.nzdl.org">New Zealand Digital Library</a>
|
---|
43 | * @version $Revision: 3615 $
|
---|
44 | * @see DocumentStream
|
---|
45 | * @see Increment
|
---|
46 | *
|
---|
47 | */
|
---|
48 | public class GeneratedDocumentStream implements DocumentStream {
|
---|
49 |
|
---|
50 | /** The next string */
|
---|
51 | protected String next = "";
|
---|
52 | /** The underlying Increment object */
|
---|
53 | protected Increment incrementer = new IncrementRange();
|
---|
54 |
|
---|
55 | /** The default constructor. Generates ASCII-only documents */
|
---|
56 | public GeneratedDocumentStream() {
|
---|
57 |
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Custom constructor
|
---|
62 | *
|
---|
63 | * @param incrementer the incrementer to use (controls the character set to use)
|
---|
64 | */
|
---|
65 | public GeneratedDocumentStream(Increment incrementer) {
|
---|
66 | this.incrementer = incrementer;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Custom constructor
|
---|
71 | *
|
---|
72 | * @param next the string for the first document (must suit the incrementor)
|
---|
73 | */
|
---|
74 | public GeneratedDocumentStream(String next) {
|
---|
75 | this.next = next;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Custom constructor
|
---|
80 | *
|
---|
81 | * @param incrementer the incrementer to use (controls the character set to use)
|
---|
82 | * @param next the string for the first document (must suit the incrementor)
|
---|
83 | */
|
---|
84 | public GeneratedDocumentStream(Increment incrementer, String next) {
|
---|
85 | this.incrementer = incrementer;
|
---|
86 | this.next = next;
|
---|
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 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Generates the nest document in the sequence
|
---|
101 | *
|
---|
102 | * @exception java.io.Exception when something goes wrong in the XML stuff
|
---|
103 | * @return the next Document
|
---|
104 | * @see org.w3c.dom.Document
|
---|
105 | */
|
---|
106 | public Document nextDocument() {
|
---|
107 |
|
---|
108 |
|
---|
109 | DocumentImpl doc = new DocumentImpl(true);
|
---|
110 |
|
---|
111 | ElementImpl root = new ElementImpl(doc,"document");
|
---|
112 | TextImpl text = new TextImpl(doc,next);
|
---|
113 |
|
---|
114 | doc.appendChild(root);
|
---|
115 | root.appendChild(text);
|
---|
116 |
|
---|
117 | next = incrementer.incrementString(next);
|
---|
118 | return doc;
|
---|
119 |
|
---|
120 | }
|
---|
121 | /**
|
---|
122 | * Are there more documents ?
|
---|
123 | *
|
---|
124 | * @return yes
|
---|
125 | */
|
---|
126 | public boolean hasNextDocument(){
|
---|
127 | return true;
|
---|
128 | }
|
---|
129 | /**
|
---|
130 | * Generates an unbounded stream of documents
|
---|
131 | */
|
---|
132 | public static void main(String args[]) throws Exception
|
---|
133 | {
|
---|
134 |
|
---|
135 | StreamResult result = new StreamResult(System.out);
|
---|
136 | TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
---|
137 |
|
---|
138 | GeneratedDocumentStream stream = new GeneratedDocumentStream();
|
---|
139 | while (stream.hasNextDocument()) {
|
---|
140 | Document document = stream.nextDocument();
|
---|
141 | Transformer transformer = transformerFactory.newTransformer();
|
---|
142 | Source source = new DOMSource(document);
|
---|
143 | transformer.transform(source,result);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | }
|
---|
148 |
|
---|