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 java.util.Collections;
018    import java.util.Comparator;
019    import java.util.List;
020    
021    /**
022     * This utility class contains static methods for general tasks on the language level.
023     * 
024     * @author Tobias Kuhn
025     */
026    public class LanguageUtils {
027            
028            private static OntologyElementsComparator comparator = new OntologyElementsComparator();
029            
030            // no instances allowed:
031            private LanguageUtils() {}
032    
033            /**
034             * Returns the pretty-printed form of the given text. Underscores are transformed into blanks.
035             * 
036             * @param text The input text.
037             * @return The pretty-printed text;
038             */
039            public static String getPrettyPrinted(String text) {
040                    if (text == null) return null;
041                    return text.replace("_", " ");
042            }
043            
044            /**
045             * Returns a heading of the form "headword1 (headword2, ..., headwordn)" for the given ontology
046             * element.
047             * 
048             * @param oe The ontology element.
049             * @return The heading.
050             */
051            public static String getHeading(OntologyElement oe) {
052                    String[] h = oe.getHeadwords();
053                    String heading = getPrettyPrinted(h[0]);
054                    if (h.length > 1) {
055                            String aliases = "";
056                            for (int i=1 ; i<h.length ; i++) {
057                                    aliases += ", " + getPrettyPrinted(h[i]);
058                            }
059                            if (aliases.length() > 0) {
060                                    aliases = aliases.substring(2);
061                            }
062                            heading += " (" + aliases + ")";
063                    }
064                    return heading;
065            }
066            
067            /**
068             * Sorts the list of ontology elements according to their main headword.
069             * 
070             * @param elements The list to be sorted.
071             */
072            public static void sortOntologyElements(List<? extends OntologyElement> elements) {
073                    Collections.sort(elements, comparator);
074            }
075            
076            
077            private static class OntologyElementsComparator implements Comparator<OntologyElement> {
078    
079                    public int compare(OntologyElement o1, OntologyElement o2) {
080                            if (o1 instanceof DummyOntologyElement && !(o2 instanceof DummyOntologyElement)) {
081                                    return -1;
082                            } else if (!(o1 instanceof DummyOntologyElement) && o2 instanceof DummyOntologyElement) {
083                                    return 1;
084                            } else {
085                                    return o1.getHeadwords()[0].compareToIgnoreCase(o2.getHeadwords()[0]);
086                            }
087                    }
088                    
089            }
090    
091    }