1 19 20 package jena; 23 24 25 import java.util.*; 28 import java.io.*; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.text.SimpleDateFormat ; 32 33 import org.apache.commons.logging.LogFactory; 34 import org.apache.oro.text.regex.*; 35 import org.apache.xerces.util.XMLChar; 36 37 import com.hp.hpl.jena.ontology.*; 38 import com.hp.hpl.jena.rdf.model.*; 39 import com.hp.hpl.jena.vocabulary.*; 40 import com.hp.hpl.jena.shared.*; 41 42 43 44 55 public class schemagen { 56 59 60 public static final String NS = "http://jena.hpl.hp.com/2003/04/schemagen#"; 61 62 63 public static final String DEFAULT_CONFIG_URI = "file:schemagen.rdf"; 64 65 66 public static final String DEFAULT_MARKER = "%"; 67 68 69 public static final String DEFAULT_TEMPLATE = "public static final %valclass% %valname% = m_model.%valcreator%( \"%valuri%\" );"; 70 71 72 public static final String DEFAULT_INDIVIDUAL_TEMPLATE = "public static final %valclass% %valname% = m_model.%valcreator%( \"%valuri%\", %valtype% );"; 73 74 75 public static final String DEFAULT_HEADER_TEMPLATE = "/* CVS $" + "Id: $ */%nl%%package% %nl%%imports% %nl%/**%nl% * Vocabulary definitions from %sourceURI% %nl% * @author Auto-generated by schemagen on %date% %nl% */"; 76 77 78 public static final int COMMENT_LENGTH_LIMIT = 80; 79 80 81 82 83 84 protected static final Object OPT_CONFIG_FILE = new Object (); 85 86 87 protected static final Object OPT_NO_COMMENTS = new Object (); 88 89 90 protected static final Object OPT_INPUT = new Object (); 91 92 93 protected static final Object OPT_LANG_DAML = new Object (); 94 95 96 protected static final Object OPT_LANG_OWL = new Object (); 97 98 99 protected static final Object OPT_LANG_RDFS = new Object (); 100 101 102 protected static final Object OPT_OUTPUT = new Object (); 103 104 105 protected static final Object OPT_HEADER = new Object (); 106 107 108 protected static final Object OPT_FOOTER = new Object (); 109 110 111 protected static final Object OPT_ROOT = new Object (); 112 113 114 protected static final Object OPT_MARKER = new Object (); 115 116 117 protected static final Object OPT_PACKAGENAME = new Object (); 118 119 120 protected static final Object OPT_ONTOLOGY = new Object (); 121 122 123 protected static final Object OPT_CLASSNAME = new Object (); 124 125 126 protected static final Object OPT_CLASSDEC = new Object (); 127 128 129 protected static final Object OPT_NAMESPACE = new Object (); 130 131 132 protected static final Object OPT_DECLARATIONS = new Object (); 133 134 135 protected static final Object OPT_PROPERTY_SECTION = new Object (); 136 137 138 protected static final Object OPT_CLASS_SECTION = new Object (); 139 140 141 protected static final Object OPT_INDIVIDUALS_SECTION = new Object (); 142 143 144 protected static final Object OPT_NOPROPERTIES = new Object (); 145 146 147 protected static final Object OPT_NOCLASSES = new Object (); 148 149 150 protected static final Object OPT_NOINDIVIDUALS = new Object (); 151 152 153 protected static final Object OPT_NOHEADER = new Object (); 154 155 156 protected static final Object OPT_PROP_TEMPLATE = new Object (); 157 158 159 protected static final Object OPT_CLASS_TEMPLATE = new Object (); 160 161 162 protected static final Object OPT_INDIVIDUAL_TEMPLATE = new Object (); 163 164 165 protected static final Object OPT_UC_NAMES = new Object (); 166 167 168 protected static final Object OPT_INCLUDE = new Object (); 169 170 171 protected static final Object OPT_CLASSNAME_SUFFIX = new Object (); 172 173 174 protected static final Object OPT_ENCODING = new Object (); 175 176 177 protected static final Object OPT_HELP = new Object (); 178 179 180 protected static final Object OPT_DOS = new Object (); 181 182 183 protected static final Object OPT_USE_INF = new Object (); 184 185 186 187 190 191 194 195 protected List m_cmdLineArgs; 196 197 198 protected Resource m_root; 199 200 201 protected Model m_config = ModelFactory.createDefaultModel(); 202 203 204 protected OntModel m_source; 205 206 207 protected PrintStream m_output; 208 209 210 protected Object [][] m_optionDefinitions = new Object [][] { 211 {OPT_CONFIG_FILE, new OptionDefinition( "-c", null ) }, 212 {OPT_ROOT, new OptionDefinition( "-r", null ) }, 213 {OPT_NO_COMMENTS, new OptionDefinition( "--nocomments", "noComments" ) }, 214 {OPT_INPUT, new OptionDefinition( "-i", "input" ) }, 215 {OPT_LANG_DAML, new OptionDefinition( "--daml", "daml" ) }, 216 {OPT_LANG_OWL, new OptionDefinition( "--owl", "owl" ) }, 217 {OPT_LANG_RDFS, new OptionDefinition( "--rdfs", "rdfs" ) }, 218 {OPT_OUTPUT, new OptionDefinition( "-o", "output" ) }, 219 {OPT_HEADER, new OptionDefinition( "--header", "header" ) }, 220 {OPT_FOOTER, new OptionDefinition( "--footer", "footer" ) }, 221 {OPT_MARKER, new OptionDefinition( "--marker", "marker" ) }, 222 {OPT_PACKAGENAME, new OptionDefinition( "--package", "package" ) }, 223 {OPT_ONTOLOGY, new OptionDefinition( "--ontology", "ontology" ) }, 224 {OPT_CLASSNAME, new OptionDefinition( "-n", "classname" ) }, 225 {OPT_CLASSDEC, new OptionDefinition( "--classdec", "classdec" ) }, 226 {OPT_NAMESPACE, new OptionDefinition( "-a", "namespace" ) }, 227 {OPT_DECLARATIONS, new OptionDefinition( "--declarations", "declarations" ) }, 228 {OPT_PROPERTY_SECTION, new OptionDefinition( "--propSection", "propSection" ) }, 229 {OPT_CLASS_SECTION, new OptionDefinition( "--classSection", "classSection" ) }, 230 {OPT_INDIVIDUALS_SECTION, new OptionDefinition( "--individualsSection", "individualsSection" ) }, 231 {OPT_NOPROPERTIES, new OptionDefinition( "--noproperties", "noproperties" ) }, 232 {OPT_NOCLASSES, new OptionDefinition( "--noclasses", "noclasses" ) }, 233 {OPT_NOINDIVIDUALS, new OptionDefinition( "--noindividuals", "noindividuals" ) }, 234 {OPT_PROP_TEMPLATE, new OptionDefinition( "--propTemplate", "propTemplate" ) }, 235 {OPT_CLASS_TEMPLATE, new OptionDefinition( "--classTemplate", "classTemplate" ) }, 236 {OPT_INDIVIDUAL_TEMPLATE, new OptionDefinition( "--individualTemplate", "individualTemplate" ) }, 237 {OPT_UC_NAMES, new OptionDefinition( "--uppercase", "uppercase" ) }, 238 {OPT_INCLUDE, new OptionDefinition( "--include", "include" ) }, 239 {OPT_CLASSNAME_SUFFIX, new OptionDefinition( "--classnamesuffix", "classnamesuffix" )}, 240 {OPT_NOHEADER, new OptionDefinition( "--noheader", "noheader" )}, 241 {OPT_ENCODING, new OptionDefinition( "-e", "encoding" )}, 242 {OPT_HELP, new OptionDefinition( "--help", null )}, 243 {OPT_DOS, new OptionDefinition( "--dos", "dos" )}, 244 {OPT_USE_INF, new OptionDefinition( "--inference", "inference" )}, 245 }; 246 247 248 protected List m_replacements = new ArrayList(); 249 250 251 protected Perl5Compiler m_perlCompiler = new Perl5Compiler(); 252 253 254 protected PatternMatcher m_matcher = new Perl5Matcher(); 255 256 257 protected String m_nl = "\n"; 258 259 260 protected int m_indentStep = 4; 261 262 263 protected Set m_usedNames = new HashSet(); 264 265 266 protected Map m_resourcesToNames = new HashMap(); 267 268 269 protected List m_includeURI = new ArrayList(); 270 271 272 275 278 279 public static void main( String [] args ) { 280 new schemagen().go( args ); 281 } 282 283 284 287 288 protected void go( String [] args ) { 289 m_cmdLineArgs = Arrays.asList( args ); 291 292 if (m_cmdLineArgs.contains( getOpt( OPT_HELP ).m_cmdLineForm )) { 294 usage(); 295 } 296 297 String configURL = DEFAULT_CONFIG_URI; 299 if (hasValue( OPT_CONFIG_FILE )) { 300 configURL = urlCheck( getValue( OPT_CONFIG_FILE ) ); 302 } 303 304 try { 306 m_config.read( configURL ); 307 } 308 catch (Exception e) { 309 if (!configURL.equals( DEFAULT_CONFIG_URI )) { 311 abort( "Failed to read configuration from URI " + configURL, e ); 312 } 313 } 314 315 processInput(); 317 } 318 319 320 protected void processInput() { 321 determineConfigRoot(); 322 determineLanguage(); 323 selectInput(); 324 selectOutput(); 325 setGlobalReplacements(); 326 327 processHeader(); 328 writeClassDeclaration(); 329 writeInitialDeclarations(); 330 writeProperties(); 331 writeClasses(); 332 writeIndividuals(); 333 writeClassClose(); 334 processFooter(); 335 closeOutput(); 336 } 337 338 339 protected void determineConfigRoot() { 340 if (hasValue( OPT_ROOT )) { 341 String rootURI = getValue( OPT_ROOT ); 342 m_root = m_config.getResource( rootURI ); 343 } 344 else { 345 StmtIterator i = m_config.listStatements( null, RDF.type, m_config.getResource( NS + "Config" ) ); 347 if (i.hasNext()) { 348 m_root = i.nextStatement().getSubject(); 349 } 350 else { 351 m_root = m_config.createResource(); 353 } 354 } 355 356 m_includeURI.addAll( getAllValues( OPT_INCLUDE ) ); 358 } 359 360 361 protected void determineLanguage() { 362 OntModelSpec s = null; 363 if (isTrue( OPT_LANG_DAML )) { 364 if (isTrue( OPT_USE_INF )) { 366 s = OntModelSpec.DAML_MEM_RULE_INF; 367 } 368 else { 369 s = OntModelSpec.DAML_MEM; 370 } 371 } 372 else if (isTrue( OPT_LANG_RDFS )) { 373 if (isTrue( OPT_USE_INF )) { 375 s = OntModelSpec.RDFS_MEM_RDFS_INF; 376 } 377 else { 378 s = OntModelSpec.RDFS_MEM; 379 } 380 } 381 else { 382 if (isTrue( OPT_USE_INF )) { 385 s = OntModelSpec.OWL_MEM_RULE_INF; 386 } 387 else { 388 s = OntModelSpec.OWL_MEM; 389 } 390 } 391 392 m_source = ModelFactory.createOntologyModel( s, null ); 393 m_source.getDocumentManager().setProcessImports( false ); 394 } 395 396 397 protected void selectInput() { 398 if (!hasResourceValue( OPT_INPUT )) { 399 usage(); 400 } 401 402 String input = urlCheck( getValue( OPT_INPUT ) ); 403 String syntax = getValue( OPT_ENCODING ); 404 405 try { 406 if (syntax == null) { 407 m_source.read( input ); 408 } 409 else { 410 m_source.read( input, syntax ); 411 } 412 } 413 catch (JenaException e) { 414 abort( "Failed to read input source " + input, e ); 415 } 416 } 417 418 419 protected void selectOutput() { 420 String outFile = getValue( OPT_OUTPUT ); 421 422 if (outFile == null) { 423 m_output = System.out; 424 } 425 else { 426 try { 427 File out = new File( outFile ); 428 429 if (out.isDirectory()) { 430 String fileName = outFile + System.getProperty( "file.separator" ) + getClassName() + ".java"; 432 out = new File( fileName ); 433 } 434 435 m_output = new PrintStream( new FileOutputStream( out ) ); 436 } 437 catch (Exception e) { 438 abort( "I/O error while trying to open file for writing: " + outFile, null ); 439 } 440 } 441 442 if (isTrue( OPT_DOS )) { 444 m_nl = "\r\n"; 445 } 446 } 447 448 449 protected void processHeader() { 450 String header = hasValue( OPT_HEADER ) ? getValue( OPT_HEADER ) : DEFAULT_HEADER_TEMPLATE; 451 452 if (!hasValue( OPT_NOHEADER )) { 454 writeln( 0, substitute( header ) ); 455 } 456 else { 457 writeln( 0, "import com.hp.hpl.jena.rdf.model.*;" ); 459 if (isTrue( OPT_ONTOLOGY )) { 460 writeln( 0, "import com.hp.hpl.jena.ontology.*;" ); 461 } 462 } 463 } 464 465 466 protected void processFooter() { 467 String footer = getValue( OPT_FOOTER ); 468 469 if (footer != null) { 470 writeln( 0, substitute( footer ) ); 471 } 472 } 473 474 475 protected void setGlobalReplacements() { 476 addReplacementPattern( "date", new SimpleDateFormat ( "dd MMM yyyy HH:mm").format( new Date() ) ); 477 addReplacementPattern( "package", hasValue( OPT_PACKAGENAME ) ? ("package " + getValue( OPT_PACKAGENAME ) + ";") : "" ); 478 addReplacementPattern( "imports", getImports() ); 479 addReplacementPattern( "classname", getClassName() ); 480 addReplacementPattern( "sourceURI", getResource( OPT_INPUT ).getURI() ); 481 addReplacementPattern( "nl", m_nl ); 482 } 483 484 485 protected void addReplacementPattern( String key, String replacement ) { 486 if (replacement != null && key != null) { 487 String marker = getValue( OPT_MARKER ); 488 marker = (marker == null) ? DEFAULT_MARKER : marker; 489 490 try { 491 m_replacements.add( new Replacement( m_perlCompiler.compile( marker + key + marker ), 492 new StringSubstitution( replacement ) ) ); 493 } 494 catch (MalformedPatternException e) { 495 abort( "Malformed regexp pattern " + marker + key + marker, e ); 496 } 497 } 498 } 499 500 501 protected void pop( int n ) { 502 for (int i = 0; i < n; i++) { 503 m_replacements.remove( m_replacements.size() - 1 ); 504 } 505 } 506 507 508 509 protected void closeOutput() { 510 m_output.flush(); 511 m_output.close(); 512 } 513 514 515 516 protected boolean isTrue( Object option ) { 517 return getOpt( option ).isTrue(); 518 } 519 520 521 protected boolean hasValue( Object option ) { 522 return getOpt( option ).hasValue(); 523 } 524 525 526 protected boolean hasResourceValue( Object option ) { 527 return getOpt( option ).hasResourceValue(); 528 } 529 530 531 protected String getValue( Object option ) { 532 return getOpt( option ).getValue(); 533 } 534 535 536 protected List getAllValues( Object option ) { 537 List values = new ArrayList(); 538 OptionDefinition opt = getOpt( option ); 539 540 for (Iterator i = m_cmdLineArgs.iterator(); i.hasNext(); ) { 542 String s = (String ) i.next(); 543 if (s.equals( opt.m_cmdLineForm )) { 544 values.add( i.next() ); 546 } 547 } 548 549 for (StmtIterator i = m_root.listProperties( opt.m_prop ); i.hasNext(); ) { 551 Statement s = i.nextStatement(); 552 553 if (s.getObject() instanceof Literal) { 554 values.add( s.getString() ); 555 } 556 else { 557 values.add( s.getResource().getURI() ); 558 } 559 } 560 561 return values; 562 } 563 564 565 protected Resource getResource( Object option ) { 566 return getOpt( option ).getResource(); 567 } 568 569 570 protected OptionDefinition getOpt( Object option ) { 571 for (int i = 0; i < m_optionDefinitions.length; i++) { 572 if (m_optionDefinitions[i][0] == option) { 573 return (OptionDefinition) m_optionDefinitions[i][1]; 574 } 575 } 576 577 return null; 578 } 579 580 581 protected void abort( String msg, Exception e ) { 582 System.err.println( msg ); 583 if (e != null) { 584 System.err.println( e ); 585 } 586 System.exit( 1 ); 587 } 588 589 590 protected void usage() { 591 System.err.println( "Usage:" ); 592 System.err.println( " java jena.schemagen [options ...]" ); 593 System.err.println(); 594 System.err.println( "Commonly used options include:" ); 595 System.err.println( " -i <input> the source document as a file or URL." ); 596 System.err.println( " -n <name> the name of the created Java class." ); 597 System.err.println( " -a <uri> the namespace URI of the source document." ); 598 System.err.println( " -o <file> the file to write the generated class into." ); 599 System.err.println( " -o <dir> the directory in which the generated Java class is created." ); 600 System.err.println( " By default, output goes to stdout." ); 601 System.err.println( " -e <encoding> the encoding of the input document (N3, RDF/XML, etc)." ); 602 System.err.println( " -c <config> a filename or URL for an RDF document containing " ); 603 System.err.println( " configuration parameters." ); 604 System.err.println(); 605 System.err.println( "Many other options are available. See the schemagen HOWTO in the " ); 606 System.err.println( "Jena documentation for full details." ); 607 System.exit( 1 ); 608 } 609 610 611 protected String substitute( String sIn ) { 612 String s = sIn; 613 614 for (Iterator i = m_replacements.iterator(); i.hasNext(); ) { 615 Replacement r = (Replacement) i.next(); 616 617 s = Util.substitute( m_matcher, r.pattern, r.sub, s, Util.SUBSTITUTE_ALL ); 618 } 619 620 return s; 621 } 622 623 624 protected int indentTo( int i, StringBuffer buf ) { 625 int indent = i * m_indentStep; 626 for (int j = 0; j < indent; j++) { 627 buf.append( ' ' ); 628 } 629 630 return indent; 631 } 632 633 634 protected void writeln( int indent ) { 635 writeln( indent, "" ); 636 } 637 638 639 protected void writeln( int indent, String s ) { 640 write( indent, s ); 641 m_output.print( m_nl ); 642 } 643 644 645 protected void write( int indentLevel, String s ) { 646 for (int i = 0; i < (m_indentStep * indentLevel); i++) { 647 m_output.print( " " ); 648 } 649 650 m_output.print( s ); 651 } 652 653 654 protected String getImports() { 655 StringBuffer buf = new StringBuffer (); 656 buf.append( "import com.hp.hpl.jena.rdf.model.*;" ); 657 buf.append( m_nl ); 658 659 if (useOntology()) { 660 buf.append( "import com.hp.hpl.jena.ontology.*;" ); 661 } 662 663 return buf.toString(); 664 } 665 666 667 protected String getClassName() { 668 if (hasValue( OPT_CLASSNAME )) { 670 return getValue(( OPT_CLASSNAME )); 671 } 672 673 String uri = getValue( OPT_INPUT ); 675 676 uri = (uri.endsWith( "#" )) ? uri.substring( 0, uri.length() - 1 ) : uri; 678 uri = (uri.endsWith( ".daml" )) ? uri.substring( 0, uri.length() - 5 ) : uri; 679 uri = (uri.endsWith( ".owl" )) ? uri.substring( 0, uri.length() - 4 ) : uri; 680 uri = (uri.endsWith( ".rdf" )) ? uri.substring( 0, uri.length() - 4 ) : uri; 681 uri = (uri.endsWith( ".rdfs" )) ? uri.substring( 0, uri.length() - 5 ) : uri; 682 uri = (uri.endsWith( ".n3" )) ? uri.substring( 0, uri.length() - 3 ) : uri; 683 uri = (uri.endsWith( ".xml" )) ? uri.substring( 0, uri.length() - 4 ) : uri; 684 685 int i = uri.length() - 1; 687 for (; i > 0; i--) { 688 if (!Character.isUnicodeIdentifierPart( uri.charAt( i ) ) && 689 uri.charAt( i ) != '-') { 690 i++; 691 break; 692 } 693 } 694 695 String name = uri.substring( i ); 696 697 if (hasValue( OPT_CLASSNAME_SUFFIX )) { 699 name = name + getValue( OPT_CLASSNAME_SUFFIX ); 700 } 701 702 return asLegalJavaID( name, true ); 704 } 705 706 707 protected boolean useOntology() { 708 return isTrue( OPT_ONTOLOGY ); 709 } 710 711 712 protected boolean noComments() { 713 return isTrue( OPT_NO_COMMENTS ); 714 } 715 716 717 protected String asLegalJavaID( String s, boolean cap ) { 718 StringBuffer buf = new StringBuffer (); 719 int i = 0; 720 721 try { 723 for (; !Character.isJavaIdentifierStart( s.charAt( i )); i++) {} 724 } 725 catch (StringIndexOutOfBoundsException e) { 726 System.err.println( "Could not identify legal Java identifier start character in '" + s + "', replacing with __" ); 727 return "__"; 728 } 729 buf.append( cap ? Character.toUpperCase( s.charAt( i ) ) : s.charAt( i ) ); 730 731 for (++i; i < s.length(); i++) { 733 char c = s.charAt( i ); 734 buf.append( Character.isJavaIdentifierPart( c ) ? c : '_' ); 735 } 736 737 return buf.toString(); 738 } 739 740 741 protected void writeClassDeclaration() { 742 write( 0, "public class " ); 743 write( 0, getClassName() ); 744 write( 0, " " ); 745 746 if (hasValue( OPT_CLASSDEC )) { 747 write( 0, getValue( OPT_CLASSDEC ) ); 748 } 749 750 writeln( 0, "{" ); 751 } 752 753 754 protected void writeClassClose() { 755 writeln( 0, "}" ); 756 } 757 758 759 protected void writeInitialDeclarations() { 760 writeModelDeclaration(); 761 writeNamespace(); 762 763 if (hasValue( OPT_DECLARATIONS )) { 764 writeln( 0, getValue( OPT_DECLARATIONS )); 765 } 766 } 767 768 769 protected void writeModelDeclaration() { 770 if (useOntology()) { 771 String lang = "OWL"; 772 if (isTrue( OPT_LANG_DAML )) { 773 lang = "DAML"; 774 } 775 else if (isTrue( OPT_LANG_RDFS )) { 776 lang = "RDFS"; 777 } 778 writeln( 1, "/** <p>The ontology model that holds the vocabulary terms</p> */" ); 779 writeln( 1, "private static OntModel m_model = ModelFactory.createOntologyModel( OntModelSpec." + lang + "_MEM, null );" ); 780 } 781 else { 782 writeln( 1, "/** <p>The RDF model that holds the vocabulary terms</p> */" ); 783 writeln( 1, "private static Model m_model = ModelFactory.createDefaultModel();" ); 784 } 785 786 writeln( 1 ); 787 } 788 789 790 protected void writeNamespace() { 791 String nsURI = determineNamespaceURI(); 792 793 writeln( 1, "/** <p>The namespace of the vocabulary as a string</p> */" ); 794 writeln( 1, "public static final String NS = \"" + nsURI + "\";" ); 795 writeln( 1 ); 796 797 writeln( 1, "/** <p>The namespace of the vocabulary as a string</p>" ); 798 writeln( 1, " * @see #NS */" ); 799 writeln( 1, "public static String getURI() {return NS;}" ); 800 writeln( 1 ); 801 802 writeln( 1, "/** <p>The namespace of the vocabulary as a resource</p> */" ); 803 writeln( 1, "public static final Resource NAMESPACE = m_model.createResource( NS );" ); 804 writeln( 1 ); 805 } 806 807 808 809 protected String determineNamespaceURI() { 810 if (hasResourceValue( OPT_NAMESPACE )) { 812 String ns = getResource( OPT_NAMESPACE ).getURI(); 813 814 m_includeURI.add( ns ); 816 817 return ns; 818 } 819 820 String defaultNS = m_source.getNsPrefixURI( "" ); 822 if (defaultNS == null) { 823 defaultNS = m_source.getBaseModel().getNsPrefixURI( "" ); 824 } 825 826 if (defaultNS != null) { 827 m_includeURI.add( defaultNS ); 828 return defaultNS; 829 } 830 831 try { 833 Resource ont = m_source.getBaseModel() 834 .listStatements( null, RDF.type, m_source.getProfile().ONTOLOGY() ) 835 .nextStatement() 836 .getSubject(); 837 838 String uri = ont.getURI(); 839 840 char ch = uri.charAt( uri.length() - 1 ); 842 boolean endsWithNCNameCh = XMLChar.isNCName( ch ); 843 uri = endsWithNCNameCh ? uri + "#" : uri; 844 845 m_includeURI.add( uri ); 847 848 return uri; 849 } 850 catch (Exception e) { 851 abort( "Could not determine the base URI for the input vocabulary", null ); 852 return null; 853 } 854 } 855 856 857 858 protected void writeProperties() { 859 if (isTrue( OPT_NOPROPERTIES )) { 860 return; 861 } 862 863 if (hasValue( OPT_PROPERTY_SECTION )) { 864 writeln( 0, getValue( OPT_PROPERTY_SECTION )); 865 } 866 867 if (useOntology()) { 868 writeObjectProperties(); 869 writeDatatypeProperties(); 870 writeAnnotationProperties(); 871 872 writeRDFProperties(); 875 } 876 else { 877 writeRDFProperties(); 878 } 879 } 880 881 882 protected void writeObjectProperties() { 883 String template = hasValue( OPT_PROP_TEMPLATE ) ? getValue( OPT_PROP_TEMPLATE ) : DEFAULT_TEMPLATE; 884 885 if (!isTrue( OPT_LANG_RDFS )) { 886 for (Iterator i = m_source.listObjectProperties(); i.hasNext(); ) { 887 writeValue( (Resource) i.next(), template, "ObjectProperty", "createObjectProperty", "_PROP" ); 888 } 889 } 890 } 891 892 893 protected void writeDatatypeProperties() { 894 String template = hasValue( OPT_PROP_TEMPLATE ) ? getValue( OPT_PROP_TEMPLATE ) : DEFAULT_TEMPLATE; 895 896 if (!isTrue( OPT_LANG_RDFS )) { 897 for (Iterator i = m_source.listDatatypeProperties(); i.hasNext(); ) { 898 writeValue( (Resource) i.next(), template, "DatatypeProperty", "createDatatypeProperty", "_PROP" ); 899 } 900 } 901 } 902 903 904 protected void writeAnnotationProperties() { 905 String template = hasValue( OPT_PROP_TEMPLATE ) ? getValue( OPT_PROP_TEMPLATE ) : DEFAULT_TEMPLATE; 906 907 if (!isTrue( OPT_LANG_RDFS )) { 908 for (Iterator i = m_source.listAnnotationProperties(); i.hasNext(); ) { 909 writeValue( (Resource) i.next(), template, "AnnotationProperty", "createAnnotationProperty", "_PROP" ); 910 } 911 } 912 } 913 914 915 protected void writeRDFProperties() { 916 String template = hasValue( OPT_PROP_TEMPLATE ) ? getValue( OPT_PROP_TEMPLATE ) : DEFAULT_TEMPLATE; 917 918 Resource[] props; 920 if (isTrue( OPT_LANG_OWL )) { 921 props = new Resource[] {OWL.ObjectProperty, OWL.DatatypeProperty, RDF.Property}; 922 } 923 else if (isTrue( OPT_LANG_DAML )) { 924 props = new Resource[] {DAML_OIL.ObjectProperty, DAML_OIL.DatatypeProperty, RDF.Property}; 925 } 926 else { 927 props = new Resource[] {RDF.Property}; 928 } 929 930 for (int j = 0; j < props.length; j++) { 932 for (StmtIterator i = m_source.listStatements( null, RDF.type, props[j] ); i.hasNext(); ) { 933 writeValue( i.nextStatement().getSubject(), template, "Property", "createProperty", "_PROP" ); 934 } 935 } 936 } 937 938 939 protected void writeClasses() { 940 if (isTrue( OPT_NOCLASSES )) { 941 return; 942 } 943 944 if (hasValue( OPT_CLASS_SECTION )) { 945 writeln( 0, getValue( OPT_CLASS_SECTION )); 946 } 947 948 if (useOntology()) { 949 writeOntClasses(); 950 } 951 else { 952 writeRDFClasses(); 953 } 954 } 955 956 957 protected void writeOntClasses() { 958 String template = hasValue( OPT_CLASS_TEMPLATE ) ? getValue( OPT_CLASS_TEMPLATE ) : DEFAULT_TEMPLATE; 959 960 for (Iterator i = m_source.listClasses(); i.hasNext(); ) { 961 writeValue( (Resource) i.next(), template, "OntClass", "createClass", "_CLASS" ); 962 } 963 } 964 965 966 protected void writeRDFClasses() { 967 String template = hasValue( OPT_CLASS_TEMPLATE ) ? getValue( OPT_CLASS_TEMPLATE ) : DEFAULT_TEMPLATE; 968 969 Resource cls = OWL.Class; 971 if (isTrue( OPT_LANG_DAML )) { 972 cls = DAML_OIL.Class; 973 } 974 else if (isTrue( OPT_LANG_RDFS )) { 975 cls = RDFS.Class; 976 } 977 978 for (StmtIterator i = m_source.listStatements( null, RDF.type, cls ); i.hasNext(); ) { 979 writeValue( i.nextStatement().getSubject(), template, "Resource", "createResource", "_CLASS" ); 980 } 981 } 982 983 984 protected void writeIndividuals() { 985 if (isTrue( OPT_NOINDIVIDUALS )) { 986 return; 987 } 988 989 if (hasValue( OPT_INDIVIDUALS_SECTION )) { 990 writeln( 0, getValue( OPT_INDIVIDUALS_SECTION )); 991 } 992 993 if (useOntology()) { 994 writeOntIndividuals(); 995 } 996 else { 997 writeRDFIndividuals(); 998 } 999 } 1000 1001 1002 protected void writeOntIndividuals() { 1003 String template = hasValue( OPT_INDIVIDUAL_TEMPLATE ) ? getValue( OPT_INDIVIDUAL_TEMPLATE ) : DEFAULT_INDIVIDUAL_TEMPLATE; 1004 1005 for (StmtIterator i = m_source.listStatements( null, RDF.type, (RDFNode) null ); i.hasNext(); ) { 1006 Statement candidate = i.nextStatement(); 1007 1008 if (candidate.getObject() instanceof Resource) { 1009 Resource candObj = (Resource)candidate.getObject(); 1010 1011 if (!candObj.isAnon()) { 1012 String uri = candObj.getURI(); 1013 1014 for (Iterator j = m_includeURI.iterator(); j.hasNext(); ) { 1015 if (uri.startsWith( (String ) j.next() )) { 1016 Resource ind = candidate.getSubject(); 1018 1019 String varName = (String ) m_resourcesToNames.get( candidate.getObject() ); 1021 String valType = (varName != null) ? varName : "m_model.createClass( \"" + uri + "\" )"; 1022 1023 addReplacementPattern( "valtype", valType ); 1025 writeValue( ind, template, "Individual", "createIndividual", "_INSTANCE" ); 1026 pop( 1 ); 1027 1028 break; 1029 } 1030 } 1031 } 1032 } 1033 } 1034 } 1035 1036 1037 protected void writeRDFIndividuals() { 1038 String template = hasValue( OPT_INDIVIDUAL_TEMPLATE ) ? getValue( OPT_INDIVIDUAL_TEMPLATE ) : DEFAULT_TEMPLATE; 1039 1040 for (StmtIterator i = m_source.listStatements( null, RDF.type, (RDFNode) null ); i.hasNext(); ) { 1041 Statement candidate = i.nextStatement(); 1042 1043 if (candidate.getObject() instanceof Resource) { 1044 Resource candObj = candidate.getResource(); 1045 1046 if (!candObj.isAnon()) { 1047 String uri = candObj.getURI(); 1048 1049 for (Iterator j = m_includeURI.iterator(); j.hasNext(); ) { 1050 if (uri.startsWith( (String ) j.next() )) { 1051 writeValue( candidate.getSubject(), template, "Resource", "createResource", "_INSTANCE" ); 1053 1054 break; 1055 } 1056 } 1057 } 1058 } 1059 } 1060 } 1061 1062 1063 protected void writeValue( Resource r, String template, String valueClass, String creator, String disambiguator ) { 1064 if (!filter( r )) { 1065 if (!noComments() && hasComment( r )) { 1066 writeln( 1, formatComment( getComment( r ) ) ); 1067 } 1068 1069 addReplacementPattern( "valuri", r.getURI() ); 1071 addReplacementPattern( "valname", getValueName( r, disambiguator )); 1072 addReplacementPattern( "valclass", valueClass ); 1073 addReplacementPattern( "valcreator", creator ); 1074 1075 writeln( 1, substitute( template ) ); 1077 writeln( 1 ); 1078 1079 pop( 4 ); 1081 } 1082 } 1083 1084 1085 protected boolean hasComment( Resource r ) { 1086 return r.hasProperty( RDFS.comment ) || r.hasProperty( DAML_OIL.comment ); 1087 } 1088 1089 1090 protected String getComment( Resource r ) { 1091 StringBuffer comment = new StringBuffer (); 1092 1093 for (NodeIterator ni = m_source.listObjectsOfProperty( r, RDFS.comment ); ni.hasNext(); ) { 1095 RDFNode n = ni.nextNode(); 1097 if (n instanceof Literal) { 1098 comment.append( ((Literal) n).getLexicalForm().trim() ); 1099 } 1100 else { 1101 LogFactory.getLog( getClass() ).debug( "Not a literal: " + n ); 1102 } 1103 } 1104 1105 for (NodeIterator ni = m_source.listObjectsOfProperty( r, DAML_OIL.comment ); ni.hasNext(); ) { 1106 comment.append( ((Literal) ni.nextNode()).getLexicalForm().trim() ); 1107 } 1108 1109 return comment.toString(); 1110 } 1111 1112 1113 protected String formatComment( String comment ) { 1114 StringBuffer buf = new StringBuffer (); 1115 buf.append( "/** <p>" ); 1116 1117 boolean inSpace = false; 1118 int pos = buf.length(); 1119 boolean singleLine = true; 1120 1121 for (int i = 0; i < comment.length(); i++ ) { 1124 char c = comment.charAt( i ); 1125 1126 if (Character.isWhitespace( c )) { 1128 if (inSpace) { 1129 continue; } 1131 else { 1132 c = ' '; inSpace = true; 1134 } 1135 } 1136 else { 1137 inSpace = false; 1138 } 1139 1140 if (c == '\\') { 1142 c = comment.charAt( ++i ); 1143 1144 switch (c) { 1145 case 'n': 1146 buf.append( m_nl ); 1147 pos = indentTo( 1, buf ); 1148 buf.append( " * " ); 1149 pos += 3; 1150 singleLine = false; 1151 break; 1152 1153 default: 1154 break; 1156 } 1157 } 1158 else if (c == '<') { 1159 buf.append( "<" ); 1160 pos += 4; 1161 } 1162 else if (c == '>') { 1163 buf.append( ">" ); 1164 pos += 4; 1165 } 1166 else if (c == '&') { 1167 buf.append( "&" ); 1168 pos += 5; 1169 } 1170 else { 1171 buf.append( c ); 1173 pos++; 1174 } 1175 1176 if ((pos > COMMENT_LENGTH_LIMIT) && (inSpace)) { 1178 buf.append( m_nl ); 1179 pos = indentTo( 1, buf ); 1180 buf.append( " * " ); 1181 pos += 3; 1182 singleLine = false; 1183 } 1184 } 1185 1186 buf.append( "</p>" ); 1187 buf.append( singleLine ? "" : m_nl ); 1188 indentTo( singleLine ? 0 : 1, buf ); 1189 buf.append( " */" ); 1190 return buf.toString(); 1191 } 1192 1193 1194 protected boolean filter( Resource r ) { 1195 if (r.isAnon()) { 1196 return true; 1197 } 1198 1199 if (m_resourcesToNames.containsKey( r )) { 1201 return true; 1202 } 1203 1204 for (Iterator i = m_includeURI.iterator(); i.hasNext(); ) { 1206 String uri = (String ) i.next(); 1207 if (r.getURI().startsWith( uri )) { 1208 return false; 1210 } 1211 } 1212 1213 return true; 1215 } 1216 1217 1218 protected String getValueName( Resource r, String disambiguator ) { 1219 String name = isTrue( OPT_UC_NAMES ) ? getUCValueName( r ) : r.getLocalName(); 1221 1222 name = asLegalJavaID( name, false ); 1224 1225 int attempt = 0; 1227 String baseName = name; 1228 while (m_usedNames.contains( name )) { 1229 name = (attempt == 0) ? (name + disambiguator) : (baseName + disambiguator + attempt); 1230 attempt++; 1231 } 1232 1233 m_usedNames.add( name ); 1235 1236 m_resourcesToNames.put( r, name ); 1238 1239 return name; 1240 } 1241 1242 1243 protected String getUCValueName( Resource r ) { 1244 StringBuffer buf = new StringBuffer (); 1245 String localName = r.getLocalName(); 1246 char lastChar = 0; 1247 1248 for (int i = 0; i < localName.length(); i++) { 1249 char c = localName.charAt(i); 1250 1251 if (Character.isLowerCase(lastChar) && Character.isUpperCase(c)) { 1252 buf.append( '_' ); 1253 } 1254 buf.append( Character.toUpperCase(c) ); 1255 lastChar = c; 1256 } 1257 1258 return buf.toString(); 1259 } 1260 1261 1264 protected String urlCheck( String uriOrFile ) { 1265 boolean legal = true; 1266 String url = uriOrFile; 1267 1268 try { 1270 new URL ( url ); 1271 } 1272 catch (MalformedURLException ignore) { 1273 legal = false; 1274 } 1275 1276 if (!legal) { 1278 legal = true; 1279 String slash = System.getProperty( "file.separator" ); 1280 url = "file:" + (uriOrFile.startsWith( slash ) ? (slash + slash) : "") + uriOrFile; 1281 1282 try { 1283 new URL ( url ); 1284 } 1285 catch (MalformedURLException ignore) { 1286 legal = false; 1287 } 1288 } 1289 1290 if (!legal) { 1291 abort( "Could not parse " + uriOrFile + " as a legal URL or a file reference. Aborting.", null ); 1292 } 1293 1294 return url; 1295 } 1296 1297 1298 1302 1303 protected class OptionDefinition 1304 { 1305 protected String m_cmdLineForm; 1306 protected Property m_prop; 1307 1308 protected OptionDefinition( String cmdLineForm, String name ) { 1309 m_cmdLineForm = cmdLineForm; 1310 if (name != null) { 1311 m_prop = m_config.getProperty( NS, name ); 1312 } 1313 } 1314 1315 1321 protected boolean isTrue() { 1322 if (m_cmdLineArgs.contains( m_cmdLineForm )) { 1323 return true; 1324 } 1325 1326 if (m_root.hasProperty( m_prop )) { 1327 return m_root.getRequiredProperty( m_prop ).getBoolean(); 1328 } 1329 1330 return false; 1331 } 1332 1333 1339 protected String getValue() { 1340 int index = m_cmdLineArgs.indexOf( m_cmdLineForm ); 1341 1342 if (index >= 0) { 1343 try { 1344 return (String ) m_cmdLineArgs.get( index + 1 ); 1345 } 1346 catch (IndexOutOfBoundsException e) { 1347 System.err.println( "Value for parameter " + m_cmdLineForm + " not set! Aborting."); 1348 } 1349 } 1350 1351 if (m_prop != null && m_root.hasProperty( m_prop )) { 1352 return m_root.getRequiredProperty( m_prop ).getString(); 1353 } 1354 1355 return null; 1357 } 1358 1359 1364 protected boolean hasValue() { 1365 return getValue() != null; 1366 } 1367 1368 1369 1374 protected Resource getResource() { 1375 int index = m_cmdLineArgs.indexOf( m_cmdLineForm ); 1376 1377 if (index >= 0) { 1378 try { 1379 return m_config.getResource( (String ) m_cmdLineArgs.get( index + 1 ) ); 1380 } 1381 catch (IndexOutOfBoundsException e) { 1382 System.err.println( "Value for parameter " + m_cmdLineForm + " not set! Aborting."); 1383 } 1384 } 1385 1386 if (m_prop != null && m_root.hasProperty( m_prop )) { 1387 return m_root.getRequiredProperty( m_prop ).getResource(); 1388 } 1389 1390 return null; 1392 } 1393 1394 1399 protected boolean hasResourceValue() { 1400 return getResource() != null; 1401 } 1402 } 1404 1405 1406 protected class Replacement 1407 { 1408 protected Substitution sub; 1409 protected Pattern pattern; 1410 1411 protected Replacement( Pattern pattern, Substitution sub) { 1412 this.sub = sub; 1413 this.pattern = pattern; 1414 } 1415 } } 1417 1418 1419 1448 | Popular Tags |