KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > util > XmlUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.util;
20
21 import java.util.*;
22 import java.io.*;
23 import javax.jmi.reflect.*;
24
25 /**
26  *
27  * @author Pavel Buzek
28  * @version
29  */

30 public class XmlUtils extends Object JavaDoc {
31
32     static byte buf[] = new byte[4096];
33
34     public static String JavaDoc readTagStart (InputStream is) throws IOException {
35         while (is.read() != '<');
36         int c, i=0;
37         while ( (c=is.read()) != '>') {
38             buf[i++]=(byte)c; // must be ascii
39
}
40         String JavaDoc result = new String JavaDoc(buf, 0, i);
41         return result;
42     }
43
44     public static String JavaDoc readValue (InputStream is) throws IOException {
45         int c, i=0;
46         while ( (c=is.read()) != '<') {
47             buf[i++]=(byte)c; // must be ascii
48
}
49         return decode(new String JavaDoc(buf, 0, i));
50     }
51
52     public static String JavaDoc readComplexValue (InputStream is) throws IOException {
53         String JavaDoc start = readTagStart(is);
54         int c, i=0, left=0;
55         for (;;) {
56             while ( ((c=is.read()) != '>') ) {
57                 if (c=='<') left = i;
58                 buf[i++]=(byte)c; // must be ascii
59
}
60             try {
61             if (start.equals(new String JavaDoc(buf, left+2, i-left-2))) break;
62             } catch (StringIndexOutOfBoundsException JavaDoc e) {
63                 Logger.getDefault().annotate(e, new Integer JavaDoc(left + 2).toString());
64                 Logger.getDefault().annotate(e, new Integer JavaDoc(i - left - 2).toString());
65                 Logger.getDefault().annotate(e, new String JavaDoc(buf, 0, i));
66                 throw e;
67             }
68             buf[i++]=(byte)'>';
69         }
70             
71         String JavaDoc value = new String JavaDoc(buf, 0, left);
72 // Logger.getDefault().log("result:"+value);
73
return value;
74     }
75     
76     public static void skipTagEnd (InputStream is) throws IOException {
77         while ( is.read() != '>'); // skip the </..end..>
78
}
79
80     public static String JavaDoc readTextTag (InputStream is) throws IOException {
81         readTagStart(is);
82         String JavaDoc value = readValue(is);
83         skipTagEnd(is);
84         return value;
85     }
86
87     public static String JavaDoc encode(String JavaDoc str) {
88         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(str.length());
89         String JavaDoc c;
90
91         for (int i = 0; i < str.length(); i++) {
92             c = str.substring(i, i + 1);
93             if (c.equals("&")) {
94                 sb.append("&amp;");
95             } else if (c.equals("<")) {
96                 sb.append("&lt;");
97             } else if (c.equals(">")) {
98                 sb.append("&gt;");
99             } else if (c.equals("\"")) {
100                 sb.append("&quot;");
101             } else if (c.equals("'")) {
102                 sb.append("&apos;");
103             } else {
104                 sb.append(c);
105             }
106         }
107
108 /* if (!sb.toString().equals(str)) {
109             Logger.getDefault().log("coded: " + str + " -> " + sb);
110         }
111 */

112         return sb.toString();
113     }
114
115     public static String JavaDoc decode(String JavaDoc str) {
116         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(str.length());
117         String JavaDoc c;
118         int i = 0;
119         int j = -1;
120
121         while ((i = str.indexOf("&", j)) >= 0) {
122             sb.append(str.substring(j + 1, i));
123             j = str.indexOf(";", i + 1);
124             if (j > i + 1) {
125                 c = str.substring(i + 1, j);
126                 if (c.equals("amp")) {
127                     sb.append("&");
128                 } else if (c.equals("lt")) {
129                     sb.append("<");
130                 } else if (c.equals("gt")) {
131                     sb.append(">");
132                 } else if (c.equals("quot")) {
133                     sb.append("\"");
134                 } else if (c.equals("apos")) {
135                     sb.append("'");
136                 } else {
137                     Logger.getDefault().log(Logger.WARNING, "Error! Cannot substitute character: " + c);
138                     return "";
139                 }
140             } else {
141                 Logger.getDefault().log(Logger.WARNING, "Error! End of substitution not found.");
142                 return "";
143             }
144         }
145
146         sb.append(str.substring(j + 1));
147
148 /* if (!sb.toString().equals(str)) {
149             Logger.getDefault().log("decoded: " + str + " -> " + sb);
150         }
151 */

152         return sb.toString();
153     }
154
155     public static String JavaDoc encodeString(String JavaDoc text) {
156         return "<textValue>" + encode(text) + "</textValue>";
157     }
158     
159     public static String JavaDoc decodeString(InputStream inputStream) throws IOException {
160         return readTextTag(inputStream); //value
161
}
162     
163     public static String JavaDoc encodeList(List list) {
164         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(100);
165         buf.append("<list>");
166         for (Iterator it = list.iterator(); it.hasNext();) {
167             buf.append(encodeString((String JavaDoc) it.next()));
168         }
169         buf.append("</list>");
170         return buf.toString();
171     }
172     
173     public static void decodeList(InputStream inputStream, List list) throws IOException {
174         readTagStart(inputStream); //<list>
175
for (;;) {
176             String JavaDoc str = readTagStart(inputStream); //<value>
177
if (str.equals("/list")) break; // </list>
178
list.add(readValue(inputStream));
179             skipTagEnd(inputStream); // </value>
180
}
181     }
182 }
183
Popular Tags