001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.test.lib;
021:
022: import java.io.File;
023: import java.io.IOException;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.logging.Level;
028: import java.util.logging.Logger;
029: import javax.swing.text.StyledDocument;
030: import org.netbeans.api.languages.ParserManager;
031: import org.netbeans.api.lexer.Token;
032: import org.netbeans.api.lexer.TokenHierarchy;
033: import org.netbeans.api.lexer.TokenSequence;
034: import org.netbeans.editor.BaseDocument;
035: import org.openide.cookies.EditorCookie;
036: import org.openide.filesystems.FileUtil;
037: import org.openide.loaders.DataObject;
038: import org.openide.util.Exceptions;
039:
040: /**
041: *
042: * @author Jindrich Sedek
043: */
044: public class DumpTokens {
045:
046: private File file = null;
047: private String str = null;
048: private List<Token> tokens = null;
049:
050: public DumpTokens(File file) {
051: this .file = file;
052: }
053:
054: public String getTokenString() {
055: if (str == null) {
056: Logger.getLogger(DumpTokens.class.getName()).info(
057: "Getting token string");
058: Iterator<Token> iterator = getTokens().iterator();
059: while (iterator.hasNext()) {
060: Token token = iterator.next();
061: String next = token.id().name() + ":"
062: + token.text().toString() + "\n";
063: if (str == null) {
064: str = next;
065: } else {
066: str = str.concat(next);
067: }
068: }
069: }
070: return str;
071: }
072:
073: private List<Token> getTokens() {
074: if (tokens == null) {
075: try {
076: tokens = dumpTokens();
077: } catch (IOException e) {
078: AssertionError error = new AssertionError(
079: "DUMPING ERROR");
080: error.initCause(e);
081: throw error;
082: }
083: }
084: return tokens;
085: }
086:
087: @SuppressWarnings("unchecked")
088: private List<Token> dumpTokens() throws IOException {
089: Logger.getLogger(DumpTokens.class.getName()).info(
090: "DUMPING TOKNES");
091: DataObject dataObj = DataObject.find(FileUtil
092: .toFileObject(file));
093: EditorCookie ed = dataObj.getCookie(EditorCookie.class);
094:
095: StyledDocument sDoc = ed.openDocument();
096: BaseDocument doc = (BaseDocument) sDoc;
097: ParserManager parser = ParserManager.get(doc);
098: while (parser.getState() != ParserManager.State.OK) {// wait parsing finished
099: try {
100: Thread.sleep(1000);
101: Logger.getLogger(DumpTokens.class.getName()).log(
102: Level.INFO, "Waiting for parser");
103: } catch (InterruptedException ex) {
104: Exceptions.printStackTrace(ex);
105: }
106: }
107: TokenHierarchy th = null;
108: TokenSequence ts = null;
109: int roundCount = 0;
110: while ((th == null) || (ts == null)) {
111: th = TokenHierarchy.get(doc);
112: if (th != null) {
113: ts = th.tokenSequence();
114: }
115: roundCount++;
116: if (roundCount > 50) {
117: throw new AssertionError(
118: "IMPOSSIBLE TO GET TOKEN HIERARCHY "
119: + roundCount + "times");
120: }
121: try {
122: Thread.sleep(1000);
123: } catch (InterruptedException interruptedException) {
124: interruptedException.printStackTrace();
125: }
126:
127: }
128: try {
129: List<Token> tok = dumpTokens(ts);
130: return tok;
131: } catch (Exception e) {
132: e.printStackTrace();
133: }
134: return null;
135: }
136:
137: private List<Token> dumpTokens(TokenSequence ts) {
138: Logger.getLogger(DumpTokens.class.getName()).info(
139: "PARSING TOKEN SEQUENCE");
140: List<Token> result = null;
141: if (ts == null) {
142: throw new AssertionError("No TOKEN SEQUENCE");
143: }
144: ts.move(0);
145:
146: if (result == null) {
147: result = new ArrayList<Token>();
148: }
149: while (ts.moveNext()) {
150: Token token = ts.token();
151: if (ts.embedded() != null) {
152: List<Token> emb = dumpTokens(ts.embedded());
153: if (emb != null) {
154: result.addAll(emb);
155: } else {
156: result.add(token);
157: }
158: } else {
159: result.add(token);
160: }
161: }
162: return result;
163: }
164:
165: }
|