-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathTransformTest.java
More file actions
153 lines (127 loc) · 4.21 KB
/
Copy pathTransformTest.java
File metadata and controls
153 lines (127 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package transform;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import javax.xml.transform.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
/**
* This program demonstrates XSL transformations. It applies a transformation to a set of employee
* records. The records are stored in the file employee.dat and turned into XML format. Specify the
* stylesheet on the command line, e.g.
* java transform.TransformTest transform/makeprop.xsl
* @version 1.02 2012-06-04
* @author Cay Horstmann
*/
public class TransformTest
{
public static void main(String[] args) throws Exception
{
Path path;
if (args.length > 0) path = Paths.get(args[0]);
else path = Paths.get("transform", "makehtml.xsl");
try (InputStream styleIn = Files.newInputStream(path))
{
StreamSource styleSource = new StreamSource(styleIn);
Transformer t = TransformerFactory.newInstance().newTransformer(styleSource);
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "xml");
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
try (InputStream docIn = Files.newInputStream(Paths.get("transform", "employee.dat")))
{
t.transform(new SAXSource(new EmployeeReader(), new InputSource(docIn)),
new StreamResult(System.out));
}
}
}
}
/**
* This class reads the flat file employee.dat and reports SAX parser events to act as if it was
* parsing an XML file.
*/
class EmployeeReader implements XMLReader
{
private ContentHandler handler;
public void parse(InputSource source) throws IOException, SAXException
{
InputStream stream = source.getByteStream();
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
String rootElement = "staff";
AttributesImpl atts = new AttributesImpl();
if (handler == null) throw new SAXException("No content handler");
handler.startDocument();
handler.startElement("", rootElement, rootElement, atts);
String line;
while ((line = in.readLine()) != null)
{
handler.startElement("", "employee", "employee", atts);
StringTokenizer t = new StringTokenizer(line, "|");
handler.startElement("", "name", "name", atts);
String s = t.nextToken();
handler.characters(s.toCharArray(), 0, s.length());
handler.endElement("", "name", "name");
handler.startElement("", "salary", "salary", atts);
s = t.nextToken();
handler.characters(s.toCharArray(), 0, s.length());
handler.endElement("", "salary", "salary");
atts.addAttribute("", "year", "year", "CDATA", t.nextToken());
atts.addAttribute("", "month", "month", "CDATA", t.nextToken());
atts.addAttribute("", "day", "day", "CDATA", t.nextToken());
handler.startElement("", "hiredate", "hiredate", atts);
handler.endElement("", "hiredate", "hiredate");
atts.clear();
handler.endElement("", "employee", "employee");
}
handler.endElement("", rootElement, rootElement);
handler.endDocument();
}
public void setContentHandler(ContentHandler newValue)
{
handler = newValue;
}
public ContentHandler getContentHandler()
{
return handler;
}
// the following methods are just do-nothing implementations
public void parse(String systemId) throws IOException, SAXException
{
}
public void setErrorHandler(ErrorHandler handler)
{
}
public ErrorHandler getErrorHandler()
{
return null;
}
public void setDTDHandler(DTDHandler handler)
{
}
public DTDHandler getDTDHandler()
{
return null;
}
public void setEntityResolver(EntityResolver resolver)
{
}
public EntityResolver getEntityResolver()
{
return null;
}
public void setProperty(String name, Object value)
{
}
public Object getProperty(String name)
{
return null;
}
public void setFeature(String name, boolean value)
{
}
public boolean getFeature(String name)
{
return false;
}
}