1 package org.exoplatform.services.xml.querying.impl.xtas; 2 3 import java.io.*; 4 import java.util.*; 5 6 import org.w3c.dom.Node ; 7 import org.w3c.dom.Document ; 8 import org.w3c.dom.NodeList ; 9 import org.w3c.dom.Attr ; 10 import org.w3c.dom.NamedNodeMap ; 11 12 14 15 public class util 16 { 17 public static final String XML_DECLARATION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 18 19 20 public static String getFileContent(String fileName) throws IOException 21 { 22 23 BufferedReader in = new BufferedReader(new FileReader(fileName)); 24 String str, out=""; 25 while ((str = in.readLine()) != null) { 26 out+=str; 27 } 28 in.close(); 29 return out.trim(); 30 31 } 32 33 public static void print(Node node) throws UnsupportedEncodingException{ 34 35 PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out, "UTF8")); 36 37 39 int type = node.getNodeType(); 40 switch ( type ) { 41 43 case Node.DOCUMENT_NODE: { 44 print( ((Document )node).getDocumentElement()); 46 out.flush(); 47 break; 48 } 49 50 case Node.ELEMENT_NODE: { 52 out.print( '<'); 53 out.print(node.getNodeName()); 54 Attr attrs[] = sortAttributes(node.getAttributes()); 55 for ( int i = 0; i < attrs.length; i++ ) { 56 Attr attr = attrs[i]; 57 out.print(' '); 58 out.print(attr.getNodeName()); 59 out.print("=\""); 60 out.print(attr.getNodeValue()); 61 out.print('"'); 62 } 63 out.print('>'); 64 65 NodeList children = node.getChildNodes(); 66 if ( children != null ) { 67 68 int len = children.getLength(); 69 for ( int i = 0; i < len; i++ ) { 70 print(children.item(i)); 71 } 72 73 } 74 75 out.print("\n"); 76 break; 77 } 78 79 case Node.ENTITY_REFERENCE_NODE: { 81 out.print('&'); 82 out.print(node.getNodeName()); 83 out.print(';'); 84 break; 85 } 86 87 case Node.CDATA_SECTION_NODE: { 89 out.print("<![CDATA["); 90 out.print(node.getNodeValue()); 91 out.print("]]>"); 92 break; 93 } 94 95 case Node.TEXT_NODE: { 97 out.print( node.getNodeValue()); 98 break; 99 } 100 101 case Node.PROCESSING_INSTRUCTION_NODE: { 103 out.print("<?"); 104 out.print(node.getNodeName()); 105 String data = node.getNodeValue(); 106 if ( data != null && data.length() > 0 ) { 107 out.print(' '); 108 out.print(data); 109 } 110 out.print("?>"); 111 break; 112 } 113 } 114 121 out.flush(); 122 123 } 124 125 126 private static Attr [] sortAttributes(NamedNodeMap attrs) { 127 128 int len = (attrs != null) ? attrs.getLength() : 0; 129 Attr array[] = new Attr [len]; 130 for ( int i = 0; i < len; i++ ) { 131 array[i] = (Attr )attrs.item(i); 132 } 133 for ( int i = 0; i < len - 1; i++ ) { 134 String name = array[i].getNodeName(); 135 int index = i; 136 for ( int j = i + 1; j < len; j++ ) { 137 String curName = array[j].getNodeName(); 138 if ( curName.compareTo(name) < 0 ) { 139 name = curName; 140 index = j; 141 } 142 } 143 if ( index != i ) { 144 Attr temp = array[i]; 145 array[i] = array[index]; 146 array[index] = temp; 147 } 148 } 149 150 return (array); 151 152 } 153 154 public static String packXmlString(String str) 155 { 156 157 StringTokenizer parser = new StringTokenizer(str,"<"); 158 String out = ""; 159 try { 160 while(parser.hasMoreTokens()) { 161 162 String out1 = "<"+parser.nextToken(); 164 StringTokenizer parser1 = new StringTokenizer(out1,">"); 165 String out2 = parser1.nextToken()+">"; 166 167 String out3 = ""; 169 int textInd = out1.indexOf(">"); 170 if (textInd++ < out1.length()) 171 out3 = new String ( out1.substring( textInd ) ).trim(); 172 173 out+=(out2+out3); 175 176 } 177 return (out.trim()); 179 } catch (NoSuchElementException e) { 180 System.out.println("QueryResult.parseInput ERROR: "+e); 181 return null; 182 } 183 184 } 185 } 186 | Popular Tags |