2ronwalf/*
2ronwalf * Created on May 7, 2005
2ronwalf */
2ronwalfpackage org.mindswap.owls.io;
2ronwalf
2ronwalfimport java.io.OutputStream;
2ronwalfimport java.io.PrintWriter;
2ronwalfimport java.io.Writer;
2ronwalfimport java.net.URI;
2ronwalf
2ronwalfimport org.mindswap.owl.OWLClass;
2ronwalfimport org.mindswap.owl.OWLIndividual;
2ronwalfimport org.mindswap.owl.OWLProperty;
2ronwalfimport org.mindswap.owl.OWLValue;
2ronwalfimport org.mindswap.owls.generic.expression.Expression;
2ronwalfimport org.mindswap.swrl.Atom;
2ronwalfimport org.mindswap.swrl.AtomList;
2ronwalfimport org.mindswap.swrl.BuiltinAtom;
2ronwalfimport org.mindswap.swrl.ClassAtom;
2ronwalfimport org.mindswap.swrl.DataPropertyAtom;
2ronwalfimport org.mindswap.swrl.DifferentIndividualsAtom;
2ronwalfimport org.mindswap.swrl.IndividualPropertyAtom;
2ronwalfimport org.mindswap.swrl.SameIndividualAtom;
2ronwalfimport org.mindswap.utils.QNameProvider;
2ronwalf
2ronwalf/**
2ronwalf * @author Evren Sirin
2ronwalf *
2ronwalf */
2ronwalfpublic abstract class BaseExpressionWriter implements ExpressionWriter {
2ronwalf protected QNameProvider qnames;
2ronwalf protected PrintWriter out;
2ronwalf protected String indent;
2ronwalf protected boolean firstLineIndent;
2ronwalf
2ronwalf public BaseExpressionWriter() {
2ronwalf firstLineIndent = false;
2ronwalf }
2ronwalf
2ronwalf public void setWriter(Writer out) {
2ronwalf if( out instanceof PrintWriter )
2ronwalf this.out = (PrintWriter) out;
2ronwalf else
2ronwalf this.out = new PrintWriter( out );
2ronwalf }
2ronwalf
2ronwalf public Writer getWriter() {
2ronwalf return out;
2ronwalf }
2ronwalf
2ronwalf public void setWriter(OutputStream out) {
2ronwalf setWriter( new PrintWriter( out ) );
2ronwalf }
2ronwalf
2ronwalf public String getIndent() {
2ronwalf return indent;
2ronwalf }
2ronwalf
2ronwalf public void setIndent(String indent) {
2ronwalf this.indent = indent;
2ronwalf }
2ronwalf
2ronwalf public boolean getFirstLineIndent() {
2ronwalf return firstLineIndent;
2ronwalf }
2ronwalf
2ronwalf public void setFirstLineIndent(boolean indent) {
2ronwalf firstLineIndent = indent;
2ronwalf }
2ronwalf
2ronwalf public void setQNames(QNameProvider qnames) {
2ronwalf this.qnames = qnames;
2ronwalf }
2ronwalf
2ronwalf public QNameProvider getQNames() {
2ronwalf return qnames;
2ronwalf }
2ronwalf
2ronwalf public void write(Expression expr, Writer out) {
2ronwalf if( out instanceof PrintWriter )
2ronwalf this.out = (PrintWriter) out;
2ronwalf else
2ronwalf this.out = new PrintWriter( out );
2ronwalf
2ronwalf boolean noQNameProvider = ( qnames == null );
2ronwalf if( noQNameProvider )
2ronwalf qnames = expr.getKB().getQNames();
2ronwalf
2ronwalf write( expr );
2ronwalf
2ronwalf if( noQNameProvider )
2ronwalf qnames = null;
2ronwalf }
2ronwalf
2ronwalf public void write(Expression expr, OutputStream out) {
2ronwalf write( expr, new PrintWriter( out ) );
2ronwalf }
2ronwalf
2ronwalf public void write( Expression expr ) {
2ronwalf write( expr.getBody() );
2ronwalf }
2ronwalf
2ronwalf public abstract void write( AtomList atoms );
2ronwalf
2ronwalf public void write( Atom atom ) {
2ronwalf if(atom instanceof ClassAtom)
2ronwalf write( (ClassAtom) atom );
2ronwalf else if(atom instanceof IndividualPropertyAtom)
2ronwalf write( (IndividualPropertyAtom) atom );
2ronwalf else if(atom instanceof DataPropertyAtom)
2ronwalf write( (DataPropertyAtom) atom );
2ronwalf else if(atom instanceof SameIndividualAtom)
2ronwalf write( (SameIndividualAtom) atom );
2ronwalf else if(atom instanceof DifferentIndividualsAtom)
2ronwalf write( (DifferentIndividualsAtom) atom );
2ronwalf else if(atom instanceof BuiltinAtom)
2ronwalf write( (BuiltinAtom) atom );
2ronwalf }
2ronwalf
2ronwalf public abstract void write( ClassAtom atom );
2ronwalf
2ronwalf public abstract void write( IndividualPropertyAtom atom );
2ronwalf
2ronwalf public abstract void write( DataPropertyAtom atom );
2ronwalf
2ronwalf public abstract void write( SameIndividualAtom atom );
2ronwalf
2ronwalf public abstract void write( DifferentIndividualsAtom atom );
2ronwalf
2ronwalf public abstract void write( BuiltinAtom atom );
2ronwalf
2ronwalf public void print( OWLValue value ) {
2ronwalf try {
2ronwalf if( value instanceof OWLIndividual ) {
2ronwalf OWLIndividual ind = (OWLIndividual) value;
2ronwalf if( ind.isAnon() )
2ronwalf out.print( "<< Anonymous Individual >>" );
2ronwalf else
2ronwalf print( ind.getURI() );
2ronwalf }
2ronwalf else
2ronwalf out.print( value );
2ronwalf }
2ronwalf catch( RuntimeException e ) {
2ronwalf out.print( "<< Invalid URI >>" );
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf public void print( OWLClass cls ) {
2ronwalf if( cls.isAnon() )
2ronwalf out.print( "<< Anonymous Class >>" );
2ronwalf else
2ronwalf print( cls.getURI() );
2ronwalf }
2ronwalf
2ronwalf public void print( OWLProperty prop ) {
2ronwalf print( prop.getURI() );
2ronwalf }
2ronwalf
2ronwalf public void print( URI uri ) {
2ronwalf out.print( qnames.shortForm( uri ) );
2ronwalf }
2ronwalf
2ronwalf}