1 7 8 package org.jdesktop.jdnc.markup; 9 10 import java.beans.Expression ; 11 12 import java.io.File ; 13 import java.io.IOException ; 14 15 import java.net.MalformedURLException ; 16 import java.net.Socket ; 17 import java.net.UnknownHostException ; 18 import java.net.URL ; 19 import java.net.URLConnection ; 20 21 import java.util.logging.Level ; 22 23 import net.openmarkup.Error; 24 import net.openmarkup.Glitch; 25 import net.openmarkup.Warning; 26 import net.openmarkup.ApplierError; 27 import net.openmarkup.ApplierException; 28 import net.openmarkup.ApplierWarning; 29 import net.openmarkup.ObjectRealizer; 30 import net.openmarkup.Realizable; 31 import net.openmarkup.Scribe; 32 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.Document ; 35 36 43 public class RealizationUtils { 44 45 49 public static Object getReferencedObject(Realizable target, String attributeValue) { 50 Document doc = target.getOwnerDocument(); 51 if (doc instanceof net.openmarkup.Document) { 52 return ( (net.openmarkup.Document) doc).getElement(attributeValue). 53 getObject(); 54 } 55 56 try { 57 ObjectRealizer or = target.getObjectRealizer(); 59 Expression exp = new Expression (or, "getElementById", 60 new Object [] {target, 61 attributeValue}); 62 Element element = (Element ) exp.getValue(); 63 Realizable realizable = RealizationUtils.getRealizable(element); 64 if (realizable == null) { 65 return null; 66 } 67 return realizable.getObject(); 68 } 69 catch (Exception ex) { 70 throw new RuntimeException ("Error processing " + attributeValue, ex); 71 } 72 } 73 74 public static Element getReferencedElement(Realizable target, 75 String attributeValue) { 76 Document doc = target.getOwnerDocument(); 77 if (doc instanceof net.openmarkup.Document) { 78 return ( (net.openmarkup.Document) doc).getElement(attributeValue); 79 } 80 try { 81 ObjectRealizer or = target.getObjectRealizer(); 83 Expression exp = new Expression (or, "getElementById", 84 new Object [] {target, 85 attributeValue}); 86 return (Element ) exp.getValue(); 87 88 } 89 catch (Exception ex) { 90 throw new RuntimeException ("Error processing " + attributeValue, ex); 91 } 92 } 93 94 99 public static boolean validateURL(String url) throws ApplierException { 100 return validateURL(url, true); 101 } 102 103 113 public static boolean validateURL(String url, boolean severe) throws ApplierException { 114 try { 115 return validateURL(new URL (url), severe); 116 } catch (MalformedURLException ex) { 117 String message = "Malformed URL: " + url; 118 if (severe) { 119 throw new ApplierError(message, ex); 120 } else { 121 throw new ApplierWarning(message, ex); 122 } 123 } 124 } 125 126 132 public static boolean validateURL(URL url) throws ApplierException { 133 return validateURL(url, true); 134 } 135 136 146 public static boolean validateURL(URL url, boolean severe) throws ApplierException { 147 if (url != null) { 148 String protocol = url.getProtocol(); 149 if ("file".equals(protocol)) { 150 return validateFileURL(url, severe); 151 } else if ("http".equals(protocol) || "https".equals(protocol)) { 152 return validateHttpURL(url, severe); 153 } 154 } 155 return false; 156 } 157 158 private static boolean validateFileURL(URL url, boolean severe) throws ApplierException { 159 try { 160 File file = new File (url.getFile()); 161 return file.canRead(); 162 } catch (Exception ex) { 163 String message = "Cannot read file: " + url.toExternalForm(); 164 if (severe) { 165 throw new ApplierError(message, ex); 166 } else { 167 throw new ApplierWarning(message, ex); 168 } 169 } 170 } 171 172 private static boolean validateHttpURL(URL url, boolean severe) throws ApplierException { 173 Socket connection = null; 174 try { 175 int port = url.getPort(); 176 if (port == -1) { 177 port = url.getDefaultPort(); 178 } 179 connection = new Socket (url.getHost(), port); 180 } catch (UnknownHostException ex0) { 181 String message = "Cannot find host for: " + url.toExternalForm(); 182 if (severe) { 183 throw new ApplierError(message, ex0); 184 } else { 185 throw new ApplierWarning(message, ex0); 186 } 187 } catch (IOException ex) { 188 String message = "Cannot open connection to: " + url.toExternalForm(); 189 if (severe) { 190 throw new ApplierError(message, ex); 191 } else { 192 throw new ApplierWarning(message, ex); 193 } 194 } 195 finally { 196 if (connection != null) { 197 try { 198 connection.close(); 199 } catch (IOException ex) { 200 throw new ApplierException("Error closing Socket for URL: " + 201 url.toExternalForm(), ex); 202 } 203 } 204 } 205 return true; 206 } 207 208 public static Realizable getRealizable(Element element) { 209 213 Object userObject = null; 214 215 218 try { 220 Class clz = Class.forName("org.apache.xerces.dom.NodeImpl"); 221 if (element.getClass() == clz) { 222 userObject = (new Expression (element, "getUserData", new Object [0])).getValue(); 223 } 224 } catch (ClassNotFoundException ex) { 225 } catch (Exception ex) { 227 throw new RuntimeException ("Error invoking getUserData for " + element.toString(), ex); 229 } 230 231 if (!(userObject instanceof Realizable)) { 232 ObjectRealizer or = getObjectRealizer(); 234 235 try { 236 userObject = (Realizable)(new Expression (or, "getRealizable", 237 new Object [] { element })).getValue(); 238 } catch (Exception ex) { 239 String message = "Error getting Realizable from " + element.toString(); 240 throw new RuntimeException (message, ex); 241 } 242 } 243 return (Realizable) (userObject == null ? element : userObject); 244 } 245 246 259 public static void logException(String message, Exception ex) { 260 Level level = Level.INFO; 261 262 if (ex instanceof Error ) { 263 level = Level.SEVERE; 264 } 265 else if (ex instanceof Glitch) { 266 level = Level.INFO; 267 } 268 else if (ex instanceof Warning) { 269 level = Level.WARNING; 270 } 271 else { 272 level = Level.SEVERE; 274 } 275 Scribe.getLogger().log(level, message, ex); 276 277 if (ex instanceof Error ) { 278 throw new RuntimeException (ex); 280 } 281 } 282 283 288 public static ObjectRealizer getObjectRealizer() { 289 String realizerImplName = null; 290 try { 291 realizerImplName = System.getProperty("openmarkup.realizer.impl"); 293 if (realizerImplName == null) { 294 realizerImplName = "org.jdesktop.openmarkup.ri.ObjectRealizerImpl"; 296 } 297 } catch (SecurityException ex) { 298 realizerImplName = "org.jdesktop.openmarkup.ri.ObjectRealizerImpl"; 301 } 302 303 ObjectRealizer realizer = null; 304 try { 305 Class clz = Class.forName(realizerImplName); 306 Object or = clz.newInstance(); 307 308 try { 311 realizer = (ObjectRealizer)(new Expression (or, "getInstance", 312 new Object [0])).getValue(); 313 } catch (Exception ex) { 314 } 316 317 if (realizer == null) { 318 realizer = (ObjectRealizer)or; 319 } 320 } catch (Exception ex) { 321 throw new RuntimeException ("Cannot find ObjectRealizer: " + realizerImplName, ex); 322 } 323 return realizer; 324 } 325 326 334 public static URL getResolvedURL(Realizable realizable, String uri) { 335 URL url = null; 336 337 Document doc = realizable.getOwnerDocument(); 339 Element docElement = doc.getDocumentElement(); 340 String base = docElement.getAttributeNS("http://www.w3.org/XML/1998/namespace", "base"); 341 if (base != null && !base.equals("")) { 342 try { 343 url = new URL (new URL (base), uri); 344 } catch (MalformedURLException ex) { 345 } 347 } 348 if (url == null) { 349 try { 350 url = new URL (getObjectRealizer().getDefaultBaseURL(), uri); 351 } catch (MalformedURLException ex) { 353 url = realizable.getClass().getResource(uri); 354 } 355 } 357 return url; 358 359 } 360 } 361 | Popular Tags |