001    // This file is part of the Attempto Java Packages.
002    // Copyright 2008-2009, Attempto Group, University of Zurich (see http://attempto.ifi.uzh.ch).
003    //
004    // The Attempto Java Packages is free software: you can redistribute it and/or modify it under the
005    // terms of the GNU Lesser General Public License as published by the Free Software Foundation,
006    // either version 3 of the License, or (at your option) any later version.
007    //
008    // The Attempto Java Packages is distributed in the hope that it will be useful, but WITHOUT ANY
009    // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
010    // PURPOSE. See the GNU Lesser General Public License for more details.
011    //
012    // You should have received a copy of the GNU Lesser General Public License along with the Attempto
013    // Java Packages. If not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.acewiki.core.ontology;
016    
017    /**
018     * This class represents a statement that can be either an ACE sentence or a comment. A
019     * statement can either have an ontology element as its owner (in this case it occurs on
020     * the article of the owner) or it can be an independent statement that has no owner.
021     * 
022     * @author Tobias Kuhn
023     */
024    public abstract class Statement {
025            
026            private Ontology ontology;
027            private OntologyElement owner;
028            
029            /**
030             * Initializes a new independent statement.
031             * 
032             * @param ontology The ontology of the new statement.
033             */
034            protected Statement(Ontology ontology) {
035                    this.ontology = ontology;
036                    
037            }
038            
039            /**
040             * Initializes a new statement with the given ontology element as its owner.
041             * 
042             * @param owner The ontology element that is the owner of the new statement.
043             */
044            protected Statement(OntologyElement owner) {
045                    this.owner = owner;
046                    this.ontology = owner.getOntology();
047            }
048            
049            /**
050             * Loads a statement from a serialized form.
051             * 
052             * @param serializedStatement The serialized statement as a string.
053             * @param owner The owner ontology element of the statement.
054             * @return The new statement object.
055             */
056            static Statement loadStatement(String serializedStatement, OntologyElement owner) {
057                    Sentence sentence;
058                    switch (serializedStatement.charAt(0)) {
059                    case '|':
060                            sentence = new Sentence(serializedStatement.substring(2), owner);
061                            sentence.setIntegrated(true);
062                            return sentence;
063                    case '#':
064                            sentence = new Sentence(serializedStatement.substring(2), owner);
065                            sentence.setIntegrated(false);
066                            return sentence;
067                    case 'c':
068                            return Comment.load(serializedStatement.substring(2), owner);
069                    }
070                    return null;
071            }
072            
073            /**
074             * This method returns the ontology this statement belongs to.
075             * 
076             * @return The ontology.
077             */
078            public Ontology getOntology() {
079                    if (ontology == null) {
080                            ontology = owner.getOntology();
081                    }
082                    return ontology;
083            }
084            
085            /**
086             * This method returns the owner ontology element of this statement.
087             * 
088             * @return The owner ontology element.
089             */
090            public OntologyElement getOwner() {
091                    return owner;
092            }
093            
094            /**
095             * This method returns the text of this statement.
096             * 
097             * @return The text.
098             */
099            public abstract String getText();
100            
101            /**
102             * This method returns a serialization of the statement.
103             * 
104             * @return The serialized representation of the statement.
105             */
106            abstract String serialize();
107            
108            public String toString() {
109                    return getText();
110            }
111    
112    }