001    // This file is part of the Attempto Java Packages.
002    // Copyright 2008, 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.preditor.text;
016    
017    /**
018     * This class represents a text element that is a reference to a preceding text element.
019     * 
020     * @see RefableTextElement
021     * @author Tobias Kuhn
022     */
023    public class RefTextElement implements TextElement {
024            
025            private RefableTextElement refableTextElement;
026            private String category;
027            private boolean sentenceInitial = false;
028            
029            /**
030             * Creates a new reference text element that refers to the given referenceable text element.
031             * 
032             * @param refableTextElement The referenceable text element occurring earlier in the text.
033             * @param category The category of the reference text element.
034             */
035            public RefTextElement(RefableTextElement refableTextElement, String category) {
036                    this.refableTextElement = refableTextElement;
037                    this.category = category;
038            }
039    
040            public String getText() {
041                    String t = refableTextElement.getReferenceText();
042                    if (sentenceInitial) {
043                            return t.substring(0, 1).toUpperCase() + t.substring(1);
044                    } else {
045                            return t;
046                    }
047            }
048    
049            public String getCategory() {
050                    return category;
051            }
052    
053            public void checkNeighborTextElements(TextElement precedingTextElement, TextElement nextTextElement) {
054                    sentenceInitial = (precedingTextElement == null || precedingTextElement.getText().matches("(\\.|\\?|\\!)"));
055            }
056            
057            public boolean equals(Object obj) {
058                    if (obj instanceof RefTextElement) {
059                            RefTextElement other = (RefTextElement) obj;
060                            return (this.refableTextElement == other.refableTextElement);
061                    }
062                    return false;
063            }
064    
065    }