PageRenderTime 46ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/XML/sidekick/html/HtmlParser.java

#
Java | 186 lines | 97 code | 24 blank | 65 comment | 9 complexity | f9863e7aec2f010604e6ede7aef14b17 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. Copyright (c) 2006, Dale Anson
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. * Neither the name of the <ORGANIZATION> nor the names of its contributors
  12. may be used to endorse or promote products derived from this software without
  13. specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package sidekick.html;
  26. import java.io.StringReader;
  27. import java.util.*;
  28. import javax.swing.tree.DefaultMutableTreeNode;
  29. import errorlist.*;
  30. import org.gjt.sp.jedit.Buffer;
  31. import org.gjt.sp.jedit.EBComponent;
  32. import org.gjt.sp.jedit.EBMessage;
  33. import org.gjt.sp.jedit.EditBus;
  34. import org.gjt.sp.jedit.EditPane;
  35. import org.gjt.sp.jedit.View;
  36. import org.gjt.sp.jedit.jEdit;
  37. import org.gjt.sp.jedit.msg.PropertiesChanged;
  38. import sidekick.SideKickParsedData;
  39. import sidekick.util.*;
  40. import sidekick.html.parser.html.HtmlCollector;
  41. import sidekick.html.parser.html.HtmlDocument;
  42. import sidekick.html.parser.html.HtmlTreeBuilder;
  43. import xml.parser.XmlParser;
  44. /**
  45. * @author Dale Anson
  46. * @version $Revision: 18099 $
  47. */
  48. public class HtmlParser extends XmlParser implements EBComponent {
  49. private View currentView = null;
  50. public static boolean showAll = true;
  51. /** Constructor for HtmlParser */
  52. public HtmlParser() {
  53. super("html");
  54. }
  55. /**
  56. * Description of the Method
  57. *
  58. * @param editPane
  59. */
  60. public void activate(EditPane editPane) {
  61. super.activate(editPane);
  62. currentView = editPane.getView();
  63. EditBus.addToBus(this);
  64. }
  65. /**
  66. * Description of the Method
  67. *
  68. * @param editPane
  69. */
  70. public void deactivate(EditPane editPane) {
  71. super.deactivate(editPane);
  72. EditBus.removeFromBus(this);
  73. }
  74. /**
  75. * Description of the Method
  76. *
  77. * @param msg
  78. */
  79. public void handleMessage(EBMessage msg) {
  80. if (msg instanceof PropertiesChanged) {
  81. parse();
  82. }
  83. }
  84. /** Description of the Method */
  85. public void parse() {
  86. if (currentView != null) {
  87. parse(currentView.getBuffer(), null);
  88. }
  89. }
  90. /**
  91. * Parse the contents of the given buffer.
  92. *
  93. * @param buffer the buffer to parse
  94. * @param errorSource
  95. * @return Description of the Returned Value
  96. */
  97. public SideKickParsedData parse(Buffer buffer, DefaultErrorSource errorSource) {
  98. String filename = buffer.getPath();
  99. SideKickParsedData parsedData = new HtmlSideKickParsedData(filename, buffer);
  100. DefaultMutableTreeNode root = parsedData.root;
  101. StringReader reader = new StringReader(buffer.getText(0, buffer.getLength()));
  102. HtmlTreeBuilder builder = null;
  103. try {
  104. // create the parser
  105. sidekick.html.parser.html.HtmlParser parser = new sidekick.html.parser.html.HtmlParser(reader);
  106. // set tab size so that the parser can accurately calculate line and column positions
  107. parser.setTabSize(buffer.getTabSize());
  108. // parse the buffer
  109. HtmlDocument document = parser.HtmlDocument();
  110. // set display options
  111. document.setShowBrackets(jEdit.getBooleanProperty(
  112. "options.sidekick.html.showBrackets", true));
  113. document.setShowTagAttributes(jEdit.getBooleanProperty(
  114. "options.sidekick.html.showTagAttributes", true));
  115. document.setShowCoreAttributes(jEdit.getBooleanProperty(
  116. "options.sidekick.html.showCoreAttributes", true));
  117. document.setShowLangAttributes(jEdit.getBooleanProperty(
  118. "options.sidekick.html.showLangAttributes", true));
  119. document.setShowScriptAttributes(jEdit.getBooleanProperty(
  120. "options.sidekick.html.showScriptAttributes", true));
  121. document.setShowJspTags(jEdit.getBooleanProperty(
  122. "options.sidekick.html.showJspElements", true));
  123. // collect and clean
  124. document.accept(new HtmlCollector());
  125. // make a tree
  126. builder = new HtmlTreeBuilder(root);
  127. builder.setShowAll(jEdit.getBooleanProperty(
  128. "options.sidekick.html.showAllElements", true));
  129. builder.setShowAll(showAll);
  130. builder.setBuffer(buffer);
  131. builder.setErrorSource(errorSource);
  132. document.accept(builder);
  133. // need to convert the HtmlDocument.HtmlElements that are currently
  134. // the user objects in the tree nodes to SideKick Assets
  135. ElementUtil.convert(buffer, root);
  136. if ( !buffer.isDirty() && errorSource != null ) {
  137. /* only handle errors when buffer is saved. Otherwise, there will be a lot
  138. of spurious errors shown when code completion is on and the user is in the
  139. middle of typing something. */
  140. List<ParseError> parseErrors = parser.getParseErrors();
  141. for (ParseError pe : parseErrors) {
  142. String message = pe.message;
  143. Range range = pe.range;
  144. // addError is lame -- what if the error spans more than one line?
  145. // Need to just deal with it...
  146. if (range.endLine != range.startLine) {
  147. range.endColumn = range.startColumn;
  148. }
  149. errorSource.addError( ErrorSource.ERROR, filename, range.startLine, range.startColumn, range.endColumn, message );
  150. }
  151. }
  152. }
  153. catch (Exception e) {
  154. errorSource.addError(ErrorSource.ERROR, filename, 0, 0, 0, e.getMessage());
  155. e.printStackTrace();
  156. }
  157. finally {
  158. reader.close();
  159. }
  160. return parsedData;
  161. }
  162. }