KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > nodes > Attribute


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xdm.nodes;
21
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25 import org.netbeans.modules.xml.xdm.visitor.XMLNodeVisitor;
26
27 /**
28  * This class represents the XML Attributes.
29  * An attribute is of the form attrName="attrValue"
30  * In terms of tokens attibute can have upto 6 tokens.
31  * Preceding whitespace token.
32  * attribute name token (wchich comprises of namespace and seperator if any and localname.
33  * whitespace token, optional.
34  * assignment operator, =.
35  * whitespace token, optional.
36  * value token which is start quote, value and end quote.
37  * @author Ajit
38  */

39 public class Attribute extends NodeImpl implements Node, org.w3c.dom.Attr JavaDoc {
40     
41     Attribute() {
42         super();
43     }
44     
45     Attribute(String JavaDoc name) {
46         super();
47         List JavaDoc<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 JavaDoc name, String JavaDoc value) {
55         super();
56         List JavaDoc<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 JavaDoc getNodeName() {
68         return getName();
69     }
70     
71     public String JavaDoc 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 JavaDoc getOwnerElement() {
84         return (org.w3c.dom.Element JavaDoc) super.getParentNode();
85     }
86     
87     public org.w3c.dom.TypeInfo JavaDoc getSchemaTypeInfo() {
88         return null;
89     }
90     
91     private void validateTokens(List JavaDoc<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 JavaDoc();
102                 nameIdx = currentIdx;
103             } else if (token.getType() == TokenType.TOKEN_ATTR_EQUAL) {
104                 if(equalIdx !=-1 || nameIdx ==-1)
105                     throw new IllegalArgumentException JavaDoc();
106                 equalIdx = currentIdx;
107             } else if (token.getType() == TokenType.TOKEN_ATTR_VAL) {
108                 if(valIdx != -1 || equalIdx ==-1)
109                     throw new IllegalArgumentException JavaDoc();
110                 valIdx = currentIdx;
111             } else if (token.getType() == TokenType.TOKEN_WHITESPACE) {
112             } else
113                 throw new IllegalArgumentException JavaDoc();
114             currentIdx++;
115         }
116         if(nameIdx == -1 || equalIdx == -1 || valIdx == -1)
117             throw new IllegalArgumentException JavaDoc();
118     }
119     
120     void setTokens(List JavaDoc<Token> newTokens) {
121 // validateTokens(newTokens);
122
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 JavaDoc getLocalName() {
132         String JavaDoc 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 JavaDoc localName) {
141         String JavaDoc 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 JavaDoc getPrefix() {
152         String JavaDoc 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 JavaDoc prefix) {
161         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getValue() {
197         if(value==null) {
198             for(Token token : getTokens()) {
199                 if(token.getType() == TokenType.TOKEN_ATTR_VAL) {
200                     String JavaDoc 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 JavaDoc 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 JavaDoc oldVal = token.getValue();
221                 String JavaDoc 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 JavaDoc<Integer JavaDoc,String JavaDoc> allNS, Map JavaDoc<String JavaDoc,String JavaDoc> prefixes) {
234         if (allNS == null) return;
235         
236         String JavaDoc[] parts = this.getValue().split(":");
237         String JavaDoc prefix = parts.length > 1 ? parts[0] : null;
238         if (prefix != null) {
239             String JavaDoc namespace = lookupNamespaceURI(prefix);
240             if (namespace != null) {
241                 prefixes.put(prefix, namespace);
242             }
243         }
244         super.cloneNamespacePrefixes(allNS, prefixes);
245     }
246     
247     private String JavaDoc insertEntityReference(String JavaDoc text) {
248         // just make sure we replace & with &amp; and not &amp; with &&amp;amp; and so on
249
String JavaDoc result = removeEntityReference(text);
250         result = result.replaceAll("&","&amp;"); //replace &
251
result = result.replaceAll("<","&lt;"); //replace <
252
// result = result.replaceAll(">","&gt;"); //replace >
253
result = result.replaceAll("'","&apos;"); //replace '
254
result = result.replaceAll("\"","&quot;"); //replace "
255
return result;
256     }
257
258     private String JavaDoc removeEntityReference(String JavaDoc text) {
259         String JavaDoc result = text;
260         result = AMPERSAND_PATTERN.matcher(result).replaceAll("&"); //replace with &
261
result = LESS_THAN_PATTERN.matcher(result).replaceAll("<"); //replace with <
262
// result = result.replaceAll("&gt;",">"); //replace with >
263
result = APOSTROPHE_PATTERN.matcher(result).replaceAll("'"); //replace with '
264
result = QUOTE_PATTERN.matcher(result).replaceAll("\""); //replace with "
265
return result;
266     }
267     
268     private String JavaDoc name = null;
269     private String JavaDoc value = null;
270     private static final Pattern JavaDoc AMPERSAND_PATTERN = Pattern.compile("&amp;"); //NOI18N
271
private static final Pattern JavaDoc LESS_THAN_PATTERN = Pattern.compile("&lt;"); //NOI18N
272
private static final Pattern JavaDoc APOSTROPHE_PATTERN = Pattern.compile("&apos;"); //NOI18N
273
private static final Pattern JavaDoc QUOTE_PATTERN = Pattern.compile("&quot;");//NOI18N
274
}
275
276
277
278
Popular Tags