turtleentityrenderer.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,609 行 · 第 1/4 页

JAVA
1,609
字号
//The MIT License//// Copyright (c) 2004 Mindswap Research Group, University of Maryland, College Park//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions://// The above copyright notice and this permission notice shall be included in// all copies or substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS// IN THE SOFTWARE.package org.mindswap.swoop.renderer.entity;import java.awt.Component;import java.io.IOException;import java.io.PrintWriter;import java.io.StringWriter;import java.io.Writer;import java.net.URI;import java.net.URISyntaxException;import java.util.Collection;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import javax.swing.JEditorPane;import org.apache.commons.lang.StringEscapeUtils;import org.mindswap.swoop.SwoopDisplayPanel;import org.mindswap.swoop.SwoopModel;import org.mindswap.swoop.TermsDisplay;import org.mindswap.swoop.renderer.BaseEntityRenderer;import org.mindswap.swoop.renderer.SwoopEditableRenderer;import org.mindswap.swoop.renderer.SwoopEntityRenderer;import org.mindswap.swoop.renderer.SwoopRenderingVisitor;import org.mindswap.swoop.utils.owlapi.DefaultShortFormProvider;import org.mindswap.swoop.utils.owlapi.OWLDescriptionFinder;import org.mindswap.swoop.utils.owlapi.QNameShortFormProvider;import org.semanticweb.owl.io.RendererException;import org.semanticweb.owl.io.ShortFormProvider;import org.semanticweb.owl.model.OWLAnd;import org.semanticweb.owl.model.OWLAnnotationInstance;import org.semanticweb.owl.model.OWLAnnotationProperty;import org.semanticweb.owl.model.OWLBooleanDescription;import org.semanticweb.owl.model.OWLClass;import org.semanticweb.owl.model.OWLDataAllRestriction;import org.semanticweb.owl.model.OWLDataCardinalityRestriction;import org.semanticweb.owl.model.OWLDataEnumeration;import org.semanticweb.owl.model.OWLDataProperty;import org.semanticweb.owl.model.OWLDataPropertyInstance;import org.semanticweb.owl.model.OWLDataPropertyRangeAxiom;import org.semanticweb.owl.model.OWLDataRange;import org.semanticweb.owl.model.OWLDataSomeRestriction;import org.semanticweb.owl.model.OWLDataType;import org.semanticweb.owl.model.OWLDataValue;import org.semanticweb.owl.model.OWLDataValueRestriction;import org.semanticweb.owl.model.OWLDescription;import org.semanticweb.owl.model.OWLDifferentIndividualsAxiom;import org.semanticweb.owl.model.OWLDisjointClassesAxiom;import org.semanticweb.owl.model.OWLEntity;import org.semanticweb.owl.model.OWLEnumeration;import org.semanticweb.owl.model.OWLEquivalentClassesAxiom;import org.semanticweb.owl.model.OWLEquivalentPropertiesAxiom;import org.semanticweb.owl.model.OWLException;import org.semanticweb.owl.model.OWLFrame;import org.semanticweb.owl.model.OWLFunctionalPropertyAxiom;import org.semanticweb.owl.model.OWLIndividual;import org.semanticweb.owl.model.OWLIndividualTypeAssertion;import org.semanticweb.owl.model.OWLInverseFunctionalPropertyAxiom;import org.semanticweb.owl.model.OWLInversePropertyAxiom;import org.semanticweb.owl.model.OWLNamedObject;import org.semanticweb.owl.model.OWLNot;import org.semanticweb.owl.model.OWLObjectAllRestriction;import org.semanticweb.owl.model.OWLObjectCardinalityRestriction;import org.semanticweb.owl.model.OWLObjectProperty;import org.semanticweb.owl.model.OWLObjectPropertyInstance;import org.semanticweb.owl.model.OWLObjectPropertyRangeAxiom;import org.semanticweb.owl.model.OWLObjectSomeRestriction;import org.semanticweb.owl.model.OWLObjectValueRestriction;import org.semanticweb.owl.model.OWLOntology;import org.semanticweb.owl.model.OWLOr;import org.semanticweb.owl.model.OWLProperty;import org.semanticweb.owl.model.OWLPropertyDomainAxiom;import org.semanticweb.owl.model.OWLRestriction;import org.semanticweb.owl.model.OWLSameIndividualsAxiom;import org.semanticweb.owl.model.OWLSubClassAxiom;import org.semanticweb.owl.model.OWLSubPropertyAxiom;import org.semanticweb.owl.model.OWLSymmetricPropertyAxiom;import org.semanticweb.owl.model.OWLTransitivePropertyAxiom;import org.semanticweb.owl.model.helper.OntologyHelper;/** * @author Dave * * To change the template for this generated type comment go to * Window - Preferences - Java - Code Generation - Code and Comments */public class TurtleEntityRenderer extends BaseEntityRenderer implements SwoopEditableRenderer,  SwoopEntityRenderer {		protected class TurtleVisitor implements org.mindswap.swoop.renderer.SwoopRenderingVisitor {				ShortFormProvider shortForms; 		boolean indenting = true;		StringWriter sw;		PrintWriter pw;				int indentLevel = 0;				public TurtleVisitor(ShortFormProvider shortForms) {			this.shortForms = shortForms;			reset();		}				public String result() {			return sw.toString();		}		public void reset() {			sw = new StringWriter();			pw = new PrintWriter( sw );		}				// done		public void visit( OWLDataValue cd ) throws OWLException {			pw.print( "\"" + escape(cd.getValue())  + "\"");			/* Only show it if it's not string */			URI dvdt = cd.getURI();			// enter the data value URI into the qname provider so we can print its prefix out properl			myQNameProvider.shortForm(dvdt);						String dvlang = cd.getLang();			if ( dvdt!=null) {				pw.print( "^^" + encodeHLink(dvdt, shortForms.shortForm(dvdt)));			} else {				if (dvlang!=null) {					pw.print( "@" + escape(dvlang) );				}			}		}		// done		public void visit( OWLAnd and ) throws OWLException {			try			{						URI uri = new URI(OWL_INTERSECTIONOF);				pw.print( encodeHLink(uri, shortForms.shortForm(uri)) + TAB + "("+ SPACE);				pw.print(BREAK);				indentLevel ++;				for ( Iterator it = and.getOperands().iterator();it.hasNext(); ) {					OWLDescription desc = (OWLDescription) it.next();					System.out.println( desc );					pw.println(getIndentString());					desc.accept( this );					if (it.hasNext()) {						pw.println(BREAK);					}				}				indentLevel --;				pw.print(SPACE + ")" + SPACE);			}			catch (Exception ex)			{ ex.printStackTrace(); }		}		// done 		public void visit( OWLOr or ) throws OWLException {				try			{					URI uri = new URI(OWL_UNIONOF);				pw.print( encodeHLink(uri, shortForms.shortForm(uri)) + TAB + "("+ SPACE);				pw.print(BREAK);				indentLevel ++;				for ( Iterator it = or.getOperands().iterator();it.hasNext(); ) {					OWLDescription desc = (OWLDescription) it.next();					pw.print(getIndentString());					desc.accept( this );					if (it.hasNext()) {						pw.println(BREAK);					}				}				indentLevel --;				pw.print(SPACE + ")" + SPACE);			}			catch (Exception ex)			{ ex.printStackTrace(); }		}		// done		public void visit( OWLNot not ) throws OWLException {						try			{									URI uri = new URI(OWL_COMPLEMENTOF);				pw.print( encodeHLink(uri, shortForms.shortForm(uri)) + TAB + SPACE);				pw.print(BREAK);				indentLevel ++;				OWLDescription desc = not.getOperand();				pw.print(getIndentString());				desc.accept(this);				indentLevel --;				pw.print(SPACE + SPACE);			}			catch (Exception ex)						{ ex.printStackTrace(); }		}		// done		public void visit( OWLEnumeration enumeration ) throws OWLException {			try			{									URI uri = new URI(OWL_ONEOF);				pw.print( encodeHLink(uri, shortForms.shortForm(uri)) + TAB + "("+ SPACE);				pw.print(BREAK);				indentLevel ++;				for ( Iterator it = enumeration.getIndividuals().iterator();it.hasNext(); ) {					OWLIndividual ind = (OWLIndividual) it.next();					pw.print(getIndentString());					ind.accept( this );					if (it.hasNext()) {						pw.print(BREAK);					}				}				indentLevel --;				pw.print(SPACE + ")" + SPACE);			}			catch (Exception ex)						{ ex.printStackTrace(); }		}		// DONE		public void visit( OWLObjectSomeRestriction restriction ) throws OWLException {			try			{				pw.print("[ "+SPACE);				URI uri = new URI(OWL_RESTRICTION);				pw.print( encodeHLink(RDF_TYPE_URI, "a") + TAB +					   encodeHLink(uri, shortForms.shortForm(uri)) + ";" + BREAK);								uri = new URI(OWL_ONPROPERTY);				print( SPACE + SPACE + encodeHLink(uri, shortForms.shortForm(uri)) + TAB);				restriction.getObjectProperty().accept( this );				pw.print(";"+BREAK);  // avoid indentation				uri = new URI(OWL_SOMEVALUESFROM);				print( SPACE + SPACE + encodeHLink(uri, shortForms.shortForm(uri)) + TAB);							OWLDescription desc = restriction.getDescription();				//System.out.println( "Obj Restriction: " +desc.getClass().toString() );				if (!(desc instanceof OWLClass) && !(desc instanceof OWLRestriction))					pw.print("[ ");				if (desc instanceof OWLRestriction){					indentLevel ++;					pw.print(BREAK + getIndentString());				}				desc.accept( this );				if (!(desc instanceof OWLClass) && !(desc instanceof OWLRestriction))					pw.print(" ]");				if (desc instanceof OWLRestriction){					indentLevel --;				}				println();				print(" ]");			}			catch (Exception ex)			{ ex.printStackTrace(); }		}		// done		public void visit( OWLObjectAllRestriction restriction ) throws OWLException {			try			{				pw.print("[ "+SPACE);				URI uri = new URI(OWL_RESTRICTION);				pw.print( encodeHLink(RDF_TYPE_URI, "a") + TAB +					   encodeHLink(uri, shortForms.shortForm(uri)) + ";" + BREAK);								uri = new URI(OWL_ONPROPERTY);				print( SPACE + SPACE + encodeHLink(uri, shortForms.shortForm(uri)) + TAB);				restriction.getObjectProperty().accept( this );				pw.print(";"+BREAK);  // avoid indentation				uri = new URI(OWL_ALLVALUESFROM);				print( SPACE + SPACE + encodeHLink(uri, shortForms.shortForm(uri)) + TAB);								OWLDescription desc = restriction.getDescription();				if (!(desc instanceof OWLClass) && !(desc instanceof OWLRestriction))					pw.print("[ ");				if (desc instanceof OWLRestriction){					indentLevel ++;					pw.print(BREAK + getIndentString());				}								desc.accept( this );				if (!(desc instanceof OWLClass) && !(desc instanceof OWLRestriction))					pw.print(" ]");				if (desc instanceof OWLRestriction){					indentLevel --;				}				println();				print(" ]");			}			catch (Exception ex)			{ ex.printStackTrace(); }		}		// done			public void visit( OWLObjectValueRestriction restriction ) throws OWLException {			try			{				pw.print("[ "+SPACE);				URI uri = new URI(OWL_RESTRICTION);				pw.print( encodeHLink(RDF_TYPE_URI, "a") + TAB +					   encodeHLink(uri, shortForms.shortForm(uri)) + ";" + BREAK);								uri = new URI(OWL_ONPROPERTY);				print( SPACE + SPACE + encodeHLink(uri, shortForms.shortForm(uri)) + TAB);				restriction.getObjectProperty().accept( this );				pw.print(";"+BREAK);  // avoid indentation				uri = new URI(OWL_HASVALUE);				print( SPACE + SPACE + encodeHLink(uri, shortForms.shortForm(uri)) + TAB);				restriction.getIndividual().accept( this );				println();				print(" ]");			}			catch (Exception ex)			{ ex.printStackTrace(); }		}		// done		public void visit( OWLDataSomeRestriction restriction ) throws OWLException {			try			{				pw.print("[ "+SPACE);				URI uri = new URI(OWL_RESTRICTION);				pw.print( encodeHLink(RDF_TYPE_URI, "a") + TAB +					   encodeHLink(uri, shortForms.shortForm(uri)) + ";" + BREAK);								uri = new URI(OWL_ONPROPERTY);				print( SPACE + SPACE + encodeHLink(uri, shortForms.shortForm(uri)) + TAB);				restriction.getDataProperty().accept( this );				pw.print(";"+BREAK);  // avoid indentation				uri = new URI(OWL_SOMEVALUESFROM);				print( SPACE + SPACE + encodeHLink(uri, shortForms.shortForm(uri)) + TAB);							OWLDataRange ran = restriction.getDataType();				if (!(ran instanceof OWLClass))					pw.print("[ ");				ran.accept( this );				if (!(ran instanceof OWLClass))					pw.print(" ]");								println();				print(" ]");			}			catch (Exception ex)			{ ex.printStackTrace(); }		}		// done		public void visit( OWLDataAllRestriction restriction ) throws OWLException {			try			{				pw.print("[ "+SPACE);				URI uri = new URI(OWL_RESTRICTION);				pw.print( encodeHLink(RDF_TYPE_URI, "a") + TAB +					   encodeHLink(uri, shortForms.shortForm(uri)) + ";" + BREAK);								uri = new URI(OWL_ONPROPERTY);				print( SPACE + SPACE + encodeHLink(uri, shortForms.shortForm(uri)) + TAB);				restriction.getDataProperty().accept( this );				pw.print(";"+BREAK);  // avoid indentation				uri = new URI(OWL_ALLVALUESFROM);				print( SPACE + SPACE + encodeHLink(uri, shortForms.shortForm(uri)) + TAB);							OWLDataRange ran = restriction.getDataType();				if (!(ran instanceof OWLClass))					pw.print("[ ");				ran.accept( this );				if (!(ran instanceof OWLClass))					pw.print(" ]");				println();				print(" ]");			}			catch (Exception ex)			{ ex.printStackTrace(); }		}		// done		public void visit( OWLObjectCardinalityRestriction restriction ) throws OWLException {			try			{							pw.print("[ " + SPACE);				URI uri = new URI(OWL_RESTRICTION);				pw.print( encodeHLink(RDF_TYPE_URI, "a") + TAB +						encodeHLink(uri, shortForms.shortForm(uri)) + ";" + BREAK);							uri = new URI(OWL_ONPROPERTY);				print( SPACE + SPACE + encodeHLink(uri, shortForms.shortForm(uri)) + TAB);								restriction.getObjectProperty().accept( this );								pw.print(";"+BREAK);  // avoid syntax indentation								if ( restriction.isExactly() ) {					uri = new URI(OWL_CARDINALITY);					print( SPACE + SPACE + encodeHLink(uri, shortForms.shortForm(uri)) + TAB + restriction.getAtLeast());

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?