Utils.java
001 /*
002  *  Utils.java
003  *
004  *  Niraj Aswani, 09/March/07
005  *
006  *  $Id: Utils.html,v 1.0 2007/03/09 16:13:01 niraj Exp $
007  */
008 package gate.gui.ontology;
009 
010 import gate.creole.ontology.AllValuesFromRestriction;
011 import gate.creole.ontology.AnnotationProperty;
012 import gate.creole.ontology.CardinalityRestriction;
013 import gate.creole.ontology.DatatypeProperty;
014 import gate.creole.ontology.HasValueRestriction;
015 import gate.creole.ontology.Literal;
016 import gate.creole.ontology.MaxCardinalityRestriction;
017 import gate.creole.ontology.MinCardinalityRestriction;
018 import gate.creole.ontology.OResource;
019 import gate.creole.ontology.RDFProperty;
020 import gate.creole.ontology.Restriction;
021 import gate.creole.ontology.SomeValuesFromRestriction;
022 
023 import java.util.ArrayList;
024 import java.util.List;
025 import java.util.Set;
026 import java.util.regex.Pattern;
027 
028 /**
029  * This class provides various static utility methods which are used by
030  * the ontology editor.
031  
032  @author niraj
033  
034  */
035 public class Utils {
036 
037   /**
038    * Checks whether the provided name space is valid name space. In this
039    * version, the namespace must match the following java regular
040    * expression. <BR>
041    
042    * "[a-zA-Z]+(:)(/)+[a-zA-Z0-9\\-]+((\\.)[a-zA-Z0-9\\-]+)+((/)[a-zA-Z0-9\\.\\-_]+)*(#|/)"
043    
044    @param s
045    @return
046    */
047   public static boolean isValidNameSpace(String s) {
048     String s1 = new String(
049             "[a-zA-Z]+(:)(/)+[a-zA-Z0-9\\-]+((\\.)[a-zA-Z0-9\\-]+)+((/)[a-zA-Z0-9\\.\\-_]+)*(#|/|/#)");
050     Pattern pattern = Pattern.compile(s1);
051     return pattern.matcher(s).matches();
052   }
053 
054   /**
055    * Checks whether the provided resource name is a valid resource name
056    * In this version, the resource name must match the following java
057    * regular expression <BR>. "[a-zA-Z0-9_-]+"
058    
059    @param s
060    @return
061    */
062   public static boolean isValidOntologyResourceName(String s) {
063     String s1 = new String("[a-zA-Z0-9_-]+");
064     Pattern pattern = Pattern.compile(s1);
065     return pattern.matcher(s).matches();
066   }
067 
068   /**
069    * This method returns the details to be added.
070    
071    @param object
072    @return
073    */
074   public static List getDetailsToAdd(Object object) {
075     ArrayList<Object> toAdd = new ArrayList<Object>();
076     toAdd.add(object);
077     if(object instanceof Restriction) {
078       Restriction res = (Restriction)object;
079       toAdd.add(new KeyValuePair(res, "RESTRICTION TYPE",
080               gate.creole.ontology.Utils.getRestrictionName(res)false));
081       toAdd.add(new KeyValuePair(res.getOnPropertyValue()"ON PROPERTY", res
082               .getOnPropertyValue().getName()false));
083       String valueString = null;
084       String datatypeString = null;
085 
086       if(res instanceof CardinalityRestriction) {
087         valueString = ((CardinalityRestriction)res).getValue();
088         datatypeString = ((CardinalityRestriction)res).getDataType()
089                 .getXmlSchemaURIString();
090         toAdd.add(new KeyValuePair(res, "DATATYPE", datatypeString, false));
091         toAdd.add(new KeyValuePair(res, "VALUE", valueString, true));
092       }
093       else if(res instanceof MinCardinalityRestriction) {
094         valueString = ((MinCardinalityRestriction)res).getValue();
095         datatypeString = ((MinCardinalityRestriction)res).getDataType()
096                 .getXmlSchemaURIString();
097         toAdd.add(new KeyValuePair(res, "DATATYPE", datatypeString, false));
098         toAdd.add(new KeyValuePair(res, "VALUE", valueString, true));
099       }
100       else if(res instanceof MaxCardinalityRestriction) {
101         valueString = ((MaxCardinalityRestriction)res).getValue();
102         datatypeString = ((MaxCardinalityRestriction)res).getDataType()
103                 .getXmlSchemaURIString();
104         toAdd.add(new KeyValuePair(res, "DATATYPE", datatypeString, false));
105         toAdd.add(new KeyValuePair(res, "VALUE", valueString, false));
106       }
107       else if(res instanceof HasValueRestriction) {
108         Object value = ((HasValueRestriction)res).getHasValue()
109         if(value instanceof Literal) {
110           valueString = ((Literal)value).getValue();
111           datatypeString = ((DatatypeProperty)((HasValueRestriction)res)
112               .getOnPropertyValue()).getDataType().getXmlSchemaURIString();
113           toAdd.add(new KeyValuePair(res, "DATATYPE", datatypeString, false));
114           toAdd.add(new KeyValuePair(res, "VALUE", valueString, true));
115         else {
116           valueString = ((OResource)value).getURI().toString();
117           toAdd.add(new KeyValuePair((OResource)value, "VALUE", valueString, false));
118         }
119       }
120       else if(res instanceof AllValuesFromRestriction) {
121         valueString = ((AllValuesFromRestriction)res).getHasValue().getURI()
122                 .toString();
123         toAdd.add(new KeyValuePair(((AllValuesFromRestriction)res)
124                 .getHasValue()"VALUE", valueString, false));
125       }
126       else if(res instanceof SomeValuesFromRestriction) {
127         valueString = ((SomeValuesFromRestriction)res).getHasValue().getURI()
128                 .toString();
129         toAdd.add(new KeyValuePair(((SomeValuesFromRestriction)res)
130                 .getHasValue()"VALUE", valueString, false));
131       }
132     }
133     else if(object instanceof RDFProperty) {
134       RDFProperty prop = (RDFProperty)object;
135       if(prop instanceof DatatypeProperty) {
136         toAdd.add(new KeyValuePair(prop, "DATATYPE"((DatatypeProperty)prop)
137                 .getDataType().getXmlSchemaURIString()false));
138       }
139       else if(!(prop instanceof AnnotationProperty)) {
140         Set<OResource> set = prop.getRange();
141         if(set == null || set.isEmpty()) {
142           toAdd.add(new KeyValuePair(prop, "RANGE""[ALL CLASSES]"false));
143         }
144         else {
145           String key = "RANGE";
146           for(OResource res : set) {
147             toAdd.add(new KeyValuePair(res, key, res.getName()false));
148             key = "";
149           }
150         }
151       }
152     }
153     return toAdd;
154   }
155 }