PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/XML/xml/TldXmlParsedData.java

#
Java | 116 lines | 83 code | 12 blank | 21 comment | 10 complexity | 403d3e25be0a5f8b0d0a860a0ec78aaf 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. package xml;
  2. import java.util.Comparator;
  3. import java.util.Enumeration;
  4. import javax.swing.tree.DefaultMutableTreeNode;
  5. import javax.swing.tree.TreeNode;
  6. import sidekick.Asset;
  7. import sidekick.IAsset;
  8. import sidekick.ExpansionModel;
  9. import xml.parser.TldXmlTag;
  10. import org.gjt.sp.jedit.View;
  11. /**
  12. * Special version of XmlParsedData for tld files. Tld files have very few
  13. * tags with attributes, and the interesting things to see in the SideKick
  14. * tree are generally hidden from view, like the 'name' child tag of a 'tag'
  15. * or 'function' node, This class renames those nodes with a child node
  16. * named 'name' so that the name is more readily visible in the tree.
  17. */
  18. public class TldXmlParsedData extends XmlParsedData {
  19. public TldXmlParsedData(String filename, boolean html) {
  20. super(filename, html);
  21. }
  22. /**
  23. * Rename the assets contained in the tree nodes to use the value
  24. * contained in the body of any child tags named 'name'.
  25. */
  26. @Override
  27. public void done(View view) {
  28. Enumeration children = root.children();
  29. while (children.hasMoreElements()) {
  30. processChild((TreeNode) children.nextElement());
  31. }
  32. tree.reload();
  33. sort(view);
  34. }
  35. private void processChild(TreeNode node) {
  36. IAsset asset = (IAsset) ((DefaultMutableTreeNode) node).getUserObject();
  37. if (asset.getName().indexOf(":") == - 1) {
  38. renameAsset(node, asset);
  39. Enumeration children = node.children();
  40. while (children.hasMoreElements()) {
  41. processChild((TreeNode) children.nextElement());
  42. }
  43. }
  44. }
  45. /**
  46. * @param node It had better be a DefaultMutableTreeNode and it had better
  47. * contain an Asset as the user object. At the time of this writing, this
  48. * was true.
  49. */
  50. private void renameAsset(final TreeNode node, IAsset asset) {
  51. if (asset instanceof Asset && node instanceof DefaultMutableTreeNode) {
  52. Enumeration children = node.children();
  53. while (children.hasMoreElements()) {
  54. DefaultMutableTreeNode child = (DefaultMutableTreeNode) children.nextElement();
  55. TldXmlTag uo = (TldXmlTag) child.getUserObject();
  56. if ("name".equals(uo.getName())) {
  57. ((Asset) asset).setName(uo.getCharacters());
  58. ((DefaultMutableTreeNode) node).setUserObject(asset);
  59. }
  60. }
  61. }
  62. }
  63. // Overridden so name and type sorting work correctly for these files.
  64. // "original name" is the original name of the xml tag, like "tag-file" or "function".
  65. // "short string" is the name provided by the child element named "name". So sort
  66. // by name uses "short string" and sort by type uses "original name".
  67. @Override
  68. protected Comparator<DefaultMutableTreeNode> getSorter() {
  69. return new Comparator<DefaultMutableTreeNode>() {
  70. public int compare(DefaultMutableTreeNode tna, DefaultMutableTreeNode tnb) {
  71. int sortBy = getSortBy();
  72. switch (sortBy) { // NOPMD, no breaks are necessary here
  73. case SORT_BY_LINE:
  74. Integer my_line = new Integer(((TldXmlTag)tna.getUserObject()).getStart().getOffset());
  75. Integer other_line = new Integer(((TldXmlTag)tnb.getUserObject()).getStart().getOffset());
  76. return my_line.compareTo(other_line) * (sortDown ? 1 : -1);
  77. case SORT_BY_TYPE:
  78. String my_on = ((TldXmlTag)tna.getUserObject()).getOriginalName().toLowerCase();
  79. String other_on = ((TldXmlTag)tnb.getUserObject()).getOriginalName().toLowerCase();
  80. return my_on.compareTo(other_on) * (sortDown ? 1 : -1);
  81. case SORT_BY_NAME:
  82. default:
  83. return compareNames(tna, tnb);
  84. }
  85. }
  86. private int compareNames(DefaultMutableTreeNode tna, DefaultMutableTreeNode tnb) {
  87. // sort by name
  88. String my_name = ((TldXmlTag)tna.getUserObject()).getShortString().toLowerCase();
  89. String other_name = ((TldXmlTag)tnb.getUserObject()).getShortString().toLowerCase();
  90. return my_name.compareTo(other_name) * (sortDown ? 1 : -1);
  91. }
  92. } ;
  93. }
  94. @Override
  95. protected ExpansionModel createExpansionModel() {
  96. ExpansionModel em = new ExpansionModel();
  97. em.add(); // root (filename node)
  98. em.add(); // main node
  99. for (int i = 0; i < root.getChildAt(0).getChildCount(); i++) {
  100. em.inc(); // tag, function, etc, nodes
  101. }
  102. return em;
  103. }
  104. }