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.io.IOException;
042: import java.util.ArrayList;
043: import java.util.Collections;
044: import java.util.EnumSet;
045: import java.util.HashMap;
046: import java.util.List;
047: import java.util.Map;
048: import java.util.Set;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051: import javax.swing.text.BadLocationException;
052: import org.netbeans.editor.BaseDocument;
053: import org.netbeans.editor.Utilities;
054: import org.netbeans.editor.ext.html.parser.AstNode;
055: import org.netbeans.editor.ext.html.parser.AstNodeUtils;
056: import org.netbeans.editor.ext.html.parser.AstNodeVisitor;
057: import org.netbeans.modules.gsf.api.CompilationInfo;
058: import org.netbeans.modules.gsf.api.ElementHandle;
059: import org.netbeans.modules.gsf.api.ElementKind;
060: import org.netbeans.modules.gsf.api.HtmlFormatter;
061: import org.netbeans.modules.gsf.api.Modifier;
062: import org.netbeans.modules.gsf.api.OffsetRange;
063: import org.netbeans.modules.gsf.api.ParserResult;
064: import org.netbeans.modules.gsf.api.StructureItem;
065: import org.netbeans.modules.gsf.api.StructureScanner;
066: import org.netbeans.modules.gsf.api.TranslatedSource;
067: import org.netbeans.modules.editor.html.HTMLKit;
068: import org.openide.util.Exceptions;
069:
070: /**
071: *
072: * @author marek
073: */
074: public class HtmlStructureScanner implements StructureScanner {
075:
076: private static final Logger LOGGER = Logger
077: .getLogger(HtmlStructureScanner.class.getName());
078: private static final boolean LOG = LOGGER.isLoggable(Level.FINE);
079: private HtmlFormatter formatter;
080:
081: public List<? extends StructureItem> scan(
082: final CompilationInfo info, HtmlFormatter formatter) {
083:
084: ParserResult presult = info.getEmbeddedResults(
085: HTMLKit.HTML_MIME_TYPE).iterator().next();
086: final TranslatedSource source = presult.getTranslatedSource();
087: AstNode root = ((HtmlParserResult) presult).root();
088:
089: if (LOG) {
090: LOGGER.log(Level.FINE, "HTML parser tree output:");
091: LOGGER.log(Level.FINE, root.toString());
092: }
093:
094: //return the root children
095: List<StructureItem> elements = new ArrayList<StructureItem>(1);
096: elements.addAll(new CSSStructureItem(new HtmlElementHandle(
097: root, info), source).getNestedItems());
098:
099: return elements;
100:
101: }
102:
103: public Map<String, List<OffsetRange>> folds(CompilationInfo info) {
104: try {
105: //so far the css parser always parses the whole css content
106: ParserResult presult = info.getEmbeddedResults("text/html")
107: .iterator().next();
108: final TranslatedSource source = presult
109: .getTranslatedSource();
110: final BaseDocument doc = (BaseDocument) info.getDocument();
111: AstNode root = ((HtmlParserResult) presult).root();
112:
113: final Map<String, List<OffsetRange>> folds = new HashMap<String, List<OffsetRange>>();
114: final List<OffsetRange> foldRange = new ArrayList<OffsetRange>();
115:
116: AstNodeVisitor foldsSearch = new AstNodeVisitor() {
117:
118: public void visit(AstNode node) {
119: if (node.type() == AstNode.NodeType.TAG
120: || node.type() == AstNode.NodeType.COMMENT) {
121: try {
122: int so = documentPosition(node
123: .startOffset(), source);
124: int eo = documentPosition(node.endOffset(),
125: source);
126:
127: if (Utilities.getLineOffset(doc, so) < Utilities
128: .getLineOffset(doc, eo)) {
129: //do not creare one line folds
130: //XXX this logic could possibly seat in the GSF folding impl.
131: foldRange.add(new OffsetRange(so, eo));
132: }
133: } catch (BadLocationException ex) {
134: Exceptions.printStackTrace(ex);
135: }
136: }
137: }
138: };
139: AstNodeUtils.visitChildren(root, foldsSearch);
140: folds.put("codeblocks", foldRange);
141:
142: return folds;
143: } catch (IOException ex) {
144: Exceptions.printStackTrace(ex);
145: }
146:
147: return null;
148:
149: }
150:
151: public static int documentPosition(int astOffset,
152: TranslatedSource source) {
153: return source == null ? astOffset : source
154: .getLexicalOffset(astOffset);
155: }
156:
157: private static final class CSSStructureItem implements
158: StructureItem {
159:
160: private TranslatedSource source;
161: private HtmlElementHandle handle;
162:
163: private CSSStructureItem(HtmlElementHandle handle,
164: TranslatedSource source) {
165: this .handle = handle;
166: this .source = source;
167:
168: }
169:
170: public String getName() {
171: return handle.getName();
172: }
173:
174: public String getHtml() {
175: return getName();
176: }
177:
178: public ElementHandle getElementHandle() {
179: return handle;
180: }
181:
182: public ElementKind getKind() {
183: return ElementKind.TAG;
184: }
185:
186: public Set<Modifier> getModifiers() {
187: return Collections.emptySet();
188: }
189:
190: public boolean isLeaf() {
191: return handle.node().children().isEmpty();
192: }
193:
194: public List<? extends StructureItem> getNestedItems() {
195: List<StructureItem> list = new ArrayList<StructureItem>(
196: handle.node().children().size());
197: for (AstNode child : handle.node().children()) {
198: if (child.type() == AstNode.NodeType.TAG
199: || child.type() == AstNode.NodeType.UNMATCHED_TAG) {
200: HtmlElementHandle childHandle = new HtmlElementHandle(
201: child, handle.compilationInfo());
202: list.add(new CSSStructureItem(childHandle, source));
203: }
204: }
205: return list;
206: }
207:
208: public long getPosition() {
209: return HtmlStructureScanner.documentPosition(handle.node()
210: .startOffset(), source);
211: }
212:
213: public long getEndPosition() {
214: return HtmlStructureScanner.documentPosition(handle.node()
215: .endOffset(), source);
216: }
217:
218: }
219: }
|