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