1 19 package org.openharmonise.vfs.metadata.range; 20 21 import org.openharmonise.commons.xml.*; 22 import org.openharmonise.vfs.metadata.*; 23 import org.openharmonise.vfs.metadata.value.*; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.Node ; 26 import org.w3c.dom.NodeList ; 27 import org.w3c.dom.Text ; 28 29 30 37 public class StringRange extends AbstractRange implements Range { 38 39 42 public static int SIZE_BORDER = 100; 43 44 47 private Integer m_nMinLength = null; 48 49 52 private Integer m_nMaxLength = new Integer (SIZE_BORDER); 53 54 57 public StringRange() { 58 super(); 59 } 60 61 64 public ValidationResult validate(ValueInstance value) { 65 boolean bIsValid = true; 66 int nMax = getMaxLength(); 67 int nMin = getMinLength(); 68 69 int nLen = ((StringValue)value).getValue().length(); 70 71 if(nMax > 0 && nLen > nMax) { 72 bIsValid = false; 73 } 74 75 if(bIsValid == true && nMin > 0 && nLen < nMin) { 76 bIsValid = false; 77 } 78 79 return new ValidationResult(bIsValid,""); 80 } 81 82 87 public int getMinLength() { 88 if(this.m_nMinLength==null) { 89 return 0; 90 } else { 91 return this.m_nMinLength.intValue(); 92 } 93 } 94 95 100 public int getMaxLength() { 101 if(this.m_nMaxLength==null) { 102 return -1; 103 } else { 104 return this.m_nMaxLength.intValue(); 105 } 106 } 107 108 113 public void setMinLength(int nMin) { 114 this.m_nMinLength = new Integer (nMin); 115 } 116 117 122 public void setMaxLength(int nMax) { 123 this.m_nMaxLength = new Integer (nMax); 124 } 125 126 129 public void instantiate(Element elRange) { 130 Element elRestriction = XMLUtils.getFirstElementChild(elRange); 131 NodeList nl = elRestriction.getChildNodes(); 132 for(int i=0; i<nl.getLength();i++) { 133 Node node = nl.item(i); 134 if(node.getNodeType()==Node.ELEMENT_NODE) { 135 Element element = (Element )node; 136 if(element.getLocalName().equalsIgnoreCase("minLength")) { 137 Node node2 = element.getFirstChild(); 138 if( node2.getNodeType()==Node.TEXT_NODE) { 139 this.m_nMinLength = Integer.valueOf( ((Text )node2).getNodeValue() ); 140 } 141 } else if(element.getLocalName().equalsIgnoreCase("maxLength") || element.getLocalName().equalsIgnoreCase("length")) { 142 Node node2 = element.getFirstChild(); 143 if( node2.getNodeType()==Node.TEXT_NODE) { 144 this.m_nMaxLength = Integer.valueOf( ((Text )node2).getNodeValue() ); 145 } 146 } 147 } 148 } 149 } 150 151 public String toString() { 152 StringBuffer sBuff = new StringBuffer (); 153 154 sBuff.append("StringRange:\n"); 155 if(this.m_nMinLength!=null) { 156 sBuff.append("minLength: ").append(this.m_nMinLength).append("\n"); 157 } 158 if(this.m_nMaxLength!=null) { 159 sBuff.append("maxLength: ").append(this.m_nMaxLength).append("\n"); 160 } 161 162 return sBuff.toString(); 163 } 164 165 } 166 | Popular Tags |