KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > metamodel_xml > io > Export


1 /*
2  * Created on 8 oct. 2003
3  *
4  * To change the template for this generated file go to
5  * Window - Preferences - Java - Code Generation - Code and Comments
6  */

7 package org.objectweb.modfact.metamodel_xml.io;
8
9 import java.io.OutputStream JavaDoc;
10 import java.io.OutputStreamWriter JavaDoc;
11 import java.io.PrintWriter JavaDoc;
12 import java.io.UnsupportedEncodingException JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import javax.jmi.reflect.*;
17
18 /**
19  * @author Xavier
20  *
21  * To change the template for this generated type comment go to
22  * Window - Preferences - Java - Code Generation - Code and Comments
23  */

24 public class Export {
25
26     /** Print writer. */
27     protected PrintWriter JavaDoc fOut;
28
29     /** Canonical output. */
30     protected boolean fCanonical;
31
32     //
33
// Constructors
34
//
35

36     /** Default constructor. */
37     public Export() {
38     } // <init>()
39

40     //
41
// Public methods
42
//
43

44     /** Sets whether output is canonical. */
45     public void setCanonical(boolean canonical) {
46         fCanonical = canonical;
47     } // setCanonical(boolean)
48

49     /** Sets the output stream for printing. */
50     public void setOutput(OutputStream JavaDoc stream, String JavaDoc encoding)
51         throws UnsupportedEncodingException JavaDoc {
52
53         if (encoding == null) {
54             encoding = "UTF8";
55         }
56
57         java.io.Writer JavaDoc writer = new OutputStreamWriter JavaDoc(stream, encoding);
58         fOut = new PrintWriter JavaDoc(writer);
59
60     } // setOutput(OutputStream,String)
61

62     /** Sets the output writer. */
63     public void setOutput(java.io.Writer JavaDoc writer) {
64
65         fOut =
66             writer instanceof PrintWriter JavaDoc
67                 ? (PrintWriter JavaDoc) writer
68                 : new PrintWriter JavaDoc(writer);
69
70     } // setOutput(java.io.Writer)
71

72     /** Writes the specified node, recursively. */
73     public void write(RefObject node) {
74
75         // is there anything to do?
76
if (node == null) {
77             return;
78         }
79
80         String JavaDoc type = (String JavaDoc) node.refGetValue("nodeType");
81
82         if (type.compareTo("DOCUMENT_NODE") == 0) {
83             fOut.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
84             fOut.flush();
85             write((RefObject) node.refGetValue("doctype"));
86             write((RefObject) node.refGetValue("rootNode"));
87         }
88
89         if (type.compareTo("DOCUMENT_TYPE_NODE") == 0) {
90             fOut.print("<!DOCTYPE ");
91             fOut.print(node.refGetValue("name"));
92             String JavaDoc publicId = (String JavaDoc) node.refGetValue("publicId");
93             String JavaDoc systemId = (String JavaDoc) node.refGetValue("systemId");
94             if (publicId != null) {
95                 fOut.print(" PUBLIC '");
96                 fOut.print(publicId);
97                 fOut.print("' '");
98                 fOut.print(systemId);
99                 fOut.print('\'');
100             } else {
101                 fOut.print(" SYSTEM '");
102                 fOut.print(systemId);
103                 fOut.print('\'');
104             }
105             String JavaDoc internalSubset = (String JavaDoc) node.refGetValue("internalSubset");
106             if (internalSubset != null) {
107                 fOut.println(" [");
108                 fOut.print(internalSubset);
109                 fOut.print(']');
110             }
111             fOut.println('>');
112         }
113
114         if (type.compareTo("ELEMENT_NODE") == 0) {
115             fOut.print('<');
116             fOut.print((String JavaDoc) node.refGetValue("tagName"));
117             RefObject namedNodeMap = (RefObject) node.refGetValue("attributes");
118             if (namedNodeMap != null) {
119                 Collection JavaDoc attrs =
120                     (Collection JavaDoc) namedNodeMap.refGetValue("items");
121                 Iterator JavaDoc attrsIt = attrs.iterator();
122                 while (attrsIt.hasNext()) {
123                     RefObject attr = (RefObject) attrsIt.next();
124                     fOut.print(' ');
125                     fOut.print(attr.refGetValue("name"));
126                     fOut.print("=\"");
127                     normalizeAndPrint((String JavaDoc) attr.refGetValue("value"));
128                     fOut.print('"');
129                 }
130             }
131             fOut.print('>');
132             fOut.flush();
133
134             RefObject nodeList = (RefObject) node.refGetValue("childNodes");
135             if (nodeList != null) {
136                 Collection JavaDoc children =
137                     (Collection JavaDoc) nodeList.refGetValue("items");
138                 Iterator JavaDoc it = children.iterator();
139                 while (it.hasNext()) {
140                     write((RefObject) it.next());
141                 }
142             }
143         }
144
145         if (type.compareTo("ENTITY_REFERENCE_NODE") == 0) {
146             //if (fCanonical) {
147
// Node child = node.getFirstChild();
148
// while (child != null) {
149
// write(child);
150
// child = child.getNextSibling();
151
// }
152
//}
153
//else {
154
// fOut.print('&');
155
// fOut.print(node.getNodeName());
156
// fOut.print(';');
157
// fOut.flush();
158
//}
159
}
160
161         if (type.compareTo("CDATA_SECTION_NODE") == 0) {
162             if (fCanonical) {
163                 normalizeAndPrint((String JavaDoc) node.refGetValue("value"));
164             } else {
165                 fOut.print("<![CDATA[");
166                 fOut.print(node.refGetValue("value"));
167                 fOut.print("]]>");
168             }
169             fOut.flush();
170         }
171
172         if (type.compareTo("TEXT_NODE") == 0) {
173             normalizeAndPrint((String JavaDoc) node.refGetValue("data"));
174             fOut.flush();
175         }
176
177         if (type.compareTo("PROCESSING_INSTRUCTION_NODE") == 0) {
178             fOut.print("<?");
179             fOut.print(node.refGetValue("name"));
180             String JavaDoc data = (String JavaDoc) node.refGetValue("data");
181             if (data != null && data.length() > 0) {
182                 fOut.print(' ');
183                 fOut.print(data);
184             }
185             fOut.println("?>");
186             fOut.flush();
187         }
188
189         if (type.compareTo("PROCESSING_INSTRUCTION_NODE") == 0) {
190             fOut.print("<?");
191             fOut.print(node.refGetValue("name"));
192             String JavaDoc data = (String JavaDoc) node.refGetValue("data");
193             if (data != null && data.length() > 0) {
194                 fOut.print(' ');
195                 fOut.print(data);
196             }
197             fOut.println("?>");
198             fOut.flush();
199         }
200
201         if (type.compareTo("COMMENT_NODE") == 0) {
202             fOut.print("<!-- ");
203             fOut.print(node.refGetValue("data"));
204             fOut.print(" -->");
205             fOut.flush();
206         }
207
208         if (type.compareTo("ELEMENT_NODE") == 0) {
209             fOut.print("</");
210             fOut.print(node.refGetValue("tagName"));
211             fOut.print('>');
212             fOut.flush();
213         }
214
215     } // write(Node)
216

217     //
218
// Protected methods
219
//
220

221     /** Normalizes and prints the given string. */
222     protected void normalizeAndPrint(String JavaDoc s) {
223
224         int len = (s != null) ? s.length() : 0;
225         for (int i = 0; i < len; i++) {
226             char c = s.charAt(i);
227             normalizeAndPrint(c);
228         }
229
230     } // normalizeAndPrint(String)
231

232     /** Normalizes and print the given character. */
233     protected void normalizeAndPrint(char c) {
234
235         switch (c) {
236             case '<' :
237                 {
238                     fOut.print("&lt;");
239                     break;
240                 }
241             case '>' :
242                 {
243                     fOut.print("&gt;");
244                     break;
245                 }
246             case '&' :
247                 {
248                     fOut.print("&amp;");
249                     break;
250                 }
251             case '"' :
252                 {
253                     fOut.print("&quot;");
254                     break;
255                 }
256             case '\r' :
257             case '\n' :
258                 {
259                     if (fCanonical) {
260                         fOut.print("&#");
261                         fOut.print(Integer.toString(c));
262                         fOut.print(';');
263                         break;
264                     }
265                     // else, default print char
266
}
267             default :
268                 {
269                     fOut.print(c);
270                 }
271         }
272
273     } // normalizeAndPrint(char)
274

275     
276     
277     
278     public void write(RefPackage p) {
279         Iterator JavaDoc it = getOuterMostNodes(p).iterator();
280         while(it.hasNext()){
281           write((RefObject) it.next());
282         }
283     }
284     
285     private Collection JavaDoc getOuterMostNodes(RefPackage p) {
286         Iterator JavaDoc it = p.refClass("Node").refAllOfType().iterator();
287         Collection JavaDoc res = new java.util.Vector JavaDoc();
288         while(it.hasNext()) {
289             RefObject o = (RefObject)it.next();
290             while(true) {
291               RefObject parent = (RefObject)o.refGetValue("parent");
292               if(parent==null) break;
293               else o = parent;
294             }
295             if(!res.contains(o)) res.add(o);
296         }
297         return res;
298     }
299     
300     
301     
302     
303     
304     
305     
306     
307     
308     
309     
310     
311     
312     
313     
314
315 } // class Writer
316
Popular Tags