/plugins/XML/tags/release-0-11-1/xml/completion/CompletionInfo.java

# · Java · 257 lines · 186 code · 36 blank · 35 comment · 18 complexity · d740bdfcf554baf9377e17de08f72e15 MD5 · raw file

  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. //{{{ Imports
  17. import gnu.regexp.*;
  18. import java.util.*;
  19. import org.gjt.sp.jedit.*;
  20. import org.gjt.sp.util.Log;
  21. import org.xml.sax.XMLReader;
  22. import org.xml.sax.SAXException;
  23. import xml.parser.*;
  24. //}}}
  25. public class CompletionInfo
  26. {
  27. public List elements;
  28. public Map elementHash;
  29. public List entities;
  30. public Map entityHash;
  31. public List elementsAllowedAnywhere;
  32. //{{{ CompletionInfo constructor
  33. public CompletionInfo()
  34. {
  35. this(new ArrayList(), new HashMap(),
  36. new ArrayList(), new HashMap(),
  37. new ArrayList());
  38. addEntity(EntityDecl.INTERNAL,"lt","<");
  39. addEntity(EntityDecl.INTERNAL,"gt",">");
  40. addEntity(EntityDecl.INTERNAL,"amp","&");
  41. addEntity(EntityDecl.INTERNAL,"quot","\"");
  42. addEntity(EntityDecl.INTERNAL,"apos","'");
  43. } //}}}
  44. //{{{ CompletionInfo constructor
  45. public CompletionInfo(List elements, Map elementHash,
  46. List entities, Map entityHash,
  47. List elementsAllowedAnywhere)
  48. {
  49. this.elements = elements;
  50. this.elementHash = elementHash;
  51. this.entities = entities;
  52. this.entityHash = entityHash;
  53. this.elementsAllowedAnywhere = elementsAllowedAnywhere;
  54. } //}}}
  55. //{{{ addEntity() method
  56. public void addEntity(int type, String name, String value)
  57. {
  58. addEntity(new EntityDecl(type,name,value));
  59. } //}}}
  60. //{{{ addEntity() method
  61. public void addEntity(int type, String name, String publicId, String systemId)
  62. {
  63. addEntity(new EntityDecl(type,name,publicId,systemId));
  64. } //}}}
  65. //{{{ addEntity() method
  66. public void addEntity(EntityDecl entity)
  67. {
  68. entities.add(entity);
  69. if(entity.type == EntityDecl.INTERNAL
  70. && entity.value.length() == 1)
  71. {
  72. Character ch = new Character(entity.value.charAt(0));
  73. entityHash.put(entity.name,ch);
  74. entityHash.put(ch,entity.name);
  75. }
  76. } //}}}
  77. //{{{ addElement() method
  78. public void addElement(ElementDecl element)
  79. {
  80. elementHash.put(element.name,element);
  81. elements.add(element);
  82. } //}}}
  83. //{{{ getAllElements() method
  84. public void getAllElements(String prefix, List out)
  85. {
  86. for(int i = 0; i < elements.size(); i++)
  87. {
  88. out.add(((ElementDecl)elements.get(i)).withPrefix(prefix));
  89. }
  90. } //}}}
  91. //{{{ toString() method
  92. public String toString()
  93. {
  94. StringBuffer buf = new StringBuffer();
  95. buf.append("<element-list>\n\n");
  96. for(int i = 0; i < elements.size(); i++)
  97. {
  98. buf.append(elements.get(i));
  99. buf.append('\n');
  100. }
  101. buf.append("\n</element-list>\n\n<entity-list>\n\n");
  102. buf.append("<!-- not implemented yet -->\n");
  103. /* for(int i = 0; i < entities.size(); i++)
  104. {
  105. buf.append(entities.get(i));
  106. buf.append('\n');
  107. } */
  108. buf.append("\n</entity-list>");
  109. return buf.toString();
  110. } //}}}
  111. //{{{ getCompletionInfoForBuffer() method
  112. public static CompletionInfo getCompletionInfoForBuffer(Buffer buffer)
  113. {
  114. Iterator iter = globs.keySet().iterator();
  115. while(iter.hasNext())
  116. {
  117. RE re = (RE)iter.next();
  118. if(re.isMatch(buffer.getName()))
  119. return getCompletionInfoFromResource((String)globs.get(re));
  120. }
  121. String resource = jEdit.getProperty("mode."
  122. + buffer.getMode().getName()
  123. + ".xml.completion-info");
  124. if(resource != null)
  125. {
  126. CompletionInfo info = getCompletionInfoFromResource(resource);
  127. if(info != null)
  128. return info;
  129. }
  130. return null;
  131. } //}}}
  132. //{{{ getCompletionInfoForNamespace() method
  133. public static CompletionInfo getCompletionInfoForNamespace(String namespace)
  134. {
  135. synchronized(lock)
  136. {
  137. Object obj = completionInfoNamespaces.get(namespace);
  138. if(obj instanceof String)
  139. {
  140. CompletionInfo info = getCompletionInfoFromResource((String)obj);
  141. completionInfoNamespaces.put(namespace,info);
  142. return info;
  143. }
  144. else
  145. return (CompletionInfo)obj;
  146. }
  147. } //}}}
  148. //{{{ getCompletionInfoFromResource() method
  149. public static CompletionInfo getCompletionInfoFromResource(String resource)
  150. {
  151. synchronized(lock)
  152. {
  153. CompletionInfo info = (CompletionInfo)completionInfoResources.get(resource);
  154. if(info != null)
  155. return info;
  156. Log.log(Log.DEBUG,CompletionInfo.class,"Loading " + resource);
  157. CompletionInfoHandler handler = new CompletionInfoHandler();
  158. try
  159. {
  160. XMLReader parser = new org.apache.xerces.parsers.SAXParser();
  161. parser.setFeature("http://apache.org/xml/features/validation/dynamic",true);
  162. parser.setErrorHandler(handler);
  163. parser.setEntityResolver(handler);
  164. parser.setContentHandler(handler);
  165. parser.parse(resource);
  166. }
  167. catch(SAXException se)
  168. {
  169. Throwable e = se.getException();
  170. if(e == null)
  171. e = se;
  172. Log.log(Log.ERROR,CompletionInfo.class,e);
  173. }
  174. catch(Exception e)
  175. {
  176. Log.log(Log.ERROR,CompletionInfo.class,e);
  177. }
  178. info = handler.getCompletionInfo();
  179. completionInfoResources.put(resource,info);
  180. return info;
  181. }
  182. } //}}}
  183. //{{{ Private members
  184. private static HashMap globs;
  185. private static HashMap completionInfoResources;
  186. private static HashMap completionInfoNamespaces;
  187. private static Object lock;
  188. //{{{ Class initializer
  189. static
  190. {
  191. completionInfoResources = new HashMap();
  192. globs = new HashMap();
  193. int i = 0;
  194. String glob;
  195. while((glob = jEdit.getProperty("xml.completion.glob." + i + ".key")) != null)
  196. {
  197. String info = jEdit.getProperty("xml.completion.glob." + i + ".value");
  198. try
  199. {
  200. globs.put(new RE(MiscUtilities.globToRE(glob),
  201. RE.REG_ICASE),info);
  202. }
  203. catch(REException re)
  204. {
  205. Log.log(Log.ERROR,CompletionInfo.class,re);
  206. }
  207. i++;
  208. }
  209. completionInfoNamespaces = new HashMap();
  210. i = 0;
  211. String namespace;
  212. while((namespace = jEdit.getProperty("xml.completion.namespace." + i + ".key")) != null)
  213. {
  214. String info = jEdit.getProperty("xml.completion.namespace." + i + ".value");
  215. completionInfoNamespaces.put(namespace,info);
  216. i++;
  217. }
  218. lock = new Object();
  219. } //}}}
  220. //}}}
  221. }