/plugins/XML/tags/release-0-19/xml/completion/CompletionInfoHandler.java

# · Java · 160 lines · 118 code · 18 blank · 24 comment · 19 complexity · 9bcf882a8f12f1017ab554d5d633b16d MD5 · raw file

  1. /*
  2. * CompletionInfoHandler.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 java.util.*;
  18. import org.gjt.sp.jedit.jEdit;
  19. import org.xml.sax.helpers.DefaultHandler;
  20. import org.xml.sax.*;
  21. import xml.parser.*;
  22. import xml.*;
  23. //}}}
  24. public class CompletionInfoHandler extends DefaultHandler
  25. {
  26. //{{{ CompletionInfoHandler constructor
  27. public CompletionInfoHandler()
  28. {
  29. completionInfo = new CompletionInfo();
  30. addEntity(new EntityDecl(EntityDecl.INTERNAL,"lt","<"));
  31. addEntity(new EntityDecl(EntityDecl.INTERNAL,"gt",">"));
  32. addEntity(new EntityDecl(EntityDecl.INTERNAL,"amp","&"));
  33. addEntity(new EntityDecl(EntityDecl.INTERNAL,"quot","\""));
  34. addEntity(new EntityDecl(EntityDecl.INTERNAL,"apos","'"));
  35. } //}}}
  36. //{{{ getCompletionInfo() method
  37. public CompletionInfo getCompletionInfo()
  38. {
  39. return completionInfo;
  40. } //}}}
  41. //{{{ setDocumentLocator() method
  42. public void setDocumentLocator(Locator loc)
  43. {
  44. this.loc = loc;
  45. } //}}}
  46. //{{{ resolveEntity() method
  47. public InputSource resolveEntity(String publicId, String systemId)
  48. throws SAXException
  49. {
  50. try
  51. {
  52. return CatalogManager.resolve(
  53. loc.getSystemId(),publicId,systemId);
  54. }
  55. catch(Exception e)
  56. {
  57. throw new SAXException(e);
  58. }
  59. } //}}}
  60. //{{{ startElement() method
  61. public void startElement(String namespaceURI,
  62. String sName, // simple name
  63. String qName, // qualified name
  64. Attributes attrs) throws SAXException
  65. {
  66. if(sName.equals("dtd"))
  67. {
  68. String extend = attrs.getValue("extend");
  69. if(extend != null)
  70. {
  71. String infoURI = jEdit.getProperty(
  72. "mode." + extend
  73. + ".xml.completion-info");
  74. if(infoURI != null)
  75. {
  76. CompletionInfo extendInfo = CompletionInfo
  77. .getCompletionInfoFromResource(infoURI);
  78. if(extendInfo != null)
  79. completionInfo = (CompletionInfo)extendInfo.clone();
  80. }
  81. }
  82. }
  83. else if(sName.equals("entity"))
  84. {
  85. addEntity(new EntityDecl(
  86. EntityDecl.INTERNAL,
  87. attrs.getValue("name"),
  88. attrs.getValue("value")));
  89. }
  90. else if(sName.equals("element"))
  91. {
  92. element = new ElementDecl(
  93. completionInfo,
  94. attrs.getValue("name"),
  95. attrs.getValue("content"));
  96. completionInfo.elements.add(element);
  97. completionInfo.elementHash.put(element.name,element);
  98. if("true".equals(attrs.getValue("anywhere")))
  99. completionInfo.elementsAllowedAnywhere.add(element);
  100. }
  101. else if(sName.equals("attribute"))
  102. {
  103. String name = attrs.getValue("name");
  104. String value = attrs.getValue("value");
  105. String type = attrs.getValue("type");
  106. ArrayList values;
  107. if(type.startsWith("("))
  108. {
  109. values = new ArrayList();
  110. StringTokenizer st = new StringTokenizer(
  111. type.substring(1,type.length() - 1),"|");
  112. while(st.hasMoreTokens())
  113. {
  114. values.add(st.nextToken());
  115. }
  116. }
  117. else
  118. values = null;
  119. boolean required = "true".equals(attrs.getValue("required"));
  120. element.addAttribute(new ElementDecl.AttributeDecl(
  121. name,value,values,type,required));
  122. }
  123. } //}}}
  124. //{{{ Private members
  125. private CompletionInfo completionInfo;
  126. private Locator loc;
  127. private ElementDecl element;
  128. //{{{ addEntity() method
  129. private void addEntity(EntityDecl entity)
  130. {
  131. completionInfo.entities.add(entity);
  132. if(entity.type == EntityDecl.INTERNAL
  133. && entity.value.length() == 1)
  134. {
  135. Character ch = new Character(
  136. entity.value.charAt(0));
  137. completionInfo.entityHash.put(entity.name,ch);
  138. completionInfo.entityHash.put(ch,entity.name);
  139. }
  140. } //}}}
  141. //}}}
  142. }