PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/XML/xml/parser/XmlTag.java

#
Java | 165 lines | 99 code | 19 blank | 47 comment | 1 complexity | cf4d5ab0741a578dcc2cc407883ca627 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. * XmlTag.java
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000, 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.parser;
  16. //{{{ Imports
  17. import javax.swing.text.Position;
  18. import javax.swing.Icon;
  19. import org.gjt.sp.jedit.jEdit;
  20. import org.xml.sax.Attributes;
  21. import org.xml.sax.helpers.AttributesImpl;
  22. import sidekick.Asset;
  23. import xml.XmlListCellRenderer;
  24. import java.util.Map;
  25. import java.util.HashMap;
  26. //}}}
  27. public class XmlTag extends Asset
  28. {
  29. //{{{ Instance variables
  30. public Attributes attributes;
  31. public String attributeString;
  32. public String idAttributeString;
  33. public boolean empty;
  34. public String namespace;
  35. /** namespace -> prefix */
  36. public Map<String,String> namespaceBindings;
  37. private StringBuilder characters = new StringBuilder();
  38. //}}}
  39. //{{{ XmlTag constructor
  40. public XmlTag(String name,String namespace, Position start, Attributes attributes)
  41. {
  42. super(name);
  43. this.namespace = namespace;
  44. this.namespaceBindings = new HashMap<String,String>();
  45. this.start = this.end = start;
  46. this.attributes = new AttributesImpl(attributes);
  47. StringBuffer buf = new StringBuffer();
  48. buf.append(name);
  49. String idName = null;
  50. String idValue = null;
  51. for(int i = 0; i < attributes.getLength(); i++)
  52. {
  53. buf.append(' ');
  54. String aname = attributes.getQName(i);
  55. String value = attributes.getValue(i);
  56. buf.append(aname);
  57. buf.append("=\"");
  58. buf.append(value);
  59. buf.append('"');
  60. if(attributes.getLocalName(i).equalsIgnoreCase("id")
  61. || attributes.getType(i).equals("ID"))
  62. {
  63. idName = aname;
  64. idValue = value;
  65. }
  66. }
  67. attributeString = buf.toString();
  68. if(idName == null)
  69. idAttributeString = name;
  70. else
  71. idAttributeString = name + ' ' + idName + "=\"" + idValue + '"';
  72. } //}}}
  73. //{{{ getIcon() method
  74. public Icon getIcon()
  75. {
  76. return (empty ? XmlListCellRenderer.EMPTY_ELEMENT_ICON
  77. : XmlListCellRenderer.ELEMENT_ICON);
  78. } //}}}
  79. //{{{ getLongString() method
  80. public String getLongString()
  81. {
  82. return attributeString;
  83. } //}}}
  84. //{{{ getShortString() method
  85. public String getShortString()
  86. {
  87. int showAttributes = jEdit.getIntegerProperty("xml.show-attributes",1);
  88. switch(showAttributes)
  89. {
  90. case 0:
  91. return name;
  92. case 1:
  93. return idAttributeString;
  94. case 2:
  95. return attributeString;
  96. default:
  97. return null;
  98. }
  99. } //}}}
  100. //{{{ toString() method
  101. public String toString()
  102. {
  103. return attributeString;
  104. } //}}}
  105. //{{{ getLocalName() method
  106. public String getLocalName(){
  107. return name.contains(":") ? name.substring(name.indexOf(":")+1) : name;
  108. }
  109. //}}}
  110. //{{{ getPrefix() method
  111. public String getPrefix(){
  112. return name.contains(":") ? name.substring(0,name.indexOf(":")) : "";
  113. }
  114. //}}}
  115. //{{{ canAddCharacters() method
  116. /**
  117. * @return <code>true</code> if this tag should accumulate the characters from
  118. * the body of a tag. This default implementation returns <code>false</code>.
  119. * Subclasses may override to return <code>true</code>.
  120. */
  121. public boolean canAddCharacters(){
  122. return false;
  123. }
  124. //}}}
  125. //{{{ addCharacters() method
  126. /**
  127. * If <code>canAddCharacters</code> returns true, characters from the body of
  128. * the tag will be accumulated.
  129. */
  130. public void addCharacters(char[] chars){
  131. characters.append(chars);
  132. }
  133. //}}}
  134. //{{{ getCharacters() method
  135. /**
  136. * @return Any characters accumulated from the body of the tag. Will return
  137. * an empty String if <code>canAddCharacters</code> returns <code>false</code>.
  138. */
  139. public String getCharacters(){
  140. return characters.toString();
  141. }
  142. //}}}
  143. }