1 19 20 package com.hp.hpl.jena.ontology; 23 24 25 import java.io.*; 28 import java.util.*; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.xerces.util.XMLChar; 33 34 import com.hp.hpl.jena.rdf.model.*; 35 import com.hp.hpl.jena.util.FileUtils; 36 import com.hp.hpl.jena.vocabulary.RDF; 37 import com.hp.hpl.jena.shared.*; 38 import com.hp.hpl.jena.shared.impl.PrefixMappingImpl; 39 40 41 52 public class OntDocumentManager 53 { 54 55 public static final String DEFAULT_METADATA_PATH = "file:ont-policy.rdf;file:etc/ont-policy.rdf"; 56 57 58 public static final String PATH_DELIMITER = ";"; 59 60 61 public static final String NS = "http://jena.hpl.hp.com/schemas/2003/03/ont-manager#"; 62 63 64 public static final String ANCHOR = "#"; 65 66 67 70 71 private static final Model vocab = ModelFactory.createDefaultModel(); 72 73 74 public static final Resource ONTOLOGY_SPEC = vocab.createResource( NS + "OntologySpec" ); 75 76 77 public static final Property PUBLIC_URI = vocab.createProperty( NS + "publicURI" ); 78 79 80 public static final Property ALT_URL = vocab.createProperty( NS + "altURL" ); 81 82 83 public static final Property PREFIX = vocab.createProperty( NS + "prefix" ); 84 85 86 public static final Property LANGUAGE = vocab.createProperty( NS + "language" ); 87 88 89 public static final Resource DOC_MGR_POLICY = vocab.createResource( NS + "DocumentManagerPolicy" ); 90 91 92 public static final Property CACHE_MODELS = vocab.createProperty( NS, "cacheModels" ); 93 94 95 public static final Property PROCESS_IMPORTS = vocab.createProperty( NS, "processImports" ); 96 97 98 public static final Property IGNORE_IMPORT = vocab.createProperty( NS, "ignoreImport" ); 99 100 101 public static final Property USE_DECLARED_NS_PREFIXES = vocab.createProperty( NS, "useDeclaredNsPrefixes" ); 102 103 104 private static OntDocumentManager s_instance = null; 105 106 107 110 111 protected String m_searchPath = DEFAULT_METADATA_PATH; 112 113 114 protected Map m_altMap = new HashMap(); 115 116 117 protected Map m_modelMap = new HashMap(); 118 119 120 protected Map m_languageMap = new HashMap(); 121 122 123 private Log m_log = LogFactory.getLog( getClass() ); 124 125 126 protected boolean m_cacheModels = true; 127 128 129 protected boolean m_processImports = true; 130 131 132 protected Set m_ignoreImports = new HashSet(); 133 134 135 protected PrefixMapping m_prefixMap = new PrefixMappingImpl(); 136 137 138 protected boolean m_useDeclaredPrefixes = true; 139 140 141 protected String m_policyURL = null; 142 143 144 147 153 public OntDocumentManager() { 154 this( DEFAULT_METADATA_PATH ); 155 } 156 157 158 170 public OntDocumentManager( String path ) { 171 m_searchPath = (path == null) ? "" : path; 172 initialiseMetadata( m_searchPath, false ); 173 } 174 175 176 182 public OntDocumentManager( Model config ) { 183 configure( config, false ); 185 } 186 187 188 191 199 public static OntDocumentManager getInstance() { 200 if (s_instance == null) { 201 s_instance = new OntDocumentManager(); 202 } 203 return s_instance; 204 } 205 206 207 216 public String getMetadataSearchPath() { 217 return m_searchPath; 218 } 219 220 221 232 public void setMetadataSearchPath( String path, boolean replace ) { 233 m_searchPath = path; 234 m_policyURL = null; 235 initialiseMetadata( path, replace ); 236 } 237 238 239 245 public void configure( Model config ) { 246 configure( config, true ); 247 } 248 249 250 259 public void configure( Model config, boolean reset ) { 260 if (reset) { 261 reset(); 262 } 263 264 loadMetadata( config ); 265 } 266 267 268 275 public void reset( boolean reload ) { 276 m_altMap.clear(); 277 m_modelMap.clear(); 278 m_languageMap.clear(); 279 m_cacheModels = true; 280 m_processImports = true; 281 m_ignoreImports.clear(); 282 m_prefixMap = new PrefixMappingImpl(); 283 m_useDeclaredPrefixes = true; 284 285 if (reload) { 286 setMetadataSearchPath( DEFAULT_METADATA_PATH, true ); 288 } 289 else { 290 m_searchPath = DEFAULT_METADATA_PATH; 291 } 292 } 293 294 301 public void reset() { 302 reset( false ); 303 } 304 305 306 314 public Iterator listDocuments() { 315 return m_altMap.keySet().iterator(); 316 } 317 318 319 328 public String doAltURLMapping( String uri ) { 329 String alt = (String ) m_altMap.get( uri ); 330 return (alt == null) ? uri : alt; 331 } 332 333 334 342 public String getLanguage( String uri ) { 343 return (String ) m_languageMap.get( uri ); 344 } 345 346 347 356 public String getPrefixForURI( String uri ) { 357 return m_prefixMap.getNsURIPrefix( uri ); 358 } 359 360 361 369 public String getURIForPrefix( String prefix ) { 370 return m_prefixMap.getNsPrefixURI( prefix ); 371 } 372 373 374 383 public Model getModel( String uri ) { 384 return (Model) m_modelMap.get( uri ); 385 } 386 387 388 395 public boolean useDeclaredPrefixes() { 396 return m_useDeclaredPrefixes; 397 } 398 399 406 public void setUseDeclaredPrefixes( boolean useDeclaredPrefixes ) { 407 m_useDeclaredPrefixes = useDeclaredPrefixes; 408 } 409 410 416 public PrefixMapping getDeclaredPrefixMapping() { 417 return m_prefixMap; 418 } 419 420 421 430 public void addPrefixMapping( String uri, String prefix ) { 431 m_prefixMap.setNsPrefix( prefix, uri ); 432 } 433 434 435 445 public void addAltEntry( String docURI, String locationURL ) { 446 m_altMap.put( docURI, locationURL ); 447 } 448 449 450 462 public void addModel( String docURI, Model model ) { 463 addModel( docURI, model, false ); 464 } 465 466 467 477 public void addModel( String docURI, Model model, boolean replace ) { 478 if (m_cacheModels && 479 (!m_modelMap.containsKey( docURI ) || replace)) 480 { 481 m_modelMap.put( docURI, model ); 482 } 483 } 484 485 486 495 public void addLanguageEntry( String docURI, String language ) { 496 m_languageMap.put( docURI, language ); 497 } 498 499 500 508 public void forget( String docURI ) { 509 m_altMap.remove( docURI ); 510 m_modelMap.remove( docURI ); 511 m_languageMap.remove( docURI ); 512 } 513 514 515 529 public OntModel getOntology( String uri, OntModelSpec spec ) { 530 OntModelSpec _spec = spec; 532 if (_spec.getDocumentManager() != this) { 533 _spec = new OntModelSpec( spec ); 534 _spec.setDocumentManager( this ); 535 } 536 537 if (m_modelMap.containsKey( uri )) { 539 Model cached = (Model) m_modelMap.get( uri ); 540 if (cached instanceof OntModel) { 541 return (OntModel) cached; 542 } 543 else { 544 return ModelFactory.createOntologyModel( _spec, cached ); 545 } 546 } 547 548 OntModel m = ModelFactory.createOntologyModel( _spec, null ); 549 read( m, uri, true ); 550 551 return m; 552 } 553 554 555 563 public boolean getProcessImports() { 564 return m_processImports; 565 } 566 567 568 577 public boolean getCacheModels() { 578 return m_cacheModels; 579 } 580 581 582 590 public void setProcessImports( boolean processImports ) { 591 m_processImports = processImports; 592 } 593 594 595 603 public void setCacheModels( boolean cacheModels ) { 604 m_cacheModels = cacheModels; 605 } 606 607 611 public void addIgnoreImport( String uri ) { 612 m_ignoreImports.add( uri ); 613 } 614 615 619 public void removeIgnoreImport( String uri ) { 620 m_ignoreImports.remove( uri ); 621 } 622 623 627 public Iterator listIgnoredImports() { 628 return m_ignoreImports.iterator(); 629 } 630 631 636 public boolean ignoringImport( String uri ) { 637 return m_ignoreImports.contains( uri ); 638 } 639 640 645 public void clearCache() { 646 m_modelMap.clear(); 647 } 648 649 650 662 public void loadImports( OntModel model ) { 663 if (m_processImports) { 664 List readQueue = new ArrayList(); 665 666 queueImports( model, readQueue, model.getProfile() ); 668 loadImports( model, readQueue ); 669 } 670 } 671 672 673 680 public void loadImport( OntModel model, String uri ) { 681 if (m_processImports) { 682 List readQueue = new ArrayList(); 683 readQueue.add( uri ); 684 loadImports( model, readQueue ); 685 } 686 } 687 688 689 695 public void unloadImport( OntModel model, String uri ) { 696 if (m_processImports) { 697 List unloadQueue = new ArrayList(); 698 unloadQueue.add( uri ); 699 unloadImports( model, unloadQueue ); 700 } 701 } 702 703 704 710 public String getLoadedPolicyURL() { 711 return m_policyURL; 712 } 713 714 715 718 723 protected void loadImports( OntModel model, List readQueue ) { 724 while (!readQueue.isEmpty()) { 725 String importURI = (String ) readQueue.remove( 0 ); 727 728 if (!model.hasLoadedImport( importURI ) && !ignoringImport( importURI )) { 729 loadImport( model, importURI, readQueue ); 731 } 732 } 733 } 734 735 736 741 protected void unloadImports( OntModel model, List unloadQueue ) { 742 while (!unloadQueue.isEmpty()) { 743 String importURI = (String ) unloadQueue.remove( 0 ); 745 746 if (model.hasLoadedImport( importURI )) { 747 749 Model importModel = getModel( importURI ); 751 if (importModel != null) { 752 List imports = new ArrayList(); 753 754 for (StmtIterator i = importModel.listStatements( null, model.getProfile().IMPORTS(), (RDFNode) null ); i.hasNext(); ) { 756 imports.add( i.nextStatement().getResource().getURI() ); 757 } 758 759 model.removeSubModel( importModel, false ); 761 model.removeLoadedImport( importURI ); 762 763 for (StmtIterator i = model.listStatements( null, model.getProfile().IMPORTS(), (RDFNode) null ); i.hasNext(); ) { 766 imports.remove( i.nextStatement().getResource().getURI() ); 767 } 768 769 unloadQueue.addAll( imports ); 771 } 772 } 773 } 774 775 model.rebind(); 776 } 777 778 779 782 protected void queueImports( Model model, List readQueue, Profile profile ) { 783 if (model instanceof OntModel) { 784 readQueue.addAll( ((OntModel) model).listImportedOntologyURIs() ); 786 } 787 else { 788 StmtIterator i = model.listStatements( null, profile.IMPORTS(), (RDFNode) null ); 790 791 while (i.hasNext()) { 792 Resource imp = i.nextStatement().getResource(); 794 795 readQueue.add( imp.getURI() ); 797 } 798 799 i.close(); 800 } 801 } 802 803 804 813 protected void initialiseMetadata( String path, boolean replace ) { 814 if (replace) { 816 m_altMap.clear(); 817 m_modelMap.clear(); 818 m_prefixMap = new PrefixMappingImpl(); 819 } 820 821 m_prefixMap.setNsPrefixes( PrefixMapping.Standard ); 823 824 Model metadata = findMetadata( path ); 826 827 if (metadata != null) { 828 loadMetadata( metadata ); 829 } 830 } 831 832 833 843 protected Model findMetadata( String path ) { 844 StringTokenizer pathElems = new StringTokenizer( path, PATH_DELIMITER ); 845 Model m = ModelFactory.createDefaultModel(); 846 boolean loaded = false; 847 848 while (!loaded && pathElems.hasMoreTokens()) { 850 String mdURI = pathElems.nextToken(); 851 loaded = read( m, mdURI, false ); 852 if (loaded) { 853 m_policyURL = mdURI; 854 } 855 } 856 857 return loaded ? m : null; 859 } 860 861 862 870 protected void loadMetadata( Model metadata ) { 871 for (ResIterator i = metadata.listSubjectsWithProperty( RDF.type, DOC_MGR_POLICY ); i.hasNext(); ) { 873 Resource policy = i.nextResource(); 874 875 for (StmtIterator j = policy.listProperties(); j.hasNext(); ) { 877 Statement s = j.nextStatement(); 878 Property pred = s.getPredicate(); 879 880 if (pred.equals( CACHE_MODELS )) { 881 setCacheModels( s.getBoolean() ); 882 } 883 else if (pred.equals( PROCESS_IMPORTS )) { 884 setProcessImports( s.getBoolean() ); 885 } 886 else if (pred.equals( IGNORE_IMPORT )) { 887 addIgnoreImport( s.getResource().getURI() ); 888 } 889 else if (pred.equals( USE_DECLARED_NS_PREFIXES )) { 890 setUseDeclaredPrefixes( s.getBoolean() ); 891 } 892 } 893 } 894 895 for (ResIterator i = metadata.listSubjectsWithProperty( RDF.type, ONTOLOGY_SPEC ); i.hasNext(); ) { 897 Resource root = i.nextResource(); 898 899 Statement s = root.getProperty( PUBLIC_URI ); 900 if (s != null) { 901 String publicURI = s.getResource().getURI(); 903 904 s = root.getProperty( ALT_URL ); 906 if (s != null) addAltEntry( publicURI, s.getResource().getURI() ); 907 908 s = root.getProperty( PREFIX ); 910 if (s != null) { 911 boolean endWithNCNameCh = XMLChar.isNCName( publicURI.charAt( publicURI.length() - 1 ) ); 913 String prefixExpansion = endWithNCNameCh ? (publicURI + ANCHOR) : publicURI; 914 915 addPrefixMapping( prefixExpansion, s.getString() ); 916 } 917 918 s = root.getProperty( LANGUAGE ); 920 if (s != null) addLanguageEntry( publicURI, s.getResource().getURI() ); 921 } 922 else { 923 m_log.warn( "Ontology specification node lists no public URI - node ignored"); 924 } 925 } 926 } 927 928 929 940 protected void loadImport( OntModel model, String importURI, List readQueue ) { 941 if (m_processImports) { 942 LogFactory.getLog( getClass() ).debug( "OntDocumentManager loading " + importURI ); 943 944 model.addLoadedImport( importURI ); 946 947 Model in = getModel( importURI ); 949 950 if (in == null) { 952 ModelMaker maker = model.getSpecification().getImportModelMaker(); 955 boolean loaded = maker.hasModel( importURI ); 956 957 in = maker.openModel( importURI ); 958 959 if (!loaded) { 961 read( in, importURI, true ); 962 } 963 } 964 965 if (in != model) { 967 queueImports( in, readQueue, model.getProfile() ); 969 970 model.addSubModel( in, false ); 972 973 addModel( importURI, in ); 975 } 976 } 977 } 978 979 980 990 protected boolean read( Model model, String uri, boolean warn ) { 991 String resolvableURI = doAltURLMapping( uri ); 993 String file = resolvableURI.startsWith( "file:" ) ? resolvableURI.substring( 5 ) : resolvableURI; 994 995 try { 997 String lang = FileUtils.guessLang( resolvableURI ); 999 1000 InputStream is = getClass().getClassLoader().getResourceAsStream( file ); 1002 1003 if (is == null) { 1004 model.read( resolvableURI, uri, lang ); 1008 } 1009 else { 1010 try { 1011 model.read( is, uri, lang ); 1013 } 1014 finally { 1015 try {is.close();} catch (IOException ignore) {} 1016 } 1017 } 1018 1019 addModel( uri, model ); 1021 return true; 1022 } 1023 catch (JenaException e) { 1024 if (warn) { 1025 String u = "<" + resolvableURI + ">"; 1026 if (!resolvableURI.equals( uri )) { 1027 u = u + " (re-directed via the document mgr from <" + uri + ">)"; 1028 } 1029 1030 LogFactory.getLog( OntDocumentManager.class ) 1031 .warn( "An error occurred while attempting to read from " + u + 1032 ". Error was '" + e.getMessage() + "'.", e ); 1033 } 1034 return false; 1035 } 1036 } 1037 1038 1039 1043 1044} 1045 1046 1047 1076 | Popular Tags |