1 19 20 package org.netbeans.modules.xml.xdm.nodes; 21 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.regex.Pattern ; 25 import org.netbeans.modules.xml.xdm.visitor.XMLNodeVisitor; 26 27 39 public class Attribute extends NodeImpl implements Node, org.w3c.dom.Attr { 40 41 Attribute() { 42 super(); 43 } 44 45 Attribute(String name) { 46 super(); 47 List <Token> tokens = getTokensForWrite(); 48 tokens.add(Token.create(" ", TokenType.TOKEN_WHITESPACE)); 49 tokens.add(Token.create(name, TokenType.TOKEN_ATTR_NAME)); 50 tokens.add(Token.create("=", TokenType.TOKEN_ATTR_EQUAL)); 51 tokens.add(Token.create("\"\"", TokenType.TOKEN_ATTR_VAL)); 52 } 53 54 Attribute(String name, String value) { 55 super(); 56 List <Token> tokens = getTokensForWrite(); 57 tokens.add(Token.create(" ", TokenType.TOKEN_WHITESPACE)); 58 tokens.add(Token.create(name, TokenType.TOKEN_ATTR_NAME)); 59 tokens.add(Token.create("=", TokenType.TOKEN_ATTR_EQUAL)); 60 tokens.add(Token.create("\"".concat(insertEntityReference(value)).concat("\""), TokenType.TOKEN_ATTR_VAL)); 61 } 62 63 public short getNodeType() { 64 return Node.ATTRIBUTE_NODE; 65 } 66 67 public String getNodeName() { 68 return getName(); 69 } 70 71 public String getNodeValue() { 72 return getValue(); 73 } 74 75 public boolean getSpecified() { 76 return false; 77 } 78 79 public boolean isId() { 80 return false; 81 } 82 83 public org.w3c.dom.Element getOwnerElement() { 84 return (org.w3c.dom.Element ) super.getParentNode(); 85 } 86 87 public org.w3c.dom.TypeInfo getSchemaTypeInfo() { 88 return null; 89 } 90 91 private void validateTokens(List <Token> newTokens) { 92 assert newTokens != null; 93 assert newTokens.size() >=3 && newTokens.size() <=6; 94 int currentIdx =0; 95 int nameIdx = -1; 96 int equalIdx = -1; 97 int valIdx = -1; 98 for (Token token :newTokens) { 99 if(token.getType() == TokenType.TOKEN_ATTR_NAME) { 100 if(nameIdx !=-1) 101 throw new IllegalArgumentException (); 102 nameIdx = currentIdx; 103 } else if (token.getType() == TokenType.TOKEN_ATTR_EQUAL) { 104 if(equalIdx !=-1 || nameIdx ==-1) 105 throw new IllegalArgumentException (); 106 equalIdx = currentIdx; 107 } else if (token.getType() == TokenType.TOKEN_ATTR_VAL) { 108 if(valIdx != -1 || equalIdx ==-1) 109 throw new IllegalArgumentException (); 110 valIdx = currentIdx; 111 } else if (token.getType() == TokenType.TOKEN_WHITESPACE) { 112 } else 113 throw new IllegalArgumentException (); 114 currentIdx++; 115 } 116 if(nameIdx == -1 || equalIdx == -1 || valIdx == -1) 117 throw new IllegalArgumentException (); 118 } 119 120 void setTokens(List <Token> newTokens) { 121 name = null; 123 value = null; 124 super.setTokens(newTokens); 125 } 126 127 public void accept(XMLNodeVisitor visitor) { 128 visitor.visit(this); 129 } 130 131 public String getLocalName() { 132 String qName = getName(); 133 if(qName != null){ 134 int idx = qName.indexOf(':')+1; 135 if(idx >0) return qName.substring(idx); 136 } 137 return qName; 138 } 139 140 public void setLocalName(String localName) { 141 String prefix = getPrefix(); 142 if(prefix == null) { 143 setName(localName); 144 } else if(localName == null || localName.equals("")) { 145 setName(prefix); 146 } else { 147 setName(prefix.concat(":").concat(localName)); 148 } 149 } 150 151 public String getPrefix() { 152 String qName = getName(); 153 if(qName != null){ 154 int idx = qName.indexOf(':'); 155 if(idx >0) return qName.substring(0,idx); 156 } 157 return null; 158 } 159 160 public void setPrefix(String prefix) { 161 String localName = getLocalName(); 162 if(prefix == null || prefix.equals("")) { 163 setName(localName); 164 } else { 165 setName(prefix.concat(":").concat(localName)); 166 } 167 } 168 169 public String getName() { 170 if(name == null) { 171 for(Token token : getTokens()) { 172 if(token.getType() == TokenType.TOKEN_ATTR_NAME) { 173 name = token.getValue(); 174 break; 175 } 176 } 177 } 178 return name; 179 } 180 181 public void setName(String name) { 182 assert name!= null && !"".equals(name); 183 checkNotInTree(); 184 this.name = name; 185 int tokenIndex = -1; 186 for(Token token : getTokens()) { 187 tokenIndex++; 188 if(token.getType() == TokenType.TOKEN_ATTR_NAME) { 189 Token newToken = Token.create(name,TokenType.TOKEN_ATTR_NAME); 190 getTokensForWrite().set(tokenIndex,newToken); 191 return; 192 } 193 } 194 } 195 196 public String getValue() { 197 if(value==null) { 198 for(Token token : getTokens()) { 199 if(token.getType() == TokenType.TOKEN_ATTR_VAL) { 200 String tokenValue = token.getValue(); 201 int len = tokenValue.length(); 202 if (len<=2) { 203 value = ""; 204 } else { 205 value = removeEntityReference(tokenValue.substring(1, len-1)); 206 } 207 } 208 } 209 } 210 return value; 211 } 212 213 public void setValue(String value) { 214 checkNotInTree(); 215 this.value = value; 216 int tokenIndex = -1; 217 for(Token token : getTokens()) { 218 tokenIndex++; 219 if(token.getType() == TokenType.TOKEN_ATTR_VAL) { 220 String oldVal = token.getValue(); 221 String newVal = oldVal.charAt(0)+insertEntityReference(value)+oldVal.charAt(oldVal.length()-1); 222 Token newToken = Token.create(newVal,TokenType.TOKEN_ATTR_VAL); 223 getTokensForWrite().set(tokenIndex,newToken); 224 return; 225 } 226 } 227 } 228 229 public boolean isXmlnsAttribute() { 230 return XMLNS.equals(getPrefix()) || XMLNS.equals(getName()); 231 } 232 233 protected void cloneNamespacePrefixes(Map <Integer ,String > allNS, Map <String ,String > prefixes) { 234 if (allNS == null) return; 235 236 String [] parts = this.getValue().split(":"); 237 String prefix = parts.length > 1 ? parts[0] : null; 238 if (prefix != null) { 239 String namespace = lookupNamespaceURI(prefix); 240 if (namespace != null) { 241 prefixes.put(prefix, namespace); 242 } 243 } 244 super.cloneNamespacePrefixes(allNS, prefixes); 245 } 246 247 private String insertEntityReference(String text) { 248 String result = removeEntityReference(text); 250 result = result.replaceAll("&","&"); result = result.replaceAll("<","<"); result = result.replaceAll("'","'"); result = result.replaceAll("\"","""); return result; 256 } 257 258 private String removeEntityReference(String text) { 259 String result = text; 260 result = AMPERSAND_PATTERN.matcher(result).replaceAll("&"); result = LESS_THAN_PATTERN.matcher(result).replaceAll("<"); result = APOSTROPHE_PATTERN.matcher(result).replaceAll("'"); result = QUOTE_PATTERN.matcher(result).replaceAll("\""); return result; 266 } 267 268 private String name = null; 269 private String value = null; 270 private static final Pattern AMPERSAND_PATTERN = Pattern.compile("&"); private static final Pattern LESS_THAN_PATTERN = Pattern.compile("<"); private static final Pattern APOSTROPHE_PATTERN = Pattern.compile("'"); private static final Pattern QUOTE_PATTERN = Pattern.compile(""");} 275 276 277 278 | Popular Tags |