/plugins/JavaSideKick/tags/javasidekick-2-3-6/src/sidekick/property/PropertyParser.java

# · Java · 149 lines · 84 code · 17 blank · 48 comment · 11 complexity · 4e4a6fd7edd90d9aa966cbe92047a516 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.property;
  26. import java.io.StringReader;
  27. import java.util.*;
  28. import java.util.regex.Matcher;
  29. import java.util.regex.Pattern;
  30. import javax.swing.text.Position;
  31. import javax.swing.tree.DefaultMutableTreeNode;
  32. import org.gjt.sp.jedit.Buffer;
  33. import org.gjt.sp.jedit.View;
  34. import sidekick.SideKickParsedData;
  35. import sidekick.SideKickParser;
  36. import sidekick.util.*;
  37. import sidekick.property.parser.property.ParseException;
  38. import sidekick.property.parser.property.Property;
  39. import sidekick.property.parser.property.Token;
  40. import errorlist.DefaultErrorSource;
  41. import errorlist.ErrorSource;
  42. public class PropertyParser extends SideKickParser {
  43. private static final String NAME = "properties";
  44. private View currentView = null;
  45. public static boolean showAll = false;
  46. public PropertyParser() {
  47. super( NAME );
  48. }
  49. public void parse() {
  50. if ( currentView != null ) {
  51. parse( currentView.getBuffer(), null );
  52. }
  53. }
  54. /**
  55. * Parse the contents of the given buffer.
  56. * @param buffer the buffer to parse
  57. * @param errorSource where to send any error messages
  58. * @return data for the tree
  59. */
  60. public SideKickParsedData parse( Buffer buffer, DefaultErrorSource errorSource ) {
  61. String filename = buffer.getPath();
  62. SideKickParsedData parsedData = new PropertySideKickParsedData( filename );
  63. DefaultMutableTreeNode root = parsedData.root;
  64. StringReader reader = new StringReader( buffer.getText( 0, buffer.getLength() ) );
  65. try {
  66. /* create the property parser property Property Parser -- I think
  67. this thing is going to parse some properties...! */
  68. sidekick.property.parser.property.PropertyParser parser = new sidekick.property.parser.property.PropertyParser( reader );
  69. // this makes the locations returned by the parser more accurate
  70. parser.setTabSize( buffer.getTabSize() );
  71. /* get the properties as Property objects, convert them to SideKick Assets,
  72. and add them to the tree */
  73. List<Property> properties = parser.Properties();
  74. for ( Property property : properties ) {
  75. DefaultMutableTreeNode node = new DefaultMutableTreeNode( property );
  76. root.add( node );
  77. }
  78. ElementUtil.convert(buffer, root);
  79. handleErrors( buffer, errorSource, parser.getExceptions() );
  80. }
  81. catch ( Exception e ) {
  82. //e.printStackTrace();
  83. }
  84. finally {
  85. reader.close();
  86. }
  87. return parsedData;
  88. }
  89. /* the parser accumulates errors as it parses. This method passed them all
  90. to the ErrorList plugin. */
  91. private void handleErrors( Buffer buffer, DefaultErrorSource errorSource, List<ParseException> errors ) {
  92. for ( ParseException pe : errors ) {
  93. Location loc = getExceptionLocation( pe );
  94. errorSource.addError( ErrorSource.ERROR, buffer.getPath(), loc.line, loc.column, loc.column, pe.getMessage() );
  95. }
  96. }
  97. /**
  98. * @return attempts to return a Location indicating the location of a parser
  99. * exception. If the ParseException contains a Token reference, all is well,
  100. * otherwise, this method attempts to parse the message string for the
  101. * exception location.
  102. */
  103. private Location getExceptionLocation( ParseException pe ) {
  104. Token t = pe.currentToken;
  105. if ( t != null ) {
  106. return new Location( t.next.beginLine - 1, t.next.beginColumn );
  107. }
  108. // ParseException message look like: "Parse error at line 116, column 5. Encountered: }"
  109. try {
  110. Pattern p = Pattern.compile( "(.*?)(\\d+)(.*?)(\\d+)(.*?)" );
  111. Matcher m = p.matcher( pe.getMessage() );
  112. if ( m.matches() ) {
  113. String ln = m.group( 2 );
  114. String cn = m.group( 4 );
  115. int line_number = -1;
  116. int column_number = 0;
  117. if ( ln != null )
  118. line_number = Integer.parseInt( ln );
  119. if ( cn != null )
  120. column_number = Integer.parseInt( cn );
  121. return line_number > -1 ? new Location( line_number - 1, column_number ) : null;
  122. }
  123. return new Location( 0, 0 );
  124. }
  125. catch ( Exception e ) {
  126. return new Location( 0, 0 );
  127. }
  128. }
  129. }