PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 314 lines | 223 code | 45 blank | 46 comment | 22 complexity | 907ab912c5fa31f23a8396ee1f23f160 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. * CompletionInfo.java
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001, 2003 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.Iterator;
  19. import java.util.List;
  20. import java.util.regex.Matcher;
  21. import java.util.regex.Pattern;
  22. import java.util.regex.PatternSyntaxException;
  23. import org.gjt.sp.jedit.Buffer;
  24. import org.gjt.sp.jedit.MiscUtilities;
  25. import org.gjt.sp.jedit.jEdit;
  26. import org.gjt.sp.util.Log;
  27. import org.gjt.sp.util.StandardUtilities;
  28. import org.xml.sax.SAXException;
  29. import org.xml.sax.XMLReader;
  30. //}}}
  31. public class CompletionInfo
  32. {
  33. public ArrayList<ElementDecl> elements;
  34. public HashMap<String, ElementDecl> elementHash;
  35. public ArrayList<EntityDecl> entities;
  36. /** Appears to implement a bidirectional relationship? */
  37. public HashMap entityHash;
  38. public ArrayList elementsAllowedAnywhere;
  39. /** namespace for this completion info */
  40. public String namespace;
  41. public boolean nameConflict;
  42. //{{{ CompletionInfo constructor
  43. public CompletionInfo()
  44. {
  45. this(new ArrayList<ElementDecl>(), new HashMap<String, ElementDecl>(),
  46. new ArrayList<EntityDecl>(), new HashMap(),
  47. new ArrayList());
  48. addEntity(EntityDecl.INTERNAL,"lt","<");
  49. addEntity(EntityDecl.INTERNAL,"gt",">");
  50. addEntity(EntityDecl.INTERNAL,"amp","&");
  51. addEntity(EntityDecl.INTERNAL,"quot","\"");
  52. addEntity(EntityDecl.INTERNAL,"apos","'");
  53. } //}}}
  54. //{{{ CompletionInfo constructor
  55. public CompletionInfo(ArrayList<ElementDecl> elements,
  56. HashMap<String, ElementDecl> elementHash,
  57. ArrayList<EntityDecl> entities, HashMap entityHash,
  58. ArrayList elementsAllowedAnywhere)
  59. {
  60. this.elements = elements;
  61. this.elementHash = elementHash;
  62. this.entities = entities;
  63. this.entityHash = entityHash;
  64. this.elementsAllowedAnywhere = elementsAllowedAnywhere;
  65. } //}}}
  66. //{{{ addEntity() method
  67. public void addEntity(int type, String name, String value)
  68. {
  69. addEntity(new EntityDecl(type,name,value));
  70. } //}}}
  71. //{{{ addEntity() method
  72. public void addEntity(int type, String name, String publicId, String systemId)
  73. {
  74. addEntity(new EntityDecl(type,name,publicId,systemId));
  75. } //}}}
  76. //{{{ addEntity() method
  77. public void addEntity(EntityDecl entity)
  78. {
  79. entities.add(entity);
  80. if(entity.type == EntityDecl.INTERNAL
  81. && entity.value.length() == 1)
  82. {
  83. Character ch = new Character(entity.value.charAt(0));
  84. entityHash.put(entity.name, ch);
  85. entityHash.put(ch, entity.name);
  86. }
  87. } //}}}
  88. //{{{ addElement() method
  89. public void addElement(ElementDecl element)
  90. {
  91. elementHash.put(element.name,element);
  92. elements.add(element);
  93. } //}}}
  94. //{{{ getAllElements() method
  95. public void getAllElements(List<ElementDecl> out)
  96. {
  97. // only look for global elements, so use elementHash instead of elements
  98. for(ElementDecl decl: elementHash.values())
  99. {
  100. if (decl.isAbstract())
  101. {
  102. List<ElementDecl> repls = decl.findReplacements();
  103. out.addAll(repls);
  104. }
  105. else out.add(decl);
  106. }
  107. } //}}}
  108. //{{{ getElementDeclLocal(CompletionInfo, localName) method
  109. /**
  110. * @return the first declaration (global or local) found for localName
  111. * or null
  112. */
  113. public ElementDecl getElementDeclLocal(String localName)
  114. {
  115. // resorting to the first one in all declarations,
  116. // even if it's the wrong one
  117. for(ElementDecl e: elements){
  118. if(localName.equals(e.name)){
  119. return e;
  120. }
  121. }
  122. return null;
  123. }
  124. //}}}
  125. //{{{ toString() method
  126. public String toString()
  127. {
  128. StringBuffer buf = new StringBuffer();
  129. buf.append("<element-list>\n\n");
  130. for(int i = 0; i < elements.size(); i++)
  131. {
  132. buf.append(elements.get(i));
  133. buf.append('\n');
  134. }
  135. buf.append("\n</element-list>\n\n<entity-list>\n\n");
  136. buf.append("<!-- not implemented yet -->\n");
  137. /* for(int i = 0; i < entities.size(); i++)
  138. {
  139. buf.append(entities.get(i));
  140. buf.append('\n');
  141. } */
  142. buf.append("\n</entity-list>");
  143. return buf.toString();
  144. } //}}}
  145. //{{{ getCompletionInfoForBuffer() method
  146. public static CompletionInfo getCompletionInfoForBuffer(Buffer buffer)
  147. {
  148. Iterator iter = globs.keySet().iterator();
  149. while(iter.hasNext())
  150. {
  151. Pattern re = (Pattern) iter.next();
  152. Matcher m = re.matcher(buffer.getName());
  153. if(m.matches())
  154. return getCompletionInfoFromResource((String)globs.get(re));
  155. }
  156. String resource = jEdit.getProperty("mode."
  157. + buffer.getMode().getName()
  158. + ".xml.completion-info");
  159. if(resource != null)
  160. {
  161. CompletionInfo info = getCompletionInfoFromResource(resource);
  162. if(info != null)
  163. return info;
  164. }
  165. return null;
  166. } //}}}
  167. //{{{ getCompletionInfoForNamespace() method
  168. public static CompletionInfo getCompletionInfoForNamespace(String namespace)
  169. {
  170. synchronized(lock)
  171. {
  172. Object obj = completionInfoNamespaces.get(namespace);
  173. if(obj instanceof String)
  174. {
  175. CompletionInfo info = getCompletionInfoFromResource((String)obj);
  176. info.namespace = namespace;
  177. completionInfoNamespaces.put(namespace, info);
  178. return info;
  179. }
  180. else
  181. return (CompletionInfo)obj;
  182. }
  183. } //}}}
  184. //{{{ getCompletionInfoFromResource() method
  185. public static CompletionInfo getCompletionInfoFromResource(String resource)
  186. {
  187. synchronized(lock)
  188. {
  189. CompletionInfo info = (CompletionInfo)completionInfoResources.get(resource);
  190. if(info != null)
  191. return info;
  192. Log.log(Log.DEBUG,CompletionInfo.class,"Loading " + resource);
  193. CompletionInfoHandler handler = new CompletionInfoHandler();
  194. try
  195. {
  196. XMLReader parser = new org.apache.xerces.parsers.SAXParser();
  197. parser.setFeature("http://apache.org/xml/features/validation/dynamic",true);
  198. parser.setFeature("http://xml.org/sax/features/use-entity-resolver2", true);
  199. parser.setErrorHandler(handler);
  200. parser.setEntityResolver(handler);
  201. parser.setContentHandler(handler);
  202. parser.parse(resource);
  203. }
  204. catch(SAXException se)
  205. {
  206. Throwable e = se.getException();
  207. if(e == null)
  208. e = se;
  209. Log.log(Log.ERROR,CompletionInfo.class,e);
  210. }
  211. catch(Exception e)
  212. {
  213. Log.log(Log.ERROR,CompletionInfo.class,e);
  214. }
  215. info = handler.getCompletionInfo();
  216. completionInfoResources.put (resource, info);
  217. return info;
  218. }
  219. } //}}}
  220. //{{{ clone() method
  221. public Object clone()
  222. {
  223. return new CompletionInfo(
  224. (ArrayList)elements.clone(),
  225. (HashMap)elementHash.clone(),
  226. (ArrayList)entities.clone(),
  227. (HashMap)entityHash.clone(),
  228. (ArrayList)elementsAllowedAnywhere.clone()
  229. );
  230. } //}}}
  231. //{{{ Private members
  232. private static HashMap<Pattern, String> globs;
  233. private static HashMap<String, CompletionInfo> completionInfoResources;
  234. private static HashMap<String, Object> completionInfoNamespaces;
  235. private static Object lock;
  236. //{{{ Class initializer
  237. static
  238. {
  239. completionInfoResources = new HashMap<String, CompletionInfo>();
  240. globs = new HashMap<Pattern, String>();
  241. int i = 0;
  242. String glob;
  243. while((glob = jEdit.getProperty("xml.completion.glob." + i + ".key")) != null)
  244. {
  245. String info = jEdit.getProperty("xml.completion.glob." + i + ".value");
  246. try
  247. {
  248. globs.put(Pattern.compile(StandardUtilities.globToRE(glob),
  249. Pattern.CASE_INSENSITIVE), info);
  250. }
  251. catch(PatternSyntaxException pse)
  252. {
  253. Log.log(Log.ERROR,CompletionInfo.class,pse);
  254. }
  255. i++;
  256. }
  257. completionInfoNamespaces = new HashMap<String, Object>();
  258. i = 0;
  259. String namespace;
  260. while((namespace = jEdit.getProperty("xml.completion.namespace." + i + ".key")) != null)
  261. {
  262. String info = jEdit.getProperty("xml.completion.namespace." + i + ".value");
  263. completionInfoNamespaces.put(namespace, info);
  264. i++;
  265. }
  266. lock = new Object();
  267. } //}}}
  268. //}}}
  269. }