PageRenderTime 178ms CodeModel.GetById 121ms app.highlight 23ms RepoModel.GetById 22ms app.codeStats 0ms

/bundles/plugins-trunk/XML/xml/completion/ElementDecl.java

#
Java | 312 lines | 240 code | 38 blank | 34 comment | 29 complexity | 97ba2903d4975d5ae2a538d792675faf MD5 | raw file
  1/*
  2 * ElementDecl.java
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2001 Slava Pestov
  7 *
  8 * The XML plugin is licensed under the GNU General Public License, with
  9 * the following exception:
 10 *
 11 * "Permission is granted to link this code with software released under
 12 * the Apache license version 1.1, for example used by the Xerces XML
 13 * parser package."
 14 */
 15
 16package xml.completion;
 17
 18import java.util.ArrayList;
 19import java.util.HashMap;
 20import java.util.HashSet;
 21import java.util.Iterator;
 22import java.util.List;
 23import java.util.Map;
 24import java.util.Set;
 25import java.util.StringTokenizer;
 26
 27import org.gjt.sp.util.StandardUtilities;
 28import org.xml.sax.helpers.NamespaceSupport;
 29
 30import xml.EditTagDialog;
 31
 32public class ElementDecl
 33{
 34	public CompletionInfo completionInfo;
 35
 36	public String name;
 37
 38	public boolean empty;
 39	public boolean any;
 40
 41	public List<AttributeDecl> attributes;
 42	public Map<String, AttributeDecl> attributeHash;
 43	public Set<String> content;
 44	public Map<String, ElementDecl> elementHash;
 45
 46	//{{{ ElementDecl constructor
 47	public ElementDecl(CompletionInfo completionInfo, String name, String content)
 48	{
 49		this.completionInfo = completionInfo;
 50
 51		this.name = name;
 52
 53		if(content != null)
 54			setContent(content);
 55
 56		attributes = new ArrayList<AttributeDecl>();
 57		attributeHash = new HashMap<String, AttributeDecl>();
 58	} //}}}
 59
 60	//{{{ ElementDecl constructor
 61	private ElementDecl(CompletionInfo completionInfo, String name,
 62		boolean empty, boolean any, List<AttributeDecl> attributes, Map<String, AttributeDecl> attributeHash,
 63		Set<String> content)
 64	{
 65		this.completionInfo = completionInfo;
 66		this.name = name;
 67		this.empty = empty;
 68		this.any = any;
 69		this.attributes = attributes;
 70		this.attributeHash = attributeHash;
 71		this.content = content;
 72	} //}}}
 73
 74	/**
 75	 * @return true if this is an abstract element, representing a class of
 76	 * 	other elements (used in w3c xsd)
 77	 */
 78	public boolean isAbstract() {
 79		return false;
 80	}
 81	//{{{ setContent() method
 82	public void setContent(String content)
 83	{
 84		if(content.equals("EMPTY"))
 85			empty = true;
 86		else if(content.equals("ANY"))
 87			any = true;
 88		else
 89		{
 90			this.content = new HashSet<String>();
 91
 92			StringTokenizer st = new StringTokenizer(content,
 93				"(?*+|,) \t\n");
 94			while(st.hasMoreTokens())
 95			{
 96				String element = st.nextToken();
 97				if(element.equals("#PCDATA"))
 98					continue;
 99
100				this.content.add(element);
101			}
102		}
103	} //}}}
104
105	//{{{ getChildElements() method
106	public List<ElementDecl> getChildElements()
107	{
108		ArrayList<ElementDecl>children = new ArrayList<ElementDecl>(100);
109
110		if(any)
111		{
112			children.addAll(completionInfo.elements);
113		}
114		else
115		{
116			children.addAll(completionInfo.elementsAllowedAnywhere);
117
118			if(content != null)
119			{
120				Iterator iter = content.iterator();
121				while(iter.hasNext())
122				{
123					ElementDecl decl = null;
124					Object n = (String)iter.next();
125					if(elementHash == null){
126						//backward compatible
127						decl = (ElementDecl)completionInfo
128							.elementHash.get(n);
129					}else{
130						decl = elementHash.get(n);
131					}
132						
133					if(decl != null) {
134						if (decl.isAbstract())
135							children.addAll(decl.findReplacements());
136						else 
137							children.add(decl);
138					}
139				}
140			}
141		}
142		
143		return children;
144	} //}}}
145
146	/**
147	 * Finds all elements which can be replaced by this one. 
148	 *  
149	 * @return a list of all elements with matching substitutionGroup, or null if there are
150	 * none.
151	 * 
152	 */
153	public List<ElementDecl> findReplacements() {
154		return null;
155	}
156	
157	//{{{ getAttribute() method
158	public AttributeDecl getAttribute(String attrname)
159	{
160		return attributeHash.get(attrname);
161	} //}}}
162
163	//{{{ addAttribute() method
164	public void addAttribute(AttributeDecl attribute)
165	{
166		attributeHash.put(attribute.name, attribute);
167
168		for(int i = 0; i < attributes.size(); i++)
169		{
170			AttributeDecl attr = (AttributeDecl)attributes.get(i);
171			if(attr.name.compareTo(attribute.name) > 0)
172			{
173				attributes.add(i, attribute);
174				return;
175			}
176		}
177
178		attributes.add(attribute);
179	} //}}}
180
181	//{{{ getRequiredAttributesString()
182	public String getRequiredAttributesString(Map<String,String> namespaces, Map<String, String> namespacesToInsert)
183	{
184		StringBuffer buf = new StringBuffer();
185
186		for(int i = 0; i < attributes.size(); i++)
187		{
188			AttributeDecl attrDecl = (AttributeDecl)attributes.get(i);
189
190			if(attrDecl.required)
191			{
192				buf.append(' ');
193				buf.append(EditTagDialog.composeName(attrDecl.name, attrDecl.namespace, namespaces, namespacesToInsert));
194				buf.append("=\"");
195				if(attrDecl.value != null)
196					buf.append(attrDecl.value);
197				buf.append('"');
198			}
199		}
200
201		return buf.toString();
202	} //}}}
203
204	//{{{ toString() method
205	public String toString()
206	{
207		StringBuffer buf = new StringBuffer();
208		buf.append("<element name=\"");
209		buf.append(name);
210		buf.append('"');
211
212		buf.append("\ncontent=\"");
213
214		if(empty)
215			buf.append("EMPTY");
216		else if(content != null)
217		{
218			buf.append('(');
219
220			Iterator iter = content.iterator();
221			while(iter.hasNext())
222			{
223				buf.append(iter.next());
224				if(iter.hasNext())
225					buf.append('|');
226			}
227
228			buf.append(')');
229		}
230
231		buf.append('"');
232
233		if(attributes.size() == 0)
234			buf.append(" />");
235		else
236		{
237			buf.append(">\n");
238			for(int i = 0; i < attributes.size(); i++)
239			{
240				buf.append(attributes.get(i));
241				buf.append('\n');
242			}
243			buf.append("</element>");
244		}
245
246		return buf.toString();
247	} //}}}
248
249	//{{{ AttributeDecl class
250	public static class AttributeDecl
251	{
252		public String name;
253		public String namespace;
254		public String value;
255		public ArrayList values;
256		public String type;
257		public boolean required;
258		
259		public AttributeDecl(String name, String namespace, String value, ArrayList<String> values,
260			String type, boolean required)
261		{
262			this.name = name;
263			this.namespace = namespace;
264			this.value = value;
265			this.values = values;
266			this.type = type;
267			this.required = required;
268		}
269
270		public AttributeDecl copy()
271		{
272			return new AttributeDecl(name,
273				namespace,value,
274				(values == null ? null : new ArrayList(values)),
275				type,
276				required);
277		}
278		
279		public String toString()
280		{
281			StringBuffer buf = new StringBuffer("<attribute name=\"");
282			buf.append(name);
283			buf.append('"');
284
285			if(value != null)
286			{
287				buf.append(" value=\"");
288				buf.append(value);
289				buf.append('"');
290			}
291
292			buf.append(" type=\"");
293			buf.append(type);
294			buf.append('"');
295
296			if(required)
297				buf.append(" required=\"true\"");
298
299			buf.append(" />");
300			return buf.toString();
301		}
302	} //}}}
303
304	//{{{ Compare class
305	public static class Compare implements java.util.Comparator<ElementDecl>
306	{
307		public int compare(ElementDecl obj1, ElementDecl obj2)
308		{
309			return StandardUtilities.compareStrings(obj1.name, obj2.name, true);
310		}
311	} //}}}
312}