/plugins/XML/tags/release-2-6-1/sidekick/css/CSS2SideKickParser.java

# · Java · 203 lines · 104 code · 29 blank · 70 comment · 17 complexity · 67464436a039d9ce68ac6f19711d97cb MD5 · raw file

  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.css;
  26. import java.io.StringReader;
  27. import java.util.*;
  28. import javax.swing.tree.DefaultMutableTreeNode;
  29. import org.gjt.sp.jedit.Buffer;
  30. import org.gjt.sp.jedit.EditPane;
  31. import org.gjt.sp.jedit.View;
  32. import org.gjt.sp.jedit.jEdit;
  33. import errorlist.DefaultErrorSource;
  34. import errorlist.ErrorSource;
  35. import sidekick.*;
  36. import sidekick.util.*;
  37. import sidekick.css.parser.CSSNode;
  38. import sidekick.css.parser.CSS3Parser;
  39. /**
  40. * @author Dale Anson
  41. * @version $Revision$
  42. */
  43. public class CSS2SideKickParser extends SideKickParser {
  44. private View currentView = null;
  45. public static boolean showAll = true;
  46. private int lineOffset = 0;
  47. public CSS2SideKickParser() {
  48. super( "css" );
  49. }
  50. /**
  51. * If called by another parser to parse part of a file (for example, to parse
  52. * a style tag in an html document), this can be set to the offset of the
  53. * style tag so that the node locations can be set correctly.
  54. */
  55. public void setLineOffset( int offset ) {
  56. if ( offset > 0 ) {
  57. lineOffset = offset;
  58. }
  59. }
  60. public void parse() {
  61. if ( currentView != null ) {
  62. parse( currentView.getBuffer(), null );
  63. }
  64. }
  65. /**
  66. * Parse the contents of the given buffer. This is the standard entry point
  67. * and will cause the entire text of the buffer to be parsed.
  68. *
  69. * @param buffer the buffer to parse
  70. * @param errorSource where to send errors
  71. * @return Description of the Returned Value
  72. */
  73. public SideKickParsedData parse( Buffer buffer, DefaultErrorSource errorSource ) {
  74. setLineOffset( 0 );
  75. return parse( buffer, buffer.getText( 0, buffer.getLength() ), errorSource );
  76. }
  77. /**
  78. * Parse the contents of the given text. This is the entry point to use when
  79. * only a portion of the buffer text is to be parsed. Note that <code>setLineOffset</code>
  80. * should be called prior to calling this method, otherwise, tree node positions
  81. * may be off.
  82. *
  83. * @param buffer the buffer to parse
  84. * @param errorSource where to send errors
  85. * @return Description of the Returned Value
  86. */
  87. public SideKickParsedData parse( Buffer buffer, String text, DefaultErrorSource errorSource ) {
  88. String filename = buffer.getPath();
  89. SideKickParsedData parsedData = new CSSParsedData( buffer.getName() );
  90. DefaultMutableTreeNode root = parsedData.root;
  91. StringReader reader = new StringReader( text );
  92. try {
  93. // create parser
  94. CSS3Parser parser = new CSS3Parser( reader );
  95. // set line offset, the parser uses this to adjust line numbers in the
  96. // case of a partial file, like when the stylesheet is embedded inside an
  97. // html document
  98. parser.setLineOffset( lineOffset );
  99. // set tab size so that the parser can accurately calculate line and
  100. // column positions
  101. parser.setTabSize( buffer.getTabSize() );
  102. // should proprieatary properties be considered an error? True or
  103. // false, the file will parse, if set to true and there are
  104. // proprietary property names, then an error will be passed to
  105. // ErrorList.
  106. parser.setProprietaryAsError( jEdit.getBooleanProperty( CssSideKickPlugin.OPTION_PREFIX + "showProprietaryAsError" ) );
  107. // parse the text
  108. CSSNode ss = parser.styleSheet();
  109. // make a tree
  110. root.setUserObject( ss );
  111. addTreeNodes( root, ss );
  112. // need to convert the CSSNodes that are currently the user objects
  113. // in the tree nodes to SideKick Assets
  114. ElementUtil.convert( buffer, root );
  115. if ( !buffer.isDirty() && errorSource != null ) {
  116. /* only handle errors when buffer is saved. Otherwise, there will be a lot
  117. of spurious errors shown when code completion is on and the user is in the
  118. middle of typing something. */
  119. List<ParseError> parseErrors = parser.getParseErrors();
  120. for ( ParseError pe : parseErrors ) {
  121. String message = pe.message;
  122. Range range = pe.range;
  123. // addError is lame -- what if the error spans more than one line?
  124. // Need to just deal with it...
  125. if ( range.endLine != range.startLine ) {
  126. range.endColumn = range.startColumn;
  127. }
  128. errorSource.addError( ErrorSource.ERROR, filename, range.startLine, range.startColumn, range.endColumn, message );
  129. }
  130. }
  131. }
  132. catch ( Exception e ) {
  133. e.printStackTrace();
  134. }
  135. finally {
  136. reader.close();
  137. }
  138. return parsedData;
  139. }
  140. private void addTreeNodes( DefaultMutableTreeNode root, CSSNode ss ) {
  141. if ( ss.hasChildren() ) {
  142. for ( Iterator it = ss.getChildren().iterator(); it.hasNext(); ) {
  143. CSSNode cssChild = ( CSSNode ) it.next();
  144. if ( cssChild != null ) {
  145. DefaultMutableTreeNode dmtNode = new DefaultMutableTreeNode( cssChild );
  146. root.add( dmtNode );
  147. addTreeNodeChildren( dmtNode, cssChild );
  148. }
  149. }
  150. }
  151. }
  152. private void addTreeNodeChildren( DefaultMutableTreeNode dmtNode, CSSNode cssNode ) {
  153. if ( cssNode.hasChildren() ) {
  154. for ( Iterator it = cssNode.getChildren().iterator(); it.hasNext(); ) {
  155. CSSNode cssChild = ( CSSNode ) it.next();
  156. if ( cssChild != null ) {
  157. DefaultMutableTreeNode dmtChild = new DefaultMutableTreeNode( cssChild );
  158. dmtNode.add( dmtChild );
  159. addTreeNodeChildren( dmtChild, cssChild );
  160. }
  161. }
  162. }
  163. }
  164. public boolean supportsCompletion() {
  165. return true;
  166. }
  167. public boolean canCompleteAnywhere() {
  168. return true;
  169. }
  170. public SideKickCompletion complete( EditPane editPane, int caret ) {
  171. CompletionRequest cr = new CompletionRequest( editPane, caret );
  172. return cr.getSideKickCompletion();
  173. }
  174. }