KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > querying > impl > xtas > util


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 JavaDoc;
7 import org.w3c.dom.Document JavaDoc;
8 import org.w3c.dom.NodeList JavaDoc;
9 import org.w3c.dom.Attr JavaDoc;
10 import org.w3c.dom.NamedNodeMap JavaDoc;
11
12 /** Some utilites for testing
13  * @version $Id: util.java 566 2005-01-25 12:50:49Z kravchuk $*/

14
15 public class util
16 {
17    public static final String JavaDoc XML_DECLARATION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
18
19
20     public static String JavaDoc getFileContent(String JavaDoc fileName) throws IOException
21     {
22
23         BufferedReader in = new BufferedReader(new FileReader(fileName));
24         String JavaDoc 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 JavaDoc node) throws UnsupportedEncodingException{
34
35        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out, "UTF8"));
36
37 // String lineRowColumn = (String ) ((NodeImpl) node).getUserData("startLine");
38

39        int type = node.getNodeType();
40        switch ( type ) {
41           // print document
42

43           case Node.DOCUMENT_NODE: {
44  // out.println( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
45
print( ((Document JavaDoc)node).getDocumentElement());
46                 out.flush();
47                 break;
48              }
49
50              // print element with attributes
51
case Node.ELEMENT_NODE: {
52                 out.print( /*lineRowColumn + ":" + */'<');
53                 out.print(node.getNodeName());
54                 Attr JavaDoc attrs[] = sortAttributes(node.getAttributes());
55                 for ( int i = 0; i < attrs.length; i++ ) {
56                    Attr JavaDoc 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 JavaDoc 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              // handle entity reference nodes
80
case Node.ENTITY_REFERENCE_NODE: {
81                 out.print('&');
82                 out.print(node.getNodeName());
83                 out.print(';');
84                 break;
85              }
86
87              // print cdata sections
88
case Node.CDATA_SECTION_NODE: {
89                 out.print("<![CDATA[");
90                 out.print(node.getNodeValue());
91                 out.print("]]>");
92                 break;
93              }
94
95              // print text
96
case Node.TEXT_NODE: {
97                 out.print( node.getNodeValue());
98                 break;
99              }
100
101              // print processing instruction
102
case Node.PROCESSING_INSTRUCTION_NODE: {
103                 out.print("<?");
104                 out.print(node.getNodeName());
105                 String JavaDoc 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  /*
115        if ( type == Node.ELEMENT_NODE ) {
116           out.print("</");
117           out.print(node.getNodeName());
118           out.print('>');
119        }
120  */

121        out.flush();
122
123     }
124
125     /** Returns a sorted list of attributes. */
126     private static Attr JavaDoc[] sortAttributes(NamedNodeMap JavaDoc attrs) {
127
128        int len = (attrs != null) ? attrs.getLength() : 0;
129        Attr JavaDoc array[] = new Attr JavaDoc[len];
130        for ( int i = 0; i < len; i++ ) {
131           array[i] = (Attr JavaDoc)attrs.item(i);
132        }
133        for ( int i = 0; i < len - 1; i++ ) {
134           String JavaDoc name = array[i].getNodeName();
135           int index = i;
136           for ( int j = i + 1; j < len; j++ ) {
137              String JavaDoc curName = array[j].getNodeName();
138              if ( curName.compareTo(name) < 0 ) {
139                 name = curName;
140                 index = j;
141              }
142           }
143           if ( index != i ) {
144              Attr JavaDoc 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 JavaDoc packXmlString(String JavaDoc str)
155     {
156
157        StringTokenizer parser = new StringTokenizer(str,"<");
158        String JavaDoc out = "";
159        try {
160           while(parser.hasMoreTokens()) {
161
162              // Get ELEMENT with attributes...
163
String JavaDoc out1 = "<"+parser.nextToken();
164              StringTokenizer parser1 = new StringTokenizer(out1,">");
165              String JavaDoc out2 = parser1.nextToken()+">";
166
167              // Get nested TEXT_ELEMENT if any...
168
String JavaDoc out3 = "";
169              int textInd = out1.indexOf(">");
170              if (textInd++ < out1.length())
171                  out3 = new String JavaDoc( out1.substring( textInd ) ).trim();
172
173               // Concatenate all...
174
out+=(out2+out3);
175
176           }
177 // System.out.println(out);
178
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