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 import ch.uzh.ifi.attempto.base.TextElement; 018 019 /** 020 * This class represents a text element that links to an ontology element. The text of 021 * the text elements corresponds to one of the word forms of the ontology element. 022 * 023 * @author Tobias Kuhn 024 */ 025 // TODO Get rid of pre and post texts. 026 public class OntologyTextElement extends TextElement { 027 028 private OntologyElement ontologyElement; 029 private int wordNumber; 030 private String pre = ""; 031 private String post = ""; 032 033 /** 034 * Creates a new ontology text element. 035 * 036 * @param element The ontology element. 037 * @param wordNumber The word number. 038 */ 039 public OntologyTextElement(OntologyElement element, int wordNumber) { 040 if (element.getWord(wordNumber) == null) { 041 throw new RuntimeException(element + " has no word number " + wordNumber); 042 } 043 this.ontologyElement = element; 044 this.wordNumber = wordNumber; 045 } 046 047 /** 048 * Returns the text of this text element in its plain form where underscores are not 049 * replaces by blanks. 050 * 051 * @return The plain text. 052 */ 053 public String getUnderscoredText() { 054 return super.getText(); 055 } 056 057 public String getText() { 058 String t = super.getText(); 059 if (t == null) return null; 060 return LanguageUtils.getPrettyPrinted(t); 061 } 062 063 public String getOriginalText() { 064 return pre + ontologyElement.getWord(wordNumber) + post; 065 } 066 067 /** 068 * This method adds a text to the front of the word of the ontology element. 069 * 070 * @param pre The text to be added to the front. 071 */ 072 public void setPreText(String pre) { 073 if (pre != null) this.pre = pre; 074 } 075 076 /** 077 * Returns the text added to the front. 078 * 079 * @return The text to be added to the front. 080 */ 081 public String getPreText() { 082 return pre; 083 } 084 085 /** 086 * This method adds a text to the end of the word of the ontology element. 087 * 088 * @param post The text to be added to the end. 089 */ 090 public void setPostText(String post) { 091 if (post != null) this.post = post; 092 } 093 094 /** 095 * Returns the text added to the end. 096 * 097 * @return The text to be added to the end. 098 */ 099 public String getPostText() { 100 return post; 101 } 102 103 /** 104 * Returns the id of the word form of the ontology element that is used for this 105 * text element. 106 * 107 * @return The word form id. 108 */ 109 public int getWordNumber() { 110 return wordNumber; 111 } 112 113 /** 114 * Returns the ontology element to which this text element is linked. 115 * 116 * @return The ontology element. 117 */ 118 public OntologyElement getOntologyElement() { 119 return ontologyElement; 120 } 121 122 public boolean equals(Object obj) { 123 if (obj instanceof OntologyTextElement) { 124 OntologyTextElement other = ((OntologyTextElement) obj); 125 return ( 126 ontologyElement == other.ontologyElement && 127 wordNumber == other.wordNumber 128 ); 129 } 130 return false; 131 } 132 133 }