1 5 6 package com.hp.hpl.jena.rdf.arp; 7 8 import com.hp.hpl.jena.rdf.model.*; 9 import com.hp.hpl.jena.rdf.model.impl.*; 10 import com.hp.hpl.jena.graph.*; 11 import com.hp.hpl.jena.shared.*; 12 import com.hp.hpl.jena.datatypes.*; 13 14 import java.io.*; 15 import java.net.*; 16 17 import org.xml.sax.InputSource ; 18 import org.xml.sax.SAXNotSupportedException ; 19 import org.xml.sax.SAXException ; 20 import org.xml.sax.SAXNotRecognizedException ; 21 22 27 public class JenaReader implements RDFReader, ARPErrorNumbers { 28 29 static private final int BULK_UPDATE_SIZE = 1000; 30 31 38 static public void useMe(Model m) { 39 m.setReaderClassName("RDF/XML", JenaReader.class.getName()); 40 m.setReaderClassName("RDF/XML-ABBREV", JenaReader.class.getName()); 41 } 42 43 static private final String saxFeaturesURL = "http://xml.org/sax/features/"; 44 45 static private final String saxPropertiesURL = "http://xml.org/sax/properties/"; 46 47 static private final String apacheFeaturesURL = "http://apache.org/xml/features/"; 48 49 static private final String apachePropertiesURL = "http://apache.org/xml/properties/"; 50 51 static final String arpPropertiesURL = "http://jena.hpl.hp.com/arp/properties/"; 52 53 static final int arpPropertiesURLLength = arpPropertiesURL.length(); 54 55 58 public JenaReader() { 59 arpf = SingleThreadedParser.create(); 60 } 61 62 final private SingleThreadedParser arpf; 63 64 private Model model; 65 66 public void read(Model model, String url) throws JenaException { 67 try { 68 URLConnection conn = new URL(url).openConnection(); 69 String encoding = conn.getContentEncoding(); 70 if (encoding == null) 71 read(model, conn.getInputStream(), url); 72 else 73 read(model, new InputStreamReader(conn.getInputStream(), 74 encoding), url); 75 } catch (FileNotFoundException e) { 76 throw new DoesNotExistException(url); 77 } catch (IOException e) { 78 throw new JenaException(e); 79 } 80 } 81 82 90 static public Literal translate(ALiteral lit) { 91 return new LiteralImpl(lit.toString(), lit.getLang(), lit 92 .isWellFormedXML(), null); 93 } 94 95 static Node convert(ALiteral lit) { 96 String dtURI = lit.getDatatypeURI(); 97 if (dtURI == null) 98 return Node.createLiteral(lit.toString(), lit.getLang(), false); 99 else { 100 if (lit.isWellFormedXML()) { 101 return Node.createLiteral(lit.toString(), null, true); 102 } else { 103 RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName( 104 dtURI); 105 106 return Node.createLiteral(lit.toString(), null, dt); 107 } 108 } 109 } 110 111 static Node convert(AResource r) { 112 if (r.isAnonymous()) { 113 String id = r.getAnonymousID(); 114 Node rr = (Node) r.getUserData(); 115 if (rr == null) { 116 rr = Node.createAnon(); 117 r.setUserData(rr); 118 } 119 return rr; 120 } else { 121 return Node.createURI(r.getURI()); 122 } 123 } 124 125 static Triple convert(AResource s, AResource p, AResource o) { 126 return Triple.create(convert(s), convert(p), convert(o)); 127 } 128 129 static Triple convert(AResource s, AResource p, ALiteral o) { 130 return Triple.create(convert(s), convert(p), convert(o)); 131 } 132 133 143 static public Property translatePred(AResource r) throws JenaException { 144 return new PropertyImpl(r.getURI()); 145 } 146 147 158 private void read(Model m, InputSource inputS, String xmlBase) 159 throws JenaException { 160 model = m; 161 if (xmlBase != null && !xmlBase.equals("")) { 162 try { 163 URI uri = new URI(xmlBase); 164 } catch (MalformedURIException e) { 165 errorHandler.error(e); 166 } 167 } 168 174 read(model.getGraph(), inputS, xmlBase, model); 175 } 176 177 private JenaHandler handler; 178 179 synchronized private void read(final Graph g, InputSource inputS, 180 String xmlBase, Model m) { 181 182 try { 183 g.getEventManager().notifyEvent(g, GraphEvents.startRead); 184 inputS.setSystemId(xmlBase); 185 handler = new JenaHandler(g, m, errorHandler); 186 handler.useWith(arpf.getHandlers()); 187 arpf.parse(inputS, xmlBase); 188 handler.bulkUpdate(); 189 } catch (IOException e) { 190 throw new WrappedIOException(e); 191 } catch (SAXException e) { 192 throw new JenaException(e); 193 } finally { 194 g.getEventManager().notifyEvent(g, GraphEvents.finishRead); 195 handler = null; 196 } 197 } 198 199 210 public void read(final Model model, Reader reader, String xmlBase) 211 throws JenaException { 212 read(model, new InputSource (reader), xmlBase); 213 } 214 215 226 public void read(Graph g, Reader reader, String xmlBase) 227 throws JenaException { 228 read(g, new InputSource (reader), xmlBase, null); 229 } 230 231 242 public void read(final Model model, InputStream in, String xmlBase) 243 throws JenaException { 244 read(model, new InputSource (in), xmlBase); 245 } 246 247 258 public void read(Graph g, InputStream in, String xmlBase) { 259 read(g, new InputSource (in), xmlBase, null); 260 } 261 262 private RDFErrorHandler errorHandler = new RDFDefaultErrorHandler(); 263 264 275 public RDFErrorHandler setErrorHandler(RDFErrorHandler errHandler) { 276 RDFErrorHandler old = this.errorHandler; 277 this.errorHandler = errHandler; 278 JenaHandler h = handler; 279 if (h != null) { 280 h.setErrorHandler(errHandler); 281 } 282 return old; 283 } 284 285 378 public Object setProperty(String str, Object value) throws JenaException { 379 Object obj = value; 380 if (str.startsWith("http:")) { 381 if (str.startsWith(arpPropertiesURL)) { 382 return setArpProperty(str.substring(arpPropertiesURLLength), 383 obj); 384 } 385 if (str.startsWith(saxPropertiesURL) 386 || str.startsWith(apachePropertiesURL)) { 387 Object old; 388 try { 389 old = arpf.getSAXParser().getProperty(str); 390 } catch (SAXNotSupportedException ns) { 391 old = null; 392 } catch (SAXNotRecognizedException nr) { 393 errorHandler.error(new UnknownPropertyException(str)); 394 return null; 395 } 396 try { 397 arpf.getSAXParser().setProperty(str, obj); 398 } catch (SAXNotSupportedException ns) { 399 errorHandler.error(new JenaException(ns)); 400 } catch (SAXNotRecognizedException nr) { 401 errorHandler.error(new UnknownPropertyException(str)); 402 return null; 403 } 404 return old; 405 } 406 407 if (str.startsWith(saxFeaturesURL) 408 || str.startsWith(apacheFeaturesURL)) { 409 Boolean old; 410 try { 411 old = new Boolean (arpf.getSAXParser().getFeature(str)); 412 } catch (SAXNotSupportedException ns) { 413 old = null; 414 } catch (SAXNotRecognizedException nr) { 415 errorHandler.error(new UnknownPropertyException(str)); 416 return null; 417 } 418 try { 419 arpf.getSAXParser().setFeature(str, 420 ((Boolean ) obj).booleanValue()); 421 } catch (SAXNotSupportedException ns) { 422 errorHandler.error(new JenaException(ns)); 423 } catch (SAXNotRecognizedException nr) { 424 errorHandler.error(new UnknownPropertyException(str)); 425 return null; 426 } catch (ClassCastException cc) { 427 errorHandler.error(new JenaException( 428 new SAXNotSupportedException ("Feature: '" + str 429 + "' can only have a boolean value."))); 430 } 431 return old; 432 } 433 } 434 return setArpProperty(str, obj); 435 } 436 437 static public int errorCode(String upper) { 438 Class c = ARPErrorNumbers.class; 439 try { 440 java.lang.reflect.Field fld = c.getField(upper); 441 return fld.getInt(null); 442 } catch (Exception e) { 443 return -1; 444 } 445 } 446 447 static public String errorCodeName(int errNo) { 448 Class c = ARPErrorNumbers.class; 449 java.lang.reflect.Field flds[] = c.getDeclaredFields(); 450 for (int i = 0; i < flds.length; i++) { 451 try { 452 if (flds[i].getInt(null) == errNo) 453 return flds[i].getName(); 454 } catch (Exception e) { 455 } 456 } 457 return null; 458 } 459 460 private Object setArpProperty( String str, Object v) { 461 return setArpProperty(arpf.getOptions(),str,v,errorHandler); 462 } 463 469 static Object setArpProperty(ARPOptions options, String str, Object v, 470 RDFErrorHandler eh ) { 471 str = str.toUpperCase(); 473 if (v == null) 474 v = ""; 475 if (v instanceof String ) { 476 v = ((String ) v).toUpperCase(); 477 } 478 if (str.equals("ERROR-MODE")) { 479 if (v instanceof String ) { 480 String val = (String ) v; 481 if (val.equals("LAX")) { 482 options.setLaxErrorMode(); 483 return null; 484 } 485 if (val.equals("DEFAULT")) { 486 options.setDefaultErrorMode(); 487 return null; 488 } 489 if (val.equals("STRICT")) { 490 options.setStrictErrorMode(); 491 return null; 492 } 493 if (val.equals("STRICT-WARNING")) { 494 options.setStrictErrorMode(EM_WARNING); 495 return null; 496 } 497 if (val.equals("STRICT-FATAL")) { 498 options.setStrictErrorMode(EM_FATAL); 499 return null; 500 } 501 if (val.equals("STRICT-IGNORE")) { 502 options.setStrictErrorMode(EM_IGNORE); 503 return null; 504 } 505 if (val.equals("STRICT-ERROR")) { 506 options.setStrictErrorMode(EM_ERROR); 507 return null; 508 } 509 } 510 eh 511 .error(new IllegalArgumentException ( 512 "Property \"ERROR-MODE\" takes the following values: " 513 + "\"default\", \"lax\", \"strict\", \"strict-ignore\", \"strict-warning\", \"strict-error\", \"strict-fatal\".")); 514 return null; 515 } 516 if (str.equals("EMBEDDING")) { 517 if (v instanceof String ) { 518 v = Boolean.valueOf((String ) v); 519 } 520 if (!(v instanceof Boolean )) { 521 eh.error(new IllegalArgumentException ( 523 "Property \"EMBEDDING\" requires a boolean value.")); 524 boolean old = options.setEmbedding(false); 525 options.setEmbedding(old); 526 return new Boolean (old); 527 } else { 528 return new Boolean (options.setEmbedding(((Boolean ) v) 529 .booleanValue())); 530 } 531 } 532 if (str.startsWith("ERR_") || str.startsWith("IGN_") 533 || str.startsWith("WARN_")) { 534 int cond = errorCode(str); 535 if (cond == -1) { 536 } else { 538 if (v instanceof String ) { 539 if (!((String ) v).startsWith("EM_")) { 540 } else { 542 int val = errorCode((String ) v); 543 if (val == -1) { 544 } else { 546 int rslt = options.setErrorMode(cond, val); 547 return new Integer (rslt); 548 } 549 } 550 } else if (v instanceof Integer ) { 551 int val = ((Integer ) v).intValue(); 552 switch (val) { 553 case EM_IGNORE: 554 case EM_WARNING: 555 case EM_ERROR: 556 case EM_FATAL: 557 int rslt = options.setErrorMode(cond, val); 558 return new Integer (rslt); 559 default: 560 } 562 } 563 eh.error(new IllegalArgumentException ("Property \"" 565 + str + "\" cannot have value: " + v.toString())); 566 int old = options.setErrorMode(cond, EM_ERROR); 567 options.setErrorMode(cond, old); 568 return new Integer (old); 569 } 570 } 571 eh.error(new UnknownPropertyException(str)); 572 return null; 573 } 574 575 582 static public Model memModel() { 583 Model rslt = ModelFactory.createDefaultModel(); 584 useMe(rslt); 585 return rslt; 586 } 587 588 } 589 590 | Popular Tags |