KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > sax > 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.sax;
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.sax.SAXDocumentSerializer;
56
57 /** <p>Writing a FI document directly with SAXDocumentSerializer.</p>
58  * This sample demonstrates the use of SAXDocumentSerializer 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 SAX Serializer and set output stream
87
SAXDocumentSerializer s = new SAXDocumentSerializer();
88             s.setOutputStream(fos);
89
90             AttributesHolder attributes = new AttributesHolder();
91             String JavaDoc temp = null;
92
93             //start FastInfoset document
94
s.startDocument();
95
96             //<ns1:invoice xmlns:ns1="http://www.sun.com/schema/spidermarkexpress/sm-inv">
97
String JavaDoc namespaceURI = "http://www.sun.com/schema/spidermarkexpress/sm-inv";
98             String JavaDoc prefix = "ns1";
99             String JavaDoc localPart = "invoice";
100             
101             //namespace must be indexed before calling startElement
102
s.startPrefixMapping(prefix, namespaceURI);
103             s.startElement(namespaceURI, localPart, "ns1:invoice", attributes);
104
105             // <Header>
106
temp = "\n\t";
107             s.characters(temp.toCharArray(), 0, temp.length());
108             s.startElement("", "header", "header", attributes);
109
110             // <IssueDateTime>2003-03-13T13:13:32-08:00</IssueDateTime>
111
temp = "\n\t\t";
112             s.characters(temp.toCharArray(), 0, temp.length());
113             s.startElement("", "IssueDateTime", "IssueDateTime", attributes);
114             temp = "2003-03-13T13:13:32-08:00";
115             s.characters(temp.toCharArray(), 0, temp.length());
116             s.endElement("", "IssueDateTime", "IssueDateTime");
117
118             // <Identifier schemeAgencyName="ISO" schemeName="Invoice">15570720</Identifier>
119
temp = "\n\t\t";
120             s.characters(temp.toCharArray(), 0, temp.length());
121             attributes.clear();
122             attributes.addAttribute(new QualifiedName("", "", "schemeAgencyName", "schemeAgencyName"), "ISO");
123             attributes.addAttribute(new QualifiedName("", "", "schemeName", "schemeName"), "Invoice");
124             s.startElement("", "Identifier", "Identifier", attributes);
125             temp = "15570720";
126             s.characters(temp.toCharArray(), 0, temp.length());
127             s.endElement("", "Identifier", "Identifier");
128
129             // <POIdentifier schemeName="Generic" schemeAgencyName="ISO">691</POIdentifier>
130
temp = "\n\t\t";
131             s.characters(temp.toCharArray(), 0, temp.length());
132             attributes.clear();
133             attributes.addAttribute(new QualifiedName("", "", "schemeName", "schemeName"), "Generic");
134             attributes.addAttribute(new QualifiedName("", "", "schemeAgencyName", "schemeAgencyName"), "ISO");
135             s.startElement("", "POIdentifier", "POIdentifier", attributes);
136             temp = "691";
137             s.characters(temp.toCharArray(), 0, temp.length());
138             s.endElement("", "POIdentifier", "POIdentifier");
139             
140             // </Header>
141
temp = "\n\t";
142             s.characters(temp.toCharArray(), 0, temp.length());
143             s.endElement("", "header", "header");
144
145             //</ns1:invoice>
146
temp = "\n";
147             s.characters(temp.toCharArray(), 0, temp.length());
148             s.endElement("", "invoice", "ns1:invoice");
149
150             s.endDocument();
151         } catch (Exception JavaDoc e) {
152             e.printStackTrace();
153         }
154     }
155     
156     private static void displayUsageAndExit() {
157         System.err.println("Usage: ant FISAXSerializer or samples.sax.FastInfosetSerializer FI_output_file");
158         System.exit(1);
159     }
160 }
161
Popular Tags