001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039: package org.netbeans.modules.html.editor.gsf;
040:
041: import java.util.HashMap;
042: import java.util.Map;
043: import java.util.Set;
044: import org.netbeans.editor.ext.html.parser.SyntaxElement.TagAttribute;
045: import org.netbeans.modules.gsf.api.ColoringAttributes;
046: import org.netbeans.modules.gsf.api.CompilationInfo;
047: import org.netbeans.modules.gsf.api.OffsetRange;
048: import org.netbeans.modules.gsf.api.ParserResult;
049: import org.netbeans.modules.gsf.api.SemanticAnalyzer;
050: import org.netbeans.modules.gsf.api.TranslatedSource;
051: import org.netbeans.modules.editor.html.HTMLKit;
052:
053: /**
054: *
055: * @author marek
056: */
057: public class HtmlSemanticAnalyzer implements SemanticAnalyzer {
058:
059: private boolean cancelled;
060: private Map<OffsetRange, ColoringAttributes> semanticHighlights;
061:
062: public Map<OffsetRange, ColoringAttributes> getHighlights() {
063: return semanticHighlights;
064: }
065:
066: public void cancel() {
067: cancelled = true;
068: }
069:
070: public void run(CompilationInfo ci) throws Exception {
071:
072: if (cancelled) {
073: return;
074: }
075:
076: final Map<OffsetRange, ColoringAttributes> highlights = new HashMap<OffsetRange, ColoringAttributes>();
077:
078: ParserResult presult = ci.getEmbeddedResults(
079: HTMLKit.HTML_MIME_TYPE).iterator().next();
080: final TranslatedSource source = presult.getTranslatedSource();
081:
082: //just a test - highlight all tags' ids
083: Set<TagAttribute> ids = ((HtmlParserResult) presult)
084: .elementsIds();
085: for (TagAttribute ta : ids) {
086: OffsetRange range = new AstOffsetRange(ta.getValueOffset(),
087: ta.getValueOffset() + ta.getValueLength(), source);
088: highlights.put(range, ColoringAttributes.METHOD);
089: }
090:
091: semanticHighlights = highlights;
092:
093: }
094:
095: public static class AstOffsetRange extends OffsetRange {
096:
097: private TranslatedSource source;
098:
099: public AstOffsetRange(int start, int end,
100: TranslatedSource source) {
101: super (start, end);
102: this .source = source;
103: }
104:
105: public int getStart() {
106: return source == null ? super .getStart() : source
107: .getLexicalOffset(super .getStart());
108: }
109:
110: public int getEnd() {
111: return source == null ? super.getEnd() : source
112: .getLexicalOffset(super.getEnd());
113: }
114:
115: }
116:
117: }
|