1 19 package org.netbeans.mdr.util; 20 21 import java.util.*; 22 import java.io.*; 23 import javax.jmi.reflect.*; 24 25 30 public class XmlUtils extends Object { 31 32 static byte buf[] = new byte[4096]; 33 34 public static String readTagStart (InputStream is) throws IOException { 35 while (is.read() != '<'); 36 int c, i=0; 37 while ( (c=is.read()) != '>') { 38 buf[i++]=(byte)c; } 40 String result = new String (buf, 0, i); 41 return result; 42 } 43 44 public static String readValue (InputStream is) throws IOException { 45 int c, i=0; 46 while ( (c=is.read()) != '<') { 47 buf[i++]=(byte)c; } 49 return decode(new String (buf, 0, i)); 50 } 51 52 public static String readComplexValue (InputStream is) throws IOException { 53 String 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; } 60 try { 61 if (start.equals(new String (buf, left+2, i-left-2))) break; 62 } catch (StringIndexOutOfBoundsException e) { 63 Logger.getDefault().annotate(e, new Integer (left + 2).toString()); 64 Logger.getDefault().annotate(e, new Integer (i - left - 2).toString()); 65 Logger.getDefault().annotate(e, new String (buf, 0, i)); 66 throw e; 67 } 68 buf[i++]=(byte)'>'; 69 } 70 71 String value = new String (buf, 0, left); 72 return value; 74 } 75 76 public static void skipTagEnd (InputStream is) throws IOException { 77 while ( is.read() != '>'); } 79 80 public static String readTextTag (InputStream is) throws IOException { 81 readTagStart(is); 82 String value = readValue(is); 83 skipTagEnd(is); 84 return value; 85 } 86 87 public static String encode(String str) { 88 StringBuffer sb = new StringBuffer (str.length()); 89 String 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("&"); 95 } else if (c.equals("<")) { 96 sb.append("<"); 97 } else if (c.equals(">")) { 98 sb.append(">"); 99 } else if (c.equals("\"")) { 100 sb.append("""); 101 } else if (c.equals("'")) { 102 sb.append("'"); 103 } else { 104 sb.append(c); 105 } 106 } 107 108 112 return sb.toString(); 113 } 114 115 public static String decode(String str) { 116 StringBuffer sb = new StringBuffer (str.length()); 117 String 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 152 return sb.toString(); 153 } 154 155 public static String encodeString(String text) { 156 return "<textValue>" + encode(text) + "</textValue>"; 157 } 158 159 public static String decodeString(InputStream inputStream) throws IOException { 160 return readTextTag(inputStream); } 162 163 public static String encodeList(List list) { 164 StringBuffer buf = new StringBuffer (100); 165 buf.append("<list>"); 166 for (Iterator it = list.iterator(); it.hasNext();) { 167 buf.append(encodeString((String ) 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); for (;;) { 176 String str = readTagStart(inputStream); if (str.equals("/list")) break; list.add(readValue(inputStream)); 179 skipTagEnd(inputStream); } 181 } 182 } 183 | Popular Tags |