1 7 package org.objectweb.modfact.metamodel_xml.io; 8 9 import java.io.OutputStream ; 10 import java.io.OutputStreamWriter ; 11 import java.io.PrintWriter ; 12 import java.io.UnsupportedEncodingException ; 13 import java.util.Collection ; 14 import java.util.Iterator ; 15 16 import javax.jmi.reflect.*; 17 18 24 public class Export { 25 26 27 protected PrintWriter fOut; 28 29 30 protected boolean fCanonical; 31 32 36 37 public Export() { 38 } 40 44 45 public void setCanonical(boolean canonical) { 46 fCanonical = canonical; 47 } 49 50 public void setOutput(OutputStream stream, String encoding) 51 throws UnsupportedEncodingException { 52 53 if (encoding == null) { 54 encoding = "UTF8"; 55 } 56 57 java.io.Writer writer = new OutputStreamWriter (stream, encoding); 58 fOut = new PrintWriter (writer); 59 60 } 62 63 public void setOutput(java.io.Writer writer) { 64 65 fOut = 66 writer instanceof PrintWriter 67 ? (PrintWriter ) writer 68 : new PrintWriter (writer); 69 70 } 72 73 public void write(RefObject node) { 74 75 if (node == null) { 77 return; 78 } 79 80 String type = (String ) 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 publicId = (String ) node.refGetValue("publicId"); 93 String systemId = (String ) 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 internalSubset = (String ) 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 ) node.refGetValue("tagName")); 117 RefObject namedNodeMap = (RefObject) node.refGetValue("attributes"); 118 if (namedNodeMap != null) { 119 Collection attrs = 120 (Collection ) namedNodeMap.refGetValue("items"); 121 Iterator 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 ) 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 children = 137 (Collection ) nodeList.refGetValue("items"); 138 Iterator 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 } 160 161 if (type.compareTo("CDATA_SECTION_NODE") == 0) { 162 if (fCanonical) { 163 normalizeAndPrint((String ) 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 ) 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 data = (String ) 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 data = (String ) 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 } 217 221 222 protected void normalizeAndPrint(String 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 } 232 233 protected void normalizeAndPrint(char c) { 234 235 switch (c) { 236 case '<' : 237 { 238 fOut.print("<"); 239 break; 240 } 241 case '>' : 242 { 243 fOut.print(">"); 244 break; 245 } 246 case '&' : 247 { 248 fOut.print("&"); 249 break; 250 } 251 case '"' : 252 { 253 fOut.print("""); 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 } 267 default : 268 { 269 fOut.print(c); 270 } 271 } 272 273 } 275 276 277 278 public void write(RefPackage p) { 279 Iterator it = getOuterMostNodes(p).iterator(); 280 while(it.hasNext()){ 281 write((RefObject) it.next()); 282 } 283 } 284 285 private Collection getOuterMostNodes(RefPackage p) { 286 Iterator it = p.refClass("Node").refAllOfType().iterator(); 287 Collection res = new java.util.Vector (); 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 } | Popular Tags |