/plugins/XML/tags/release-0-11/xml/XmlParsedData.java

# · Java · 146 lines · 99 code · 18 blank · 29 comment · 16 complexity · 4c7e2980cb280b2a0f9e7d3c4743095d MD5 · raw file

  1. /*
  2. * XmlParsedData.java
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 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;
  16. //{{{ Imports
  17. import javax.swing.tree.TreeModel;
  18. import java.util.*;
  19. import org.gjt.sp.jedit.Buffer;
  20. import org.gjt.sp.jedit.EditPane;
  21. import sidekick.SideKickParsedData;
  22. import xml.completion.*;
  23. import xml.parser.*;
  24. //}}}
  25. /**
  26. * Encapsulates the results of parsing a buffer, either using Xerces or the
  27. * Swing HTML parser.
  28. */
  29. public class XmlParsedData extends SideKickParsedData
  30. {
  31. public boolean html;
  32. public Map mappings;
  33. public List ids;
  34. //{{{ XmlParsedData constructor
  35. public XmlParsedData(String fileName, boolean html)
  36. {
  37. super(fileName);
  38. this.html = html;
  39. mappings = new HashMap();
  40. ids = new ArrayList();
  41. } //}}}
  42. //{{{ getNoNamespaceCompletionInfo() method
  43. public CompletionInfo getNoNamespaceCompletionInfo()
  44. {
  45. CompletionInfo info = (CompletionInfo)mappings.get("");
  46. if(info == null)
  47. {
  48. info = new CompletionInfo();
  49. mappings.put("",info);
  50. }
  51. return info;
  52. } //}}}
  53. //{{{ getElementDecl() method
  54. public ElementDecl getElementDecl(String name)
  55. {
  56. if(html)
  57. name = name.toLowerCase();
  58. String prefix = getElementNamePrefix(name);
  59. CompletionInfo info = (CompletionInfo)mappings.get(prefix);
  60. if(info == null)
  61. return null;
  62. else
  63. {
  64. String lName;
  65. int prefixLen = prefix.length();
  66. if(prefixLen == 0)
  67. lName = name;
  68. else
  69. lName = name.substring(prefixLen + 1);
  70. ElementDecl decl = (ElementDecl)info.elementHash.get(lName);
  71. if(decl == null)
  72. return null;
  73. else
  74. return decl.withPrefix(prefix);
  75. }
  76. } //}}}
  77. //{{{ getAllowedElements() method
  78. public List getAllowedElements(Buffer buffer, int pos)
  79. {
  80. TagParser.Tag parentTag = TagParser.findLastOpenTag(
  81. buffer.getText(0,pos),pos,this);
  82. ArrayList returnValue = new ArrayList();
  83. if(parentTag == null)
  84. {
  85. // add everything
  86. Iterator iter = mappings.keySet().iterator();
  87. while(iter.hasNext())
  88. {
  89. String prefix = (String)iter.next();
  90. CompletionInfo info = (CompletionInfo)
  91. mappings.get(prefix);
  92. info.getAllElements(prefix,returnValue);
  93. }
  94. }
  95. else
  96. {
  97. String parentPrefix = getElementNamePrefix(parentTag.tag);
  98. ElementDecl parentDecl = getElementDecl(parentTag.tag);
  99. if(parentDecl != null)
  100. returnValue.addAll(parentDecl.getChildElements(parentPrefix));
  101. // add everything but the parent's prefix now
  102. Iterator iter = mappings.keySet().iterator();
  103. while(iter.hasNext())
  104. {
  105. String prefix = (String)iter.next();
  106. if(!prefix.equals(parentPrefix))
  107. {
  108. CompletionInfo info = (CompletionInfo)
  109. mappings.get(prefix);
  110. info.getAllElements(prefix,returnValue);
  111. }
  112. }
  113. }
  114. Collections.sort(returnValue,new ElementDecl.Compare());
  115. return returnValue;
  116. } //}}}
  117. //{{{ Private members
  118. //{{{ getElementPrefix() method
  119. private static String getElementNamePrefix(String name)
  120. {
  121. int index = name.indexOf(':');
  122. if(index == -1)
  123. return "";
  124. else
  125. return name.substring(0,index);
  126. } //}}}
  127. //}}}
  128. }