KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > stax > FastInfosetSerializer


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * 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, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39 package samples.stax;
40
41 import java.io.File JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.io.FileInputStream JavaDoc;
44 import java.io.FileOutputStream JavaDoc;
45 import java.io.BufferedInputStream JavaDoc;
46 import java.io.BufferedOutputStream JavaDoc;
47 import java.io.FileNotFoundException JavaDoc;
48
49 import javax.xml.stream.XMLStreamWriter;
50 import javax.xml.stream.XMLOutputFactory;
51 import javax.xml.stream.XMLStreamException;
52
53 import com.sun.xml.fastinfoset.QualifiedName;
54 import com.sun.xml.fastinfoset.sax.AttributesHolder;
55 import com.sun.xml.fastinfoset.stax.StAXDocumentSerializer;
56
57 /** <p>Writing a FI document directly with StAXDocumentSerializer.</p>
58  * This sample demonstrates the use of StAXDocumentSerializer to write out an FI document
59  * with following content:
60 <ns1:invoice xmlns:ns1="http://www.sun.com/schema/spidermarkexpress/sm-inv">
61     <Header>
62         <IssueDateTime>2003-03-13T13:13:32-08:00</IssueDateTime>
63         <Identifier schemeAgencyName="ISO" schemeName="Invoice">15570720</Identifier>
64         <POIdentifier schemeName="Generic" schemeAgencyName="ISO">691</POIdentifier>
65     </Header>
66 </ns1:invoice>
67  *
68  * You may use tool "fitosaxtoxml" provided in the FastInfoset package to verify the result.
69  */

70
71 public class FastInfosetSerializer {
72     
73     /** Creates a new instance of FastInfosetSerializer */
74     public FastInfosetSerializer() {
75     }
76     
77     public static void main(String JavaDoc[] args) {
78         if (args.length != 1) {
79             displayUsageAndExit();
80         }
81
82         try {
83             File JavaDoc output = new File JavaDoc(args[0]);
84             BufferedOutputStream JavaDoc fos = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(output));
85
86             //create an instance of FastInfoset StAX Serializer and set output stream
87
StAXDocumentSerializer s = new StAXDocumentSerializer();
88             s.setOutputStream(fos);
89
90             AttributesHolder attributes = new AttributesHolder();
91             attributes.clear();
92
93             String JavaDoc temp = null;
94
95             //start FastInfoset document
96
s.writeStartDocument();
97
98             //<ns1:invoice xmlns:ns1="http://www.sun.com/schema/spidermarkexpress/sm-inv">
99
String JavaDoc namespaceURI = "http://www.sun.com/schema/spidermarkexpress/sm-inv";
100             String JavaDoc prefix = "ns1";
101             String JavaDoc localPart = "invoice";
102             
103             //namespace must be indexed before calling startElement
104

105             s.writeStartElement(prefix, localPart, namespaceURI);
106             s.writeNamespace(prefix, namespaceURI);
107             s.setPrefix(prefix, namespaceURI);
108
109             // <Header>
110
s.writeCharacters("\n\t");
111             s.writeStartElement("header");
112
113             // <IssueDateTime>2003-03-13T13:13:32-08:00</IssueDateTime>
114
s.writeCharacters("\n\t\t");
115             s.writeStartElement("IssueDateTime");
116             s.writeCharacters("2003-03-13T13:13:32-08:00");
117             s.writeEndElement();
118
119             // <Identifier schemeAgencyName="ISO" schemeName="Invoice">15570720</Identifier>
120
s.writeCharacters("\n\t\t");
121             s.writeStartElement("Identifier");
122             s.writeAttribute("schemeAgencyName", "ISO");
123             s.writeAttribute("schemeName", "Invoice");
124             s.writeCharacters("15570720");
125             s.writeEndElement();
126
127             // <POIdentifier schemeName="Generic" schemeAgencyName="ISO">691</POIdentifier>
128
s.writeCharacters("\n\t\t");
129             s.writeStartElement("POIdentifier");
130             s.writeAttribute("schemeName", "Generic");
131             s.writeAttribute("schemeAgencyName", "ISO");
132             s.writeCharacters("691");
133             s.writeEndElement();
134             
135             // </Header>
136
s.writeCharacters("\n\t");
137             s.writeEndElement();
138
139             //</ns1:invoice>
140
s.writeCharacters("\n");
141             s.writeEndElement();
142
143             s.writeEndDocument();
144         } catch (Exception JavaDoc e) {
145             e.printStackTrace();
146         }
147     }
148     
149     private static void displayUsageAndExit() {
150         System.err.println("Usage: ant FIStAXSerializer or samples.stax.FastInfosetSerializer FI_output_file");
151         System.exit(1);
152     }
153 }
154
Popular Tags