001    // This file is part of AceWiki.
002    // Copyright 2008-2012, AceWiki developers.
003    // 
004    // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU
005    // Lesser General Public License as published by the Free Software Foundation, either version 3 of
006    // the License, or (at your option) any later version.
007    // 
008    // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009    // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010    // Lesser General Public License for more details.
011    // 
012    // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If
013    // not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.acewiki.core;
016    
017    /**
018     * This is a partial implementation of an ontology element.
019     * 
020     * @author Tobias Kuhn
021     */
022    public abstract class AbstractOntologyElement implements OntologyElement {
023            
024            private Ontology ontology;
025            private Article article;
026            
027            private long id = -1;
028            
029            public void initId(long id) {
030                    this.id = id;
031            }
032            
033            public void initOntology(Ontology ontology) {
034                    if (this.ontology != null && this.ontology != ontology) {
035                            throw new RuntimeException("Cannot change the ontology for element " + toString());
036                    }
037                    this.ontology = ontology;
038            }
039            
040            public void initArticle(Article article) {
041                    this.article = article;
042            }
043            
044            public String getWord(int n) {
045                    return getWords()[n];
046            }
047            
048            public String getWord() {
049                    return getWord(0);
050            }
051            
052            public String[] getHeadwords() {
053                    return new String[] {getWord(0)};
054            }
055            
056            public long getId() {
057                    return id;
058            }
059            
060            public Ontology getOntology() {
061                    return ontology;
062            }
063            
064            public Article getArticle() {
065                    if (article == null) {
066                            article = new Article(this);
067                    }
068                    return article;
069            }
070            
071            /**
072             * Writes the text to the log file.
073             * 
074             * @param text The text to be written to the log file.
075             */
076            protected void log(String text) {
077                    if (ontology != null) {
078                            ontology.log(text);
079                    }
080            }
081            
082            public String toString() {
083                    return getInternalType() + ":" + serializeWords();
084            }
085    
086    }