001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.sms;
031
032//import org.apache.commons.logging.Log;
033//import org.apache.commons.logging.LogFactory;
034import java.util.ArrayList;
035import java.util.Arrays;
036import java.util.Iterator;
037import java.util.List;
038import java.util.regex.Matcher;
039import java.util.regex.Pattern;
040
041import org.semanticweb.owl.model.OWLOntology;
042
043/**
044 * Created by IntelliJ IDEA.
045 * User: sean
046 * Date: May 26, 2009
047 * Time: 1:14:05 PM
048 */
049public class Util {
050        public static String join(Object[] strings, String delimiter) {
051                if (strings.length == 0) {
052                        return "";
053                }
054                StringBuilder builder = new StringBuilder(strings[0].toString());
055                for (int i = 1; i < strings.length; i++) {
056                        builder.append(delimiter).append(strings[i].toString());
057                }
058                return builder.toString();
059        }
060        
061        public static String join(List strings, String delimiter) {
062                return join(strings.toArray(new Object[strings.size()]), delimiter);
063        }
064        
065        public static List<String> splitTags(String rawTags) {
066                rawTags = rawTags.trim();
067                String[] tags = rawTags.split(",");
068                List<String> currentTags = new ArrayList<String>();
069                for (String tag : tags) {
070                        String effectiveTag = tag.replace(",", "").trim();
071                        if (!"".equals(effectiveTag)) {
072                                currentTags.add(effectiveTag);
073                        }
074                }
075                
076                if ("".equals(rawTags)) {
077                        return Arrays.asList((String[]) new String[] {""});
078                }
079                // Bit of a hack
080                if (rawTags.charAt(rawTags.length() - 1) == ',') {
081                        currentTags.add("");
082                }
083                                
084                return currentTags;
085        }
086        
087        public static String truncate(String string, int length) {
088                try {
089                        return string.substring(0, length) + "...";
090                }
091                catch(StringIndexOutOfBoundsException ex) {
092                        return string;
093                }
094        }       
095        
096        private static int countSubstring(String string, String substring) {
097                int searchFrom = -1;
098                int count = 0;
099                do {
100                        searchFrom = string.indexOf(substring, searchFrom+1);
101                        if (searchFrom != -1) {
102                                count++;
103                        }
104                }
105                while (searchFrom != -1);               
106                return count;
107        }
108        
109        protected static Pattern GET_STRING_FROM_OWL_SERIALIZATION = Pattern.compile("\"(.*)\"\\^\\^string");
110        
111        public static String parseOWLSerializedString(String string) {
112                Matcher matcher = GET_STRING_FROM_OWL_SERIALIZATION.matcher(string);
113                if (matcher.matches()) {
114                        return matcher.group(1);
115                }
116                return string;
117        }
118        
119        public static String format(List<?> tags) {
120                return join(tags, ",");
121        }
122        
123//      private static final Log log = LogFactory.getLog(Util.class);
124
125        public static String[] tagSplit(String rawTags) {
126                // The main reason we can't use String.split() is that if the
127                // delimiter is the end of the string, we don't get an empty string
128                // as one of the pieces at the end.
129                // 
130                // For example, "one,two,".split(",") -> {"one", "two"} and not
131                // {"one", "two", ""}, as I would like. For the moment, we just cover
132                // this case of the trailing empty tag, to solve the problem of the
133                // 'Add new tag' dropdown option not going away after it is selected.
134                
135                String[] tags = rawTags.split(",");
136                if ("".equals(rawTags)) return new String[] {""};
137                if (rawTags.charAt(rawTags.length() - 1) == ',') {
138                        String[] newTags = new String[tags.length + 1];
139                        System.arraycopy(tags, 0, newTags, 0, tags.length);
140                        newTags[newTags.length - 1] = "";
141                        return newTags;
142                }
143                
144                return tags;
145        }
146
147        public static String getOntologyLabel(OWLOntology ontology) {
148                OntologyCatalog catalog = OntologyCatalog.instance();
149                Iterator<NamedOntModel> iterator = catalog.getNamedOntModels();
150                while (iterator.hasNext()) {
151                        NamedOntModel model = iterator.next();
152                        if (model.getOntology().equals(ontology)) {
153                                return model.getName();
154                        }
155                }
156                
157                return "(ontology)";
158        }
159}