001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.dllearner.utilities.owl;
020
021import org.semanticweb.owlapi.io.OWLObjectRenderer;
022import org.semanticweb.owlapi.model.OWLObject;
023import org.semanticweb.owlapi.util.ShortFormProvider;
024import org.semanticweb.owlapi.util.SimpleShortFormProvider;
025
026import java.io.IOException;
027import java.io.StringWriter;
028import java.io.Writer;
029
030/**
031 * An implementation of the OWLObjectRenderer interface. (Renders standalone
032 * class class expressions and axioms in the manchester syntax).
033 * 
034 * @author Matthew Horridge, The University Of Manchester, Bio-Health
035 *         Informatics Group, Date: 25-Nov-2007
036 */
037public class ManchesterOWLSyntaxOWLObjectRendererImplExt implements
038        OWLObjectRenderer {
039
040    private ManchesterOWLSyntaxObjectRendererExt ren;
041    private WriterDelegate writerDelegate;
042
043    /** default constructor */
044    public ManchesterOWLSyntaxOWLObjectRendererImplExt() {
045        writerDelegate = new WriterDelegate();
046        ren = new ManchesterOWLSyntaxObjectRendererExt(writerDelegate,
047                new SimpleShortFormProvider());
048    }
049    
050    /** default constructor */
051    public ManchesterOWLSyntaxOWLObjectRendererImplExt(boolean useTabbing, boolean useWrapping) {
052        writerDelegate = new WriterDelegate();
053        ren = new ManchesterOWLSyntaxObjectRendererExt(writerDelegate,
054                new SimpleShortFormProvider());
055        ren.setUseTabbing(useTabbing);
056        ren.setUseWrapping(useWrapping);
057    }
058
059    @Override
060    public synchronized String render(OWLObject object) {
061        writerDelegate.reset();
062        object.accept(ren);
063        return writerDelegate.toString();
064    }
065
066    @Override
067    public synchronized void setShortFormProvider(
068            ShortFormProvider shortFormProvider) {
069        ren = new ManchesterOWLSyntaxObjectRendererExt(writerDelegate,
070                shortFormProvider);
071    }
072    
073    /**
074     * @param useTabbing
075     *        useTabbing
076     */
077    public void setUseTabbing(boolean useTabbing) {
078        ren.setUseTabbing(useTabbing);
079    }
080
081    /**
082     * @param useWrapping
083     *        useWrapping
084     */
085    public void setUseWrapping(boolean useWrapping) {
086        ren.setUseWrapping(useWrapping);
087    }
088
089    /** @return true if use wrapping */
090    public boolean isUseWrapping() {
091        return ren.isUseWrapping();
092    }
093
094    /** @return true if use tabbing */
095    public boolean isUseTabbing() {
096        return ren.isUseWrapping();
097    }
098
099    private static class WriterDelegate extends Writer {
100
101        private StringWriter delegate;
102
103        /** default constructor */
104        public WriterDelegate() {}
105
106        protected void reset() {
107            delegate = new StringWriter();
108        }
109
110        @Override
111        public String toString() {
112            return delegate.getBuffer().toString();
113        }
114
115        @Override
116        public void close() throws IOException {
117            delegate.close();
118        }
119
120        @Override
121        public void flush() throws IOException {
122            delegate.flush();
123        }
124
125        @Override
126        public void write(char[] cbuf, int off, int len) throws IOException {
127            delegate.write(cbuf, off, len);
128        }
129    }
130}