1 19 package org.openharmonise.rm.resources.metadata.properties.ranges; 20 21 import java.util.StringTokenizer ; 22 23 import org.openharmonise.rm.PopulateException; 24 import org.openharmonise.rm.metadata.GeneralPropertyInstance; 25 import org.openharmonise.rm.publishing.*; 26 import org.w3c.dom.Element ; 27 28 29 38 public class StringRange extends AbstractRange implements Range, Publishable { 39 40 43 public final static String TAG_STRING_RANGE = "StringRange"; 44 45 48 public final static String TAG_MIN_LENGTH = "MinLength"; 49 50 53 public final static String TAG_MAX_LENGTH = "MaxLength"; 54 55 56 59 private int m_minLength = 0; 60 61 64 private int m_maxLength = -1; 65 66 70 static final private String SEPARATOR = ":"; 71 72 76 public StringRange() { 77 super(String .class.getName()); 78 } 79 80 87 public StringRange(String sDetails) { 88 super(String .class.getName(), sDetails); 89 } 90 91 94 public boolean isValid(Object obj) { 95 boolean bIsValid = false; 96 97 if (obj instanceof String ) { 98 bIsValid = true; 99 String strObj = (String ) obj; 100 int nLength = strObj.length(); 101 102 if (m_minLength > 0) { 103 bIsValid = !(nLength <= m_minLength); 104 } 105 106 if (m_maxLength > 0 && bIsValid == true) { 107 bIsValid = !(nLength >= m_maxLength); 108 } 109 } 110 111 return bIsValid; 112 } 113 114 120 public void setMinLength(int nMinLength) throws InvalidRangeRestrictionException { 121 if(nMinLength < 0) { 123 if(m_minLength > 0) { 124 m_minLength = 0; 125 isChanged(true); 126 } 127 } else { 128 if(m_maxLength > 0 && nMinLength > m_maxLength) { 129 throw new InvalidRangeRestrictionException(nMinLength + " is not a valid minumum"); 130 } 131 132 if (m_minLength != nMinLength) { 133 isChanged(true); 134 m_minLength = nMinLength; 135 } 136 } 137 138 } 139 140 145 public int getMinLength() { 146 return m_minLength; 147 } 148 149 155 public void setMaxLength(int nMaxLength) throws InvalidRangeRestrictionException { 156 if(nMaxLength > 2000 || nMaxLength < m_minLength) { 157 throw new InvalidRangeRestrictionException(nMaxLength + " is not permitted as a maximum"); 158 } 159 if (m_maxLength != nMaxLength) { 160 isChanged(true); 161 } 162 m_maxLength = nMaxLength; 163 } 164 165 170 public int getMaxLength() { 171 return m_maxLength; 172 } 173 174 177 public String getDetails() { 178 179 if (isChanged() == true) { 180 StringBuffer strbuf = new StringBuffer (); 181 strbuf.append(String.valueOf(m_minLength)).append( 182 SEPARATOR).append( 183 m_maxLength); 184 super.setDetails(strbuf.toString()); 185 } 186 187 return super.getDetails(); 188 } 189 190 193 public void setDetails(String sDetails) { 194 if (sDetails != null) { 195 StringTokenizer tokenizer = 196 new StringTokenizer (sDetails, SEPARATOR); 197 198 int i = 0; 199 200 while (tokenizer.hasMoreTokens()) { 201 String token = tokenizer.nextToken(); 202 203 int nTmp = Integer.parseInt(token); 204 205 if (i == 0) { 206 m_minLength = nTmp; 207 } else if (i == 1) { 208 m_maxLength = nTmp; 209 } 210 i++; 211 } 212 } 213 214 super.setDetails(sDetails); 215 216 } 217 218 221 public boolean equals(Object obj) { 222 boolean bResult = false; 223 224 if (obj instanceof StringRange) { 225 if (super.equals(obj) == true) { 226 StringRange sRange = (StringRange) obj; 227 228 if (m_minLength == sRange.getMinLength() 229 && m_maxLength == sRange.getMaxLength()) { 230 bResult = true; 231 } 232 } 233 } 234 235 return bResult; 236 } 237 238 241 public Class getPropertyInstanceClass() throws ClassNotFoundException { 242 return GeneralPropertyInstance.class; 243 } 244 245 248 public String getTagName() { 249 return TAG_STRING_RANGE; 250 } 251 252 253 254 261 private int getChildIntValue(Element el) { 262 int nVal = 0; 263 264 String sVal = el.getFirstChild().getNodeValue(); 265 266 if(sVal != null) { 267 nVal = Integer.parseInt(sVal); 268 } 269 270 return nVal; 271 } 272 273 276 279 public void populate(Element xmlElement, State state) 280 throws PopulateException { 281 282 String sTagname = xmlElement.getTagName(); 283 284 try { 285 if(sTagname.equals(TAG_MIN_LENGTH)) { 286 setMinLength(getChildIntValue(xmlElement)); 287 } else if(sTagname.equals(TAG_MAX_LENGTH)) { 288 setMaxLength(getChildIntValue(xmlElement)); 289 } else { 290 super.populate(xmlElement, state); 291 } 292 } catch (InvalidRangeRestrictionException e) { 293 throw new PopulateException(e); 294 } catch (PopulateException e) { 295 throw new PopulateException(e); 296 } 297 } 298 299 } 300 | Popular Tags |