1 4 5 9 10 package org.openlaszlo.xml.internal; 11 import org.openlaszlo.utils.ChainedException; 12 import org.jdom.Document; 13 import org.jdom.Element; 14 import org.xml.sax.SAXException ; 15 import java.io.*; 16 import java.util.*; 17 18 23 abstract public class XMLUtils { 24 33 public static String getAttributeValue(Element e, String aname, 34 String defaultValue) 35 { 36 String value = e.getAttributeValue(aname); 37 if (value == null) { 38 value = defaultValue; 39 } 40 return value; 41 } 42 43 51 public static String requireAttributeValue(Element e, String aname) { 52 String value = e.getAttributeValue(aname); 53 if (value == null) { 54 throw new MissingAttributeException(e, aname); 55 } 56 return value; 57 } 58 59 66 public static String escapeXml(String s) { 67 if (s == null) return null; 68 StringBuffer sb = new StringBuffer (); 69 for(int i=0; i<s.length(); i++) { 70 char c = s.charAt(i); 71 if (c == '<') { 72 sb.append("<"); 73 } else if (c == '>') { 74 sb.append(">"); 75 } else if (c == '\'') { 76 sb.append("'"); 77 } else if (c == '&') { 78 sb.append("&"); 79 } else if (c == '"') { 80 sb.append("""); 81 } else { 82 sb.append(c); 83 } 84 } 85 return sb.toString(); 86 } 87 88 98 public static String escapeXmlForSWFHTML(String s) { 99 if (s == null) return null; 100 StringBuffer sb = new StringBuffer (); 101 for(int i=0; i<s.length(); i++) { 102 char c = s.charAt(i); 103 if (c == '<') { 104 sb.append("<"); 105 } else if (c == '>') { 106 sb.append(">"); 107 } else if (c == '&') { 108 sb.append("&"); 109 } else { 110 sb.append(c); 111 } 112 } 113 return sb.toString(); 114 } 115 116 125 public static String escapeAmpersands(String s) { 126 if (s == null) return null; 127 StringBuffer sb = new StringBuffer (); 128 for(int i=0; i<s.length(); i++) { 129 char c = s.charAt(i); 130 if (c == '&') { 131 int j = i+1; 132 if (s.regionMatches(j, "lt;", 0, 3)) { 133 sb.append("&lt;"); 134 i += 3; 135 } else if (s.regionMatches(j, "gt;", 0, 3)) { 136 sb.append("&gt;"); 137 i += 3; 138 } else if (s.regionMatches(j, "apos;", 0, 5)) { 139 sb.append("&apos;"); 140 i += 5; 141 } else if (s.regionMatches(j, "quot;", 0, 5)) { 142 sb.append("&quot;"); 143 i += 5; 144 } else if (s.regionMatches(j, "amp;", 0, 4)) { 145 sb.append("&amp;"); 146 i += 4; 147 } else { 148 sb.append(c); 149 } 150 } else { 151 sb.append(c); 152 } 153 } 154 155 return sb.toString(); 156 } 157 158 public static boolean isURL(String str) { 159 if (str.startsWith("http:") || 162 str.startsWith("https:") || 163 str.startsWith("file:") || 164 str.startsWith("ftp:") || 165 str.startsWith("soap:")) { 166 return true; 167 } 168 try { 169 new java.net.URL (str); return true; 171 } catch (java.net.MalformedURLException e) { 172 return false; 173 } 174 } 175 176 public static Element parse(String source) { 177 try { 178 org.jdom.input.SAXHandler handler = new org.jdom.input.SAXHandler(); 179 org.xml.sax.XMLReader reader = 180 org.xml.sax.helpers.XMLReaderFactory.createXMLReader( 181 "org.apache.xerces.parsers.SAXParser"); 182 reader.setContentHandler(handler); 183 reader.parse(new org.xml.sax.InputSource (new StringReader(source))); 184 Document doc = handler.getDocument(); 185 return doc.getRootElement(); 186 } catch (IOException e) { 187 throw new ChainedException(e); 188 } catch (SAXException e) { 189 throw new ChainedException(e); 190 } 191 } 192 193 199 public static String getNoDeclarationXML(StringBuffer xml) 200 { 201 int len = xml.length(); 202 for (int i=0; i < len; i++) 203 if ( xml.charAt(i) == '<' && ( i+1 < len) ) 204 if (xml.charAt(i+1) != '?' && xml.charAt(i+1) != '!') 205 return xml.substring(i); 206 return xml.toString(); 207 } 208 209 210 public static String toString(Element element) { 211 org.jdom.output.XMLOutputter outputter = 212 new org.jdom.output.XMLOutputter(); 213 outputter.setTextNormalize(true); 214 return outputter.outputString(element); 215 } 216 217 220 private static StringBuffer getXPathToInternal(Element elt) { 221 Element parent = elt.getParent(); 222 if (parent == null) { 223 return new StringBuffer ("/*[1]"); 224 } 225 int i = parent.getChildren().indexOf(elt); 226 if (i == -1) { 227 throw new RuntimeException ("Element " + elt + " not in parent " + parent + " .getChildren()?"); 228 } 229 StringBuffer sb = getXPathToInternal(parent); 230 sb.append("/*["); 231 sb.append(i + 1); 233 sb.append("]"); 234 return sb; 235 } 236 237 243 public static String getXPathTo(Element elt) { 244 return getXPathToInternal(elt).toString(); 245 } 246 } 247 | Popular Tags |