View Javadoc

1   /*   Copyright 2004 BEA Systems, Inc.
2    *
3    *   Licensed under the Apache License, Version 2.0 (the "License");
4    *   you may not use this file except in compliance with the License.
5    *   You may obtain a copy of the License at
6    *
7    *       http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *   Unless required by applicable law or agreed to in writing, software
10   *   distributed under the License is distributed on an "AS IS" BASIS,
11   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *   See the License for the specific language governing permissions and
13   *   limitations under the License.
14   */
15  package com.bea.xml.stream;
16  
17  import java.io.Writer;
18  import java.io.IOException;
19  import javax.xml.stream.XMLStreamWriter;
20  import javax.xml.stream.XMLStreamReader;
21  import javax.xml.stream.XMLStreamException;
22  import javax.xml.stream.events.XMLEvent;
23  import javax.xml.stream.XMLInputFactory;
24  import javax.xml.stream.XMLOutputFactory;
25  
26  /***
27   * <p> Writes XML in a non-xml format to create XML tests. </p>
28   */
29  
30  public class XMLStreamRecorder extends XMLWriterBase {
31  
32    public XMLStreamRecorder(){}
33    public XMLStreamRecorder(Writer writer) {
34      super(writer);
35    }
36  
37    protected String writeName(String prefix,String namespaceURI, String localName) 
38      throws XMLStreamException
39    {
40      if (!"".equals(namespaceURI))
41        write("['"+namespaceURI+"':");
42      else
43        write("[");
44      prefix = super.writeName(prefix,namespaceURI,localName);
45      write(']');
46      return prefix;
47    }
48  
49    protected void writeType(int type) 
50      throws XMLStreamException
51    {
52      closeStartElement();
53      write('[');
54      write(com.bea.xml.stream.util.ElementTypeNames.getEventTypeString(type));
55      write(']');
56  
57    }
58    
59    protected void openStartTag() 
60      throws XMLStreamException
61    {
62      write('[');
63    }
64  
65    protected void closeStartTag() throws XMLStreamException{
66      write("];\n");
67    }
68  
69    protected void openEndTag() 
70      throws XMLStreamException
71    {
72      write('[');
73    }
74    
75    protected void closeEndTag() 
76      throws XMLStreamException
77    {
78      write(']');
79    }
80  
81    public void writeAttribute(String namespaceURI,
82                               String localName,
83                               String value) 
84      throws XMLStreamException
85    {
86      write("[[ATTRIBUTE]");
87      writeName("",namespaceURI,localName);
88      write("=");
89      writeCharactersInternal(value.toCharArray(),0,value.length(),true);
90      write("]");
91    }
92    public void writeNamespace(String prefix, String namespaceURI) 
93      throws XMLStreamException 
94    {
95  
96      if(!isOpen())
97       throw new XMLStreamException("A start element must be written before a namespace");
98      if (prefix == null || "".equals(prefix) || "xmlns".equals(prefix)) {
99        writeDefaultNamespace(namespaceURI);
100       return;
101     }
102     write("[[NAMESPACE][");
103     write("xmlns:");
104     write(prefix);
105     write("]=[");
106     write(namespaceURI);
107     write("]");
108     setPrefix(prefix,namespaceURI);
109     write(']');
110   }
111 
112   public void writeDefaultNamespace(String namespaceURI)
113     throws XMLStreamException 
114   {
115     write("[[DEFAULT][");
116     if(!isOpen())
117      throw new XMLStreamException("A start element must be written before the default namespace");
118     write("xmlns]");
119     write("=[");
120     write(namespaceURI);
121     write("]");
122     setPrefix(DEFAULTNS,namespaceURI);
123     write(']');
124   }
125 
126   public void writeComment(String data) 
127       throws XMLStreamException
128   {
129     closeStartElement();
130     write("[");
131     if (data != null)
132       write(data);
133     write("]");
134   }
135 
136   public void writeProcessingInstruction(String target,
137                                          String text) 
138     throws XMLStreamException
139   {
140     closeStartElement();
141     write("[");
142     if (target != null)
143       write("["+target+"]");
144     if (text != null) {
145       write(",["+text+"]");
146     }
147     write("]");
148   }
149 
150   public void writeDTD(String dtd) 
151     throws XMLStreamException
152   {
153     write("[");
154     super.write(dtd);
155     write("]");
156   }
157 
158   public void writeCData(String data) 
159     throws XMLStreamException
160   {
161     write("[");
162     if (data != null)
163       write(data);
164     write("]");
165   }
166 
167   public void writeEntityRef(String name) 
168     throws XMLStreamException
169   {
170     write("[");
171     super.writeEntityRef(name);
172     write("]");
173   }
174 
175   public void writeStartDocument() 
176     throws XMLStreamException
177   {
178     write("[[1.0],[utf-8]]");
179   }
180 
181   public void writeStartDocument(String version) 
182     throws XMLStreamException
183   {
184     write("[[");
185     write(version);
186     write("],[utf-8]]");
187   }
188 
189   public void writeStartDocument(String encoding,
190                                  String version) 
191     throws XMLStreamException
192   {
193     write("[[");
194     write(version);
195     write("],[");
196     write(encoding);
197     write("]]");
198   }
199   protected void writeCharactersInternal(char characters[],
200                                          int start,
201                                          int length,
202                                          boolean isAttributeValue) 
203     throws XMLStreamException
204   {
205     if(length == 0) write("[]");
206     else { 
207       write("[");
208       write(characters,start,length);
209       write("]");
210     }
211   }
212   
213   public void write(XMLStreamReader xmlr) 
214     throws XMLStreamException
215   {
216     writeType(xmlr.getEventType());
217     super.write(xmlr);
218     if (!isOpen()) write(";\n");
219   }
220 
221   public static void main(String args[]) throws Exception {
222     XMLInputFactory xmlif = XMLInputFactory.newInstance();
223     XMLOutputFactory xmlof = XMLOutputFactory.newInstance();
224     XMLStreamReader xmlr = xmlif.createXMLStreamReader(new java.io.FileReader(args[0]));
225 
226     XMLStreamRecorder r = new XMLStreamRecorder(new java.io.OutputStreamWriter(new java.io.FileOutputStream("out.stream")));
227 
228     while (xmlr.hasNext()) {
229       r.write(xmlr);
230       xmlr.next();
231     }
232     r.write(xmlr);
233     r.flush();
234   }
235 
236   
237 }
238 
239 
240 
241 
242 
243