Skip to content

Package: XMLToFastInfosetSAXSerializer

XMLToFastInfosetSAXSerializer

nameinstructionbranchcomplexitylinemethod
XMLToFastInfosetSAXSerializer()
M: 21 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
displayUsageAndExit()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getDOMSource(File)
M: 29 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
getSAXResult(File)
M: 33 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
main(String[])
M: 36 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
write(File, File)
M: 47 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3: *
4: * Copyright (c) 2004-2018 Oracle and/or its affiliates. All rights reserved.
5: *
6: * Oracle licenses this file to You under the Apache License, Version 2.0
7: * (the "License"); you may not use this file except in compliance with
8: * the License. You may obtain a copy of the License at
9: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package samples.transform;
20:
21: import com.sun.xml.fastinfoset.sax.SAXDocumentSerializer;
22: import java.io.BufferedOutputStream;
23: import java.io.File;
24: import java.io.FileInputStream;
25: import java.io.FileOutputStream;
26: import java.io.IOException;
27: import javax.xml.parsers.DocumentBuilder;
28: import javax.xml.parsers.DocumentBuilderFactory;
29: import javax.xml.transform.Transformer;
30: import javax.xml.transform.TransformerFactory;
31: import javax.xml.transform.dom.DOMSource;
32: import javax.xml.transform.sax.SAXResult;
33: import org.w3c.dom.Document;
34:
35:
36: /** <p>Serializes an XML input stream into FI document using
37: * SAXDocumentSerializer defined in the fastinfoset.sax package.</p>
38: * The sample demonstrates how to use SAXDocumentSerializer as a SAX handler and JAXP
39: * transformer to convert an XML file into a FI document. As shown in the sample,
40: * transforming a DOM source to SAX Result involves very little coding. However, the process
41: * may not be efficient due to the construction of DOM source.
42: *
43: * In the sample, a DOMSource is constructed out of an XML file input (see method getDOMSource)
44: * and a SAXResult instantiated using an instance of SAXDocumentSerializer as the
45: * handler which takes a FI document as OutputStream (see method getSAXResult).
46: * The sample then calls transformer's tranform method to convert the XML file into the FI
47: * document.
48: */
49: @SuppressWarnings("CallToThreadDumpStack")
50: public class XMLToFastInfosetSAXSerializer {
51: Transformer _transformer;
52: DocumentBuilder _docBuilder;
53: DOMSource _source = null;
54: SAXResult _result = null;
55:
56: /** Creates a new instance of FISerializer */
57: public XMLToFastInfosetSAXSerializer() {
58: try {
59: // get a transformer and document builder
60: _transformer = TransformerFactory.newInstance().newTransformer();
61: _docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
62: } catch (Exception e) {
63: e.printStackTrace();
64: }
65: }
66:
67:
68: /** Construct a DOMSource with a file.
69: *
70: * @param input the XML file input
71: */
72: void getDOMSource(File input) throws IOException {
73: FileInputStream fis = null;
74: try {
75: fis = new FileInputStream(input);
76: Document document = _docBuilder.parse(fis);
77: fis.close();
78: _source = new DOMSource(document);
79: } catch (Exception e) {
80:• if (fis != null) {
81: fis.close();
82: }
83: e.printStackTrace();
84: }
85:
86: }
87:
88: /** Initialize a SAXResult and set its handers.
89: *
90: * @param output FI document output
91: */
92: void getSAXResult(File output) {
93: try {
94: BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(output));
95: SAXDocumentSerializer serializer = new SAXDocumentSerializer();
96: serializer.setOutputStream(fos);
97:
98: _result = new SAXResult();
99: _result.setHandler(serializer);
100: _result.setLexicalHandler(serializer);
101:
102: } catch (Exception e) {
103: e.printStackTrace();
104: }
105: }
106:
107: /** Transform an XML file into a FI document.
108: *
109: * @param input an XML file input
110: * @param output the FI document output
111: */
112: public void write(File input, File output) throws IOException {
113: // construct a DOMSource from the input file
114: getDOMSource(input);
115: // Initialize a SAXResult object
116: getSAXResult(output);
117:
118:• if (_source != null && _result != null) {
119: try {
120: System.out.println("Transforming "+input.getName()+ " into " + output.getName());
121: // Transform the XML input file into a FI document
122: _transformer.transform(_source, _result);
123: } catch (Exception e) {
124: e.printStackTrace();
125: }
126: System.out.println("\ndone.");
127: } else {
128: System.out.println("Source or Result could not be null.");
129: }
130: }
131:
132: /** Starts the sample
133: * @param args XML input file name and FI output document name
134: */
135: public static void main(String[] args) {
136:• if (args.length < 1 || args.length > 4) {
137: displayUsageAndExit();
138: }
139:
140: try {
141: //XML input file, such as ./data/inv100.xml
142: File input = new File(args[0]);
143: //FastInfoset output file, such as ./data/inv100_sax.finf.
144: File ouput = new File(args[1]);
145: XMLToFastInfosetSAXSerializer docSerializer = new XMLToFastInfosetSAXSerializer();
146: docSerializer.write(input, ouput);
147: } catch (Exception e) {
148: e.printStackTrace();
149: }
150: }
151:
152: private static void displayUsageAndExit() {
153: System.err.println("Usage: ant FISAXSerialixer or samples.sax.FISerializer XML_input_file FI_output_file");
154: System.exit(1);
155: }
156:
157: }