PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package xml.completion;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.HashSet;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import java.util.StringTokenizer;
  24. import org.gjt.sp.util.StandardUtilities;
  25. import org.xml.sax.helpers.NamespaceSupport;
  26. import xml.EditTagDialog;
  27. public class ElementDecl
  28. {
  29. public CompletionInfo completionInfo;
  30. public String name;
  31. public boolean empty;
  32. public boolean any;
  33. public List<AttributeDecl> attributes;
  34. public Map<String, AttributeDecl> attributeHash;
  35. public Set<String> content;
  36. public Map<String, ElementDecl> elementHash;
  37. //{{{ ElementDecl constructor
  38. public ElementDecl(CompletionInfo completionInfo, String name, String content)
  39. {
  40. this.completionInfo = completionInfo;
  41. this.name = name;
  42. if(content != null)
  43. setContent(content);
  44. attributes = new ArrayList<AttributeDecl>();
  45. attributeHash = new HashMap<String, AttributeDecl>();
  46. } //}}}
  47. //{{{ ElementDecl constructor
  48. private ElementDecl(CompletionInfo completionInfo, String name,
  49. boolean empty, boolean any, List<AttributeDecl> attributes, Map<String, AttributeDecl> attributeHash,
  50. Set<String> content)
  51. {
  52. this.completionInfo = completionInfo;
  53. this.name = name;
  54. this.empty = empty;
  55. this.any = any;
  56. this.attributes = attributes;
  57. this.attributeHash = attributeHash;
  58. this.content = content;
  59. } //}}}
  60. /**
  61. * @return true if this is an abstract element, representing a class of
  62. * other elements (used in w3c xsd)
  63. */
  64. public boolean isAbstract() {
  65. return false;
  66. }
  67. //{{{ setContent() method
  68. public void setContent(String content)
  69. {
  70. if(content.equals("EMPTY"))
  71. empty = true;
  72. else if(content.equals("ANY"))
  73. any = true;
  74. else
  75. {
  76. this.content = new HashSet<String>();
  77. StringTokenizer st = new StringTokenizer(content,
  78. "(?*+|,) \t\n");
  79. while(st.hasMoreTokens())
  80. {
  81. String element = st.nextToken();
  82. if(element.equals("#PCDATA"))
  83. continue;
  84. this.content.add(element);
  85. }
  86. }
  87. } //}}}
  88. //{{{ getChildElements() method
  89. public List<ElementDecl> getChildElements()
  90. {
  91. ArrayList<ElementDecl>children = new ArrayList<ElementDecl>(100);
  92. if(any)
  93. {
  94. children.addAll(completionInfo.elements);
  95. }
  96. else
  97. {
  98. children.addAll(completionInfo.elementsAllowedAnywhere);
  99. if(content != null)
  100. {
  101. Iterator iter = content.iterator();
  102. while(iter.hasNext())
  103. {
  104. ElementDecl decl = null;
  105. Object n = (String)iter.next();
  106. if(elementHash == null){
  107. //backward compatible
  108. decl = (ElementDecl)completionInfo
  109. .elementHash.get(n);
  110. }else{
  111. decl = elementHash.get(n);
  112. }
  113. if(decl != null) {
  114. if (decl.isAbstract())
  115. children.addAll(decl.findReplacements());
  116. else
  117. children.add(decl);
  118. }
  119. }
  120. }
  121. }
  122. return children;
  123. } //}}}
  124. /**
  125. * Finds all elements which can be replaced by this one.
  126. *
  127. * @return a list of all elements with matching substitutionGroup, or null if there are
  128. * none.
  129. *
  130. */
  131. public List<ElementDecl> findReplacements() {
  132. return null;
  133. }
  134. //{{{ getAttribute() method
  135. public AttributeDecl getAttribute(String attrname)
  136. {
  137. return attributeHash.get(attrname);
  138. } //}}}
  139. //{{{ addAttribute() method
  140. public void addAttribute(AttributeDecl attribute)
  141. {
  142. attributeHash.put(attribute.name, attribute);
  143. for(int i = 0; i < attributes.size(); i++)
  144. {
  145. AttributeDecl attr = (AttributeDecl)attributes.get(i);
  146. if(attr.name.compareTo(attribute.name) > 0)
  147. {
  148. attributes.add(i, attribute);
  149. return;
  150. }
  151. }
  152. attributes.add(attribute);
  153. } //}}}
  154. //{{{ getRequiredAttributesString()
  155. public String getRequiredAttributesString(Map<String,String> namespaces, Map<String, String> namespacesToInsert)
  156. {
  157. StringBuffer buf = new StringBuffer();
  158. for(int i = 0; i < attributes.size(); i++)
  159. {
  160. AttributeDecl attrDecl = (AttributeDecl)attributes.get(i);
  161. if(attrDecl.required)
  162. {
  163. buf.append(' ');
  164. buf.append(EditTagDialog.composeName(attrDecl.name, attrDecl.namespace, namespaces, namespacesToInsert));
  165. buf.append("=\"");
  166. if(attrDecl.value != null)
  167. buf.append(attrDecl.value);
  168. buf.append('"');
  169. }
  170. }
  171. return buf.toString();
  172. } //}}}
  173. //{{{ toString() method
  174. public String toString()
  175. {
  176. StringBuffer buf = new StringBuffer();
  177. buf.append("<element name=\"");
  178. buf.append(name);
  179. buf.append('"');
  180. buf.append("\ncontent=\"");
  181. if(empty)
  182. buf.append("EMPTY");
  183. else if(content != null)
  184. {
  185. buf.append('(');
  186. Iterator iter = content.iterator();
  187. while(iter.hasNext())
  188. {
  189. buf.append(iter.next());
  190. if(iter.hasNext())
  191. buf.append('|');
  192. }
  193. buf.append(')');
  194. }
  195. buf.append('"');
  196. if(attributes.size() == 0)
  197. buf.append(" />");
  198. else
  199. {
  200. buf.append(">\n");
  201. for(int i = 0; i < attributes.size(); i++)
  202. {
  203. buf.append(attributes.get(i));
  204. buf.append('\n');
  205. }
  206. buf.append("</element>");
  207. }
  208. return buf.toString();
  209. } //}}}
  210. //{{{ AttributeDecl class
  211. public static class AttributeDecl
  212. {
  213. public String name;
  214. public String namespace;
  215. public String value;
  216. public ArrayList values;
  217. public String type;
  218. public boolean required;
  219. public AttributeDecl(String name, String namespace, String value, ArrayList<String> values,
  220. String type, boolean required)
  221. {
  222. this.name = name;
  223. this.namespace = namespace;
  224. this.value = value;
  225. this.values = values;
  226. this.type = type;
  227. this.required = required;
  228. }
  229. public AttributeDecl copy()
  230. {
  231. return new AttributeDecl(name,
  232. namespace,value,
  233. (values == null ? null : new ArrayList(values)),
  234. type,
  235. required);
  236. }
  237. public String toString()
  238. {
  239. StringBuffer buf = new StringBuffer("<attribute name=\"");
  240. buf.append(name);
  241. buf.append('"');
  242. if(value != null)
  243. {
  244. buf.append(" value=\"");
  245. buf.append(value);
  246. buf.append('"');
  247. }
  248. buf.append(" type=\"");
  249. buf.append(type);
  250. buf.append('"');
  251. if(required)
  252. buf.append(" required=\"true\"");
  253. buf.append(" />");
  254. return buf.toString();
  255. }
  256. } //}}}
  257. //{{{ Compare class
  258. public static class Compare implements java.util.Comparator<ElementDecl>
  259. {
  260. public int compare(ElementDecl obj1, ElementDecl obj2)
  261. {
  262. return StandardUtilities.compareStrings(obj1.name, obj2.name, true);
  263. }
  264. } //}}}
  265. }