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