KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > stax > StAXOutputFactory


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
40 package com.sun.xml.fastinfoset.stax;
41
42 import javax.xml.transform.Result JavaDoc;
43 import javax.xml.stream.XMLOutputFactory ;
44 import javax.xml.stream.XMLEventWriter;
45 import javax.xml.stream.XMLStreamWriter;
46 import javax.xml.stream.XMLStreamException;
47 import javax.xml.transform.stream.StreamResult JavaDoc;
48 import java.io.File JavaDoc;
49 import java.io.FileWriter JavaDoc;
50 import java.io.IOException JavaDoc;
51 import java.io.OutputStream JavaDoc;
52 import java.io.Writer JavaDoc;
53 import com.sun.xml.fastinfoset.CommonResourceBundle;
54
55 public class StAXOutputFactory extends XMLOutputFactory {
56         
57     //List of supported properties and default values.
58
private StAXManager _manager = null ;
59     
60     /** Creates a new instance of StAXOutputFactory */
61     public StAXOutputFactory() {
62         _manager = new StAXManager(StAXManager.CONTEXT_WRITER);
63     }
64     
65     public XMLEventWriter createXMLEventWriter(Result JavaDoc result) throws XMLStreamException {
66         return new StAXEventWriter(createXMLStreamWriter(result));
67     }
68     
69     public XMLEventWriter createXMLEventWriter(Writer writer) throws XMLStreamException {
70         return new StAXEventWriter(createXMLStreamWriter(writer));
71     }
72     
73     public XMLEventWriter createXMLEventWriter(OutputStream JavaDoc outputStream) throws XMLStreamException {
74         return new StAXEventWriter(createXMLStreamWriter(outputStream));
75     }
76     
77     public XMLEventWriter createXMLEventWriter(OutputStream JavaDoc outputStream, String JavaDoc encoding) throws XMLStreamException {
78         return new StAXEventWriter(createXMLStreamWriter(outputStream, encoding));
79     }
80     
81     public XMLStreamWriter createXMLStreamWriter(Result JavaDoc result) throws XMLStreamException {
82         if(result instanceof StreamResult JavaDoc){
83             StreamResult JavaDoc streamResult = (StreamResult JavaDoc)result;
84             if( streamResult.getWriter() != null){
85                 return createXMLStreamWriter(streamResult.getWriter());
86             }else if(streamResult.getOutputStream() != null ){
87                 return createXMLStreamWriter(streamResult.getOutputStream());
88             }else if(streamResult.getSystemId()!= null){
89                 try{
90                     FileWriter JavaDoc writer = new FileWriter JavaDoc(new File JavaDoc(streamResult.getSystemId()));
91                     return createXMLStreamWriter(writer);
92                 }catch(IOException JavaDoc ie){
93                     throw new XMLStreamException(ie);
94                 }
95             }
96         }
97         else if(result instanceof Result JavaDoc){
98             try{
99                 //xxx: should we be using FileOutputStream - nb.
100
FileWriter JavaDoc writer = new FileWriter JavaDoc(new File JavaDoc(result.getSystemId()));
101                 return createXMLStreamWriter(writer);
102             }catch(IOException JavaDoc ie){
103                 throw new XMLStreamException(ie);
104             }
105         }
106         throw new java.lang.UnsupportedOperationException JavaDoc();
107     }
108     
109     /** this is assumed that user wants to write the file in xml format
110      *
111      */

112     public XMLStreamWriter createXMLStreamWriter(Writer writer) throws XMLStreamException {
113         throw new java.lang.UnsupportedOperationException JavaDoc();
114     }
115     
116     public XMLStreamWriter createXMLStreamWriter(OutputStream JavaDoc outputStream) throws XMLStreamException {
117         return new StAXDocumentSerializer(outputStream, new StAXManager(_manager));
118     }
119     
120     public XMLStreamWriter createXMLStreamWriter(OutputStream JavaDoc outputStream, String JavaDoc encoding) throws XMLStreamException {
121         StAXDocumentSerializer serializer = new StAXDocumentSerializer(outputStream, new StAXManager(_manager));
122         serializer.setEncoding(encoding);
123         return serializer;
124     }
125     
126     public Object JavaDoc getProperty(String JavaDoc name) throws java.lang.IllegalArgumentException JavaDoc {
127         if(name == null){
128             throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.propertyNotSupported", new Object JavaDoc[]{name}));
129         }
130         if(_manager.containsProperty(name))
131             return _manager.getProperty(name);
132         throw new IllegalArgumentException JavaDoc(CommonResourceBundle.getInstance().getString("message.propertyNotSupported", new Object JavaDoc[]{name}));
133     }
134     
135     public boolean isPropertySupported(String JavaDoc name) {
136         if(name == null)
137             return false ;
138         else
139             return _manager.containsProperty(name);
140     }
141     
142     public void setProperty(String JavaDoc name, Object JavaDoc value) throws java.lang.IllegalArgumentException JavaDoc {
143         _manager.setProperty(name,value);
144         
145     }
146         
147 }
148
Popular Tags