View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3    
4    Copyright (c) 2004-2005, OntoText Lab. / SIRMA
5    
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  
19  package com.ontotext.wsmo4j.ontology;
20  
21  /**
22   * <p>Title: WSMO4J</p>
23   * <p>Description: WSMO API and a Reference Implementation</p>
24   * <p>Copyright:  Copyright (c) 2004-2005</p>
25   * <p>Company: OntoText Lab. / SIRMA </p>
26   * @author not attributable
27   * @version 1.0
28   *
29   */
30  
31  import org.omwg.logicalexpression.terms.Visitor;
32  import org.omwg.ontology.Variable;
33  
34  public class VariableImpl implements Variable {
35  
36      private String name;
37      
38      public VariableImpl(String name) throws IllegalArgumentException {
39          if (name == null) {
40              throw new IllegalArgumentException("Variable name can not be null");
41          }
42          if (name.startsWith("?")) {
43              this.name = name.substring(1);
44          }
45          else {
46              this.name = name;
47          }
48          
49  		if (!isValidName(this.name)){
50              throw new IllegalArgumentException("Variable name ("+name+") may only contain alphanumeric characters");
51  		}
52          
53      }
54      
55      public String getName() {
56          return name;
57      }
58      
59      public boolean equals(Object object) {
60          if (object == this) {
61              return true;
62          }
63          if (object == null
64                  || false == object instanceof Variable) {
65              return false;
66          }
67          return name.equals(((Variable)object).getName());
68      }
69      
70      public int hashCode() {
71          return this.name.hashCode();
72      }
73      
74      public String toString() {
75          return "?" + this.name;
76      }
77      
78      public void accept(Visitor v) {
79      	v.visitVariable(this);
80      }
81      
82      /**
83       * Checks if the given name is a valid name. This check is made
84       * character by character.
85       * @param name
86       * @return <code>true</code> if the name is valid or <code>false</code> if not
87       * @see #isValidNameChar(char)
88       */
89      private boolean isValidName(String name) {
90          for (int i = 0; i < name.length(); i++) {
91              if (!isValidNameChar(name.charAt(i))) {
92                  return false;
93              }
94          }
95          return true;
96  
97      }
98      
99      /**
100      * Checks the validity of the given character. 
101      * @param chr character of a String name
102      * @return <code>true</code> if the given character is valid, <code>false</code> otherwise
103      * @see #isValidName(String)
104      */
105     private boolean isValidNameChar(char chr) {
106         return // basechar
107                 (chr >= '\u0041' && chr <= '\u005A') || 
108                 (chr >= '\u0061' && chr <= '\u007A') ||
109                // ideographic
110                 (chr >= '\u4E00' && chr <= '\u9FA5') || 
111                 (chr == '\u3007') || 
112                 (chr >= '\u3021' && chr <= '\u3029') ||
113                 //digit	
114                 (chr >= '\u0030' && chr <= '\u0039');
115     }
116 }
117 
118 /*
119  * $Log$
120  * Revision 1.7  2006/11/21 15:45:20  vassil_momtchev
121  * isValid check was not applied over the correct variable
122  *
123  * Revision 1.6  2005/09/02 15:19:16  alex_simov
124  * bugfix: inverted if condition in constructor
125  *
126  * Revision 1.5  2005/09/02 14:20:56  holgerlausen
127  * throws illegal argument exception now
128  *
129  * Revision 1.4  2005/09/02 13:32:45  ohamano
130  * move logicalexpression packages from ext to core
131  * move tests from logicalexpression.test to test module
132  *
133  * Revision 1.3  2005/09/02 09:43:32  ohamano
134  * integrate wsmo-api and le-api on api and reference implementation level; full parser, serializer and validator integration will be next step
135  *
136  * Revision 1.2  2005/08/31 08:36:14  alex_simov
137  * bugfix: Variable.getName() now returns the name without leading '?'
138  *
139  * Revision 1.1  2005/06/01 12:09:05  marin_dimitrov
140  * v0.4.0
141  *
142  * Revision 1.1  2005/05/12 15:07:25  alex
143  * initial commit
144  *
145  *
146  */