Java Code Examples for org.netbeans.api.lexer.Token#length()
The following examples show how to use
org.netbeans.api.lexer.Token#length() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AttrImpl.java From netbeans with Apache License 2.0 | 6 votes |
private Node getFirstChildLocked(TokenSequence ts) { while (ts.moveNext()) { Token<XMLTokenId> t = ts.token(); if (t.id() == XMLTokenId.VALUE) { // fuzziness to relax minor tokenization changes CharSequence image = t.text(); if (image.length() == 1) { char test = image.charAt(0); if (test == '"' || test == '\'') { if (ts.moveNext()) { t = ts.token(); } else { return null; } } } if (t.id() == XMLTokenId.VALUE) { return new TextImpl(syntax, t, ts.offset(), ts.offset() + t.length(), this); } else { return null; } } } return null; }
Example 2
Source File: CompletionContext.java From netbeans with Apache License 2.0 | 6 votes |
/** * Skips to and reads the root Element. Reads root element's attributes, * so we can detect whether required namespace(s) are present. * * This MUST be called prior to readCurrentContent(). * @param s */ private void readRootElement(TokenSequence<XMLTokenId> seq) { seq.move(0); while (seq.moveNext()) { Token<XMLTokenId> t = seq.token(); XMLTokenId id = t.id(); if (id == XMLTokenId.TAG && t.length() > 1) { int startOffset = seq.offset(); readTagContent(seq); // reassign stuff: copyToRoot(); rootTagStartOffset = startOffset; rootAttrInsertOffset = startOffset + t.length(); if (t.text().charAt(t.length() - 1) == '>') { rootAttrInsertOffset--; if (t.length() > 2 && t.text().charAt(t.length() - 2) == '/') { rootAttrInsertOffset--; } } findRootInsertionPoint(); return; } } }
Example 3
Source File: XMLBraceMatcher.java From netbeans with Apache License 2.0 | 6 votes |
/** * For CDATA and XML comments, there is no start and end tokens differentiator. * XML lexer just gives us one token and we have to find the origin in there. */ private int[] findGenericOrigin(TokenSequence ts, String startTag, String endTag) { Token token = ts.token(); String text = token.text().toString(); int start = ts.offset(); int end = start+startTag.length(); //if context.getSearchOffset() is inside start tag such as "<!--" if(text.startsWith(startTag) && start <= searchOffset && end > searchOffset) return new int[] {start, end}; //if context.getSearchOffset() is inside end tag such as "-->" start = ts.offset() + token.length()-endTag.length(); end = start+endTag.length(); if(text.toString().endsWith(endTag) && start <= searchOffset && end >= searchOffset) return new int[] {start, end}; //if none works return null return null; }
Example 4
Source File: XMLBraceMatcher.java From netbeans with Apache License 2.0 | 6 votes |
/** * For CDATA and XML comments, there is no start and end tokens differentiator. * XML lexer just gives us one token and we have to find the origin in there. */ private int[] findGenericMatch(TokenSequence ts, String startTag, String endTag) { Token token = ts.token(); int offset = ts.offset(); int start = offset; int end = start+startTag.length(); //when the cursor is inside "<!--" return the end position for "-->" if(token.text().toString().startsWith(startTag) && start <= searchOffset && end > searchOffset) { return findGenericMatchForward(ts, endTag); } //when the cursor is inside "-->" return the start position for "<!--" start = offset + token.length()-endTag.length(); end = start+endTag.length(); if(token.text().toString().endsWith(endTag) && start <= searchOffset && end >= searchOffset) { return findGenericMatchBackward(ts, startTag); } //if none works return null return null; }
Example 5
Source File: LexUtilities.java From netbeans with Apache License 2.0 | 6 votes |
public static OffsetRange findEnd(BaseDocument doc, TokenSequence<GroovyTokenId> ts) { int balance = 0; while (ts.moveNext()) { Token<GroovyTokenId> token = ts.token(); TokenId id = token.id(); if (isBeginToken(id, doc, ts)) { balance--; } else if (id == GroovyTokenId.RBRACE) { if (balance == 0) { return new OffsetRange(ts.offset(), ts.offset() + token.length()); } balance++; } } return OffsetRange.NONE; }
Example 6
Source File: LexerUtilities.java From netbeans with Apache License 2.0 | 6 votes |
/** * Search forwards in the token sequence until a matching closing token is * found so keeps track of nested pairs of up-down eg (()) is ignored if * we're searching for a ) * * @param ts the TokenSequence set to the position after an up * @param up the opening token eg { or [ * @param down the closing token eg } or ] * @return the Range of closing token in our case 1 char */ public static OffsetRange findBwd(TokenSequence<? extends SQLTokenId> ts, int up, int down) { int balance = 0; while (ts.movePrevious()) { Token<? extends SQLTokenId> token = ts.token(); if (token.id().ordinal() == up) { if (balance == 0) { return new OffsetRange(ts.offset(), ts.offset() + token.length()); } balance++; } else if (token.id().ordinal() == down) { balance--; } } return OffsetRange.NONE; }
Example 7
Source File: AssignComments.java From netbeans with Apache License 2.0 | 6 votes |
private int countIndent(TokenSequence<JavaTokenId> seq, Tree tree) { int st = (int)positions.getStartPosition(unit, tree); int save = seq.offset(); int nl = -1; seq.move(st); while (seq.movePrevious()) { Token<JavaTokenId> tukac = seq.token(); if (tukac.id() != JavaTokenId.WHITESPACE) { if (tukac.id() == JavaTokenId.LINE_COMMENT) { nl = seq.offset() + tukac.length(); } break; } nl = tukac.text().toString().lastIndexOf('\n'); if (nl != -1) { break; } } seq.move(save); if (!seq.moveNext()) { seq.movePrevious(); } return nl == -1 ? -1 : st - nl; }
Example 8
Source File: ManifestHyperlinkProvider.java From netbeans with Apache License 2.0 | 5 votes |
public static int[] getIdentifierSpan(Document doc, int offset, Token[] token) { FileObject fo = getFileObject(doc); if (fo == null) { //do nothing if FO is not attached to the document - the goto would not work anyway: return null; } Project prj = FileOwnerQuery.getOwner(fo); if (prj == null) { return null; } NbModuleProvider module = prj.getLookup().lookup(NbModuleProvider.class); if (module == null) { return null; } TokenHierarchy<?> th = TokenHierarchy.get(doc); TokenSequence ts = th.tokenSequence(Language.find("text/x-manifest")); if (ts == null) return null; ts.move(offset); if (!ts.moveNext()) { return null; } Token t = ts.token(); if (findFile(fo, t.toString()) != null) { return new int [] { ts.offset(), ts.offset() + t.length() }; } return null; }
Example 9
Source File: TwigHtmlEmbeddingProvider.java From netbeans with Apache License 2.0 | 5 votes |
@Override public List<Embedding> getEmbeddings(Snapshot snapshot) { TokenHierarchy<CharSequence> th = TokenHierarchy.create(snapshot.getText(), TwigTopTokenId.language()); TokenSequence<TwigTopTokenId> sequence = th.tokenSequence(TwigTopTokenId.language()); if (sequence == null) { return Collections.emptyList(); } sequence.moveStart(); List<Embedding> embeddings = new ArrayList<>(); int offset = -1; int length = 0; while (sequence.moveNext()) { Token t = sequence.token(); if (t.id() == TwigTopTokenId.T_HTML) { if (offset < 0) { offset = sequence.offset(); } length += t.length(); } else if (offset >= 0) { embeddings.add(snapshot.create(offset, length, TARGET_MIME_TYPE)); embeddings.add(snapshot.create(GENERATED_CODE, TARGET_MIME_TYPE)); offset = -1; length = 0; } } if (offset >= 0) { embeddings.add(snapshot.create(offset, length, TARGET_MIME_TYPE)); } if (embeddings.isEmpty()) { return Collections.singletonList(snapshot.create("", TARGET_MIME_TYPE)); } else { return Collections.singletonList(Embedding.create(embeddings)); } }
Example 10
Source File: JavaReference.java From netbeans with Apache License 2.0 | 4 votes |
private void insideMember(TokenSequence<JavadocTokenId> jdts) { Token<JavadocTokenId> token; if (!jdts.moveNext() || JavadocTokenId.IDENT != (token = jdts.token()).id()) { return; } // member identifier member = token.text (); end = jdts.offset() + token.length(); // params part (int, String) if (!jdts.moveNext ()) return; token = jdts.token (); if (JavadocTokenId.OTHER_TEXT != token.id ()) return; CharSequence cs = token.text (); if (cs.length () == 0 || cs.charAt (0) != '(') { // no params return; } StringBuilder params = new StringBuilder(); while (jdts.offset() < tagEndPosition) { int len = tagEndPosition - jdts.offset(); cs = len > 0 ? token.text() : token.text().subSequence(0, len); if (token.id () == JavadocTokenId.IDENT) { JavaReference parameter = JavaReference.resolve ( jdts, jdts.offset(), jdts.offset() + len ); if (parameters == null) parameters = new ArrayList<JavaReference> (); parameters.add (parameter); if (parameter.fqn != null) { params.append(parameter.fqn); } else { params.append(cs); } } else { params.append(cs); } if (params.indexOf (")") > 0) break; if (!jdts.moveNext()) { break; } token = jdts.token(); } paramsText = parseParamString(params); }
Example 11
Source File: ApisupportHyperlinkProvider.java From netbeans with Apache License 2.0 | 4 votes |
public static int[] getIdentifierSpan(Document doc, int offset, Token<JavaTokenId>[] token) { FileObject fo = getFileObject(doc); if (fo == null) { //do nothing if FO is not attached to the document - the goto would not work anyway: return null; } Project prj = FileOwnerQuery.getOwner(fo); if (prj == null) { return null; } NbModuleProvider module = prj.getLookup().lookup(NbModuleProvider.class); if (module == null) { return null; } ((AbstractDocument) doc).readLock(); TokenHierarchy th = TokenHierarchy.get(doc); TokenSequence<JavaTokenId> ts = null; try { ts = SourceUtils.getJavaTokenSequence(th, offset); } finally { ((AbstractDocument) doc).readUnlock(); } if (ts == null) return null; ts.move(offset); if (!ts.moveNext()) return null; Token<JavaTokenId> t = ts.token(); boolean hasMessage = false; boolean hasNbBundle = false; if (USABLE_TOKEN_IDS.contains(t.id())) { for (int i = 0; i < 10; i++) { if (!ts.movePrevious()) { break; } Token<JavaTokenId> tk = ts.token(); if (TokenUtilities.equals("getMessage", tk.text())) { hasMessage = true; } else if (TokenUtilities.equals("NbBundle", tk.text())) { hasNbBundle = true; } } if (hasNbBundle && hasMessage) { ts.move(offset); ts.moveNext(); return new int [] {ts.offset(), ts.offset() + t.length()}; } } return null; }
Example 12
Source File: Utilities.java From netbeans with Apache License 2.0 | 4 votes |
/** * This method returns the part of the syntax tree to be highlighted. * It will be usually the class/method/variable identifier. */ public static TextSpan getUnderlineSpan(CompilationInfo info, Tree tree) { SourcePositions srcPos = info.getTrees().getSourcePositions(); int startOffset = (int) srcPos.getStartPosition(info.getCompilationUnit(), tree); int endOffset = (int) srcPos.getEndPosition(info.getCompilationUnit(), tree); Tree startSearchingForNameIndentifierBehindThisTree = null; if (TreeUtilities.CLASS_TREE_KINDS.contains(tree.getKind())) { startSearchingForNameIndentifierBehindThisTree = ((ClassTree) tree).getModifiers(); } else if (tree.getKind() == Tree.Kind.METHOD) { startSearchingForNameIndentifierBehindThisTree = ((MethodTree) tree).getReturnType(); } else if (tree.getKind() == Tree.Kind.VARIABLE) { startSearchingForNameIndentifierBehindThisTree = ((VariableTree) tree).getType(); } if (startSearchingForNameIndentifierBehindThisTree != null) { int searchStart = (int) srcPos.getEndPosition(info.getCompilationUnit(), startSearchingForNameIndentifierBehindThisTree); TokenSequence<JavaTokenId> tokenSequence = info.getTreeUtilities().tokensFor(tree); if (tokenSequence != null) { boolean eob = false; tokenSequence.move(searchStart); do { eob = !tokenSequence.moveNext(); } while (!eob && tokenSequence.token().id() != JavaTokenId.IDENTIFIER); if (!eob) { Token<JavaTokenId> identifier = tokenSequence.token(); startOffset = identifier.offset(info.getTokenHierarchy()); endOffset = startOffset + identifier.length(); } } } return new TextSpan(startOffset, endOffset); }
Example 13
Source File: JadeHtmlEmbeddingProvider.java From netbeans with Apache License 2.0 | 4 votes |
@Override public List<Embedding> getEmbeddings(Snapshot snapshot) { TokenHierarchy<?> th = snapshot.getTokenHierarchy(); TokenSequence<JadeTokenId> ts = th.tokenSequence(JadeTokenId.jadeLanguage()); if (ts == null) { LOGGER.log( Level.WARNING, "TokenHierarchy.tokenSequence(JadeTokenId.jadeLanguage()) == null " + "for static immutable Jade TokenHierarchy!\nFile = ''{0}'' ;snapshot mimepath=''{1}''", new Object[]{snapshot.getSource().getFileObject().getPath(), snapshot.getMimePath()}); return Collections.emptyList(); } ts.moveStart(); List<Embedding> embeddings = new ArrayList<>(); int from = -1; int len = 0; while (ts.moveNext()) { Token<JadeTokenId> token = ts.token(); if (token.id() == JadeTokenId.PLAIN_TEXT) { if (from < 0) { from = ts.offset(); } len += token.length(); } else { if (from >= 0) { embeddings.add(snapshot.create(from, len + 1, HTML_MIME_TYPE)); } from = -1; len = 0; } } if (from >= 0) { embeddings.add(snapshot.create(from, len, HTML_MIME_TYPE)); } if (embeddings.isEmpty()) { return Collections.emptyList(); } return Collections.singletonList(Embedding.create(embeddings)); }
Example 14
Source File: XmlLexerParser.java From netbeans with Apache License 2.0 | 4 votes |
@NbBundle.Messages({ "# {0} - attribute name", "ERR_DuplicateAttribute=Duplicate attribute: {0}" }) private boolean parseAttribute(Token<XMLTokenId> t) { String argName = t.text().toString(); boolean ignore; if (attrs.containsKey(argName)) { addError(ERR_DuplicateAttribute, ERR_DuplicateAttribute(argName)); ignore = true; } else { attrOffsets.put(argName, currentAttrOffsets = new int[] { seq.offset(), -1, -1, -1 }); attrNames.add(argName); attrs.put(argName, null); ignore = false; } consume(); skipWhitespace(); t = nextToken(); if (t == null || t.id() != XMLTokenId.OPERATOR) { markUnexpectedAttrToken(); return false; } consume(); skipWhitespace(); t = nextToken(); if (t == null ||t.id() != XMLTokenId.VALUE) { markUnexpectedAttrToken(); return false; } consume(); CharSequence s = t.text(); StringBuilder sb = null; int valStart = seq.offset(); int valEnd = seq.offset() + t.length(); char quote = s.charAt(0); int end; t = nextToken(); while (t != null && (t.id() == XMLTokenId.VALUE || t.id() == XMLTokenId.CHARACTER)) { valEnd = seq.offset() + t.length(); if (sb == null) { sb = new StringBuilder(); sb.append(s.toString()); } sb.append(t.text()); consume(); t = nextToken(); } end = valEnd; if (sb != null) { s = sb; } if (quote == '\'' || quote == '"') { // NOI18N if (s.charAt(s.length() - 1) == quote) { valStart++; // also handle opening quote with no content (yet) if (s.length() > 1) { valEnd--; s = s.subSequence(1, s.length() - 1); } else { s = ""; // NOI18N } } else if (t == null || t.id() == XMLTokenId.ERROR) { // strip at least 1st quote in case of an error s = s.subSequence(1, s.length()); valStart++; } } if (!ignore) { attrs.put(argName, s.toString()); int[] offsets = attrOffsets.get(argName); offsets[OFFSET_END] = end; offsets[OFFSET_VALUE_START] = valStart; offsets[OFFSET_VALUE_END] = valEnd; } return true; }
Example 15
Source File: CdiEditorAnalysisFactory.java From netbeans with Apache License 2.0 | 4 votes |
public static List<Integer> getElementPosition(CompilationInfo info, Tree tree){ SourcePositions srcPos = info.getTrees().getSourcePositions(); int startOffset = (int) srcPos.getStartPosition(info.getCompilationUnit(), tree); int endOffset = (int) srcPos.getEndPosition(info.getCompilationUnit(), tree); Tree startTree = null; if (TreeUtilities.CLASS_TREE_KINDS.contains(tree.getKind())){ startTree = ((ClassTree)tree).getModifiers(); } else if (tree.getKind() == Tree.Kind.METHOD){ startTree = ((MethodTree)tree).getReturnType(); } else if (tree.getKind() == Tree.Kind.VARIABLE){ startTree = ((VariableTree)tree).getType(); } if (startTree != null){ int searchStart = (int) srcPos.getEndPosition(info.getCompilationUnit(), startTree); TokenSequence<?> tokenSequence = info.getTreeUtilities().tokensFor(tree); if (tokenSequence != null){ boolean eob = false; tokenSequence.move(searchStart); do{ eob = !tokenSequence.moveNext(); } while (!eob && tokenSequence.token().id() != JavaTokenId.IDENTIFIER); if (!eob){ Token<?> identifier = tokenSequence.token(); startOffset = identifier.offset(info.getTokenHierarchy()); endOffset = startOffset + identifier.length(); } } } List<Integer> result = new ArrayList<Integer>(2); result.add(startOffset); result.add(endOffset ); return result; }
Example 16
Source File: JavaElementFoldVisitor.java From netbeans with Apache License 2.0 | 4 votes |
@Override @SuppressWarnings("fallthrough") public Object visitCompilationUnit(CompilationUnitTree node, Object p) { int importsStart = Integer.MAX_VALUE; int importsEnd = -1; TokenHierarchy<?> th = info.getTokenHierarchy(); TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language()); IMP: for (ImportTree imp : node.getImports()) { // eliminate invalid imports; erroenous imports at the start/end of import block // will not be folder. Tree qualIdent = imp.getQualifiedIdentifier(); if (qualIdent == null) { continue; } while (qualIdent.getKind() == Tree.Kind.MEMBER_SELECT) { MemberSelectTree mst = (MemberSelectTree)qualIdent; if (mst.getIdentifier().contentEquals("<error>")) { // NOI18N // ignore erroneous imports continue IMP; } qualIdent = mst.getExpression(); } // don't rely on Javac for the end position: in case of missing semicolon the import consumes all whitespace // including comments / javadocs up to the following declaration or text. Rather scan tokens and consume only the import // identifier + semi. int start = (int) sp.getStartPosition(cu, imp); int identPos = (int) sp.getStartPosition(cu, qualIdent); int end = identPos; boolean firstNewline = true; ts.move(identPos); IDENT: while (ts.moveNext()) { Token<JavaTokenId> tukac = ts.token(); switch (tukac.id()) { case IDENTIFIER: case DOT: case STAR: firstNewline = false; end = ts.offset() + tukac.length(); break; case SEMICOLON: end = (int) sp.getEndPosition(cu, imp); break IDENT; case WHITESPACE: { if (firstNewline) { int endl = tukac.text().toString().indexOf("\n"); // NOI18N if (endl > -1) { // remember the first newline after some ident/star/dot content end = ts.offset() + endl; firstNewline = true; } } // fall through } case LINE_COMMENT: case BLOCK_COMMENT: continue; default: break IDENT; } } if (importsStart > start) importsStart = start; if (end > importsEnd) { importsEnd = end; } } if (importsEnd != (-1) && importsStart != (-1)) { if (importsStart < initialCommentStopPos) { initialCommentStopPos = importsStart; } importsStart += 7/*"import ".length()*/; if (importsStart < importsEnd) { addFold(creator.createImportsFold(importsStart, importsEnd), importsStart); } } return super.visitCompilationUnit(node, p); }
Example 17
Source File: RestScanTask.java From netbeans with Apache License 2.0 | 4 votes |
static List<Integer> getElementPosition(CompilationInfo info, Tree tree){ SourcePositions srcPos = info.getTrees().getSourcePositions(); int startOffset = (int) srcPos.getStartPosition(info.getCompilationUnit(), tree); int endOffset = (int) srcPos.getEndPosition(info.getCompilationUnit(), tree); Tree startTree = null; if (TreeUtilities.CLASS_TREE_KINDS.contains(tree.getKind())){ startTree = ((ClassTree)tree).getModifiers(); } else if (tree.getKind() == Tree.Kind.METHOD){ startTree = ((MethodTree)tree).getReturnType(); } else if (tree.getKind() == Tree.Kind.VARIABLE){ startTree = ((VariableTree)tree).getType(); } if (startTree != null){ int searchStart = (int) srcPos.getEndPosition(info.getCompilationUnit(), startTree); TokenSequence<?> tokenSequence = info.getTreeUtilities().tokensFor(tree); if (tokenSequence != null){ boolean eob = false; tokenSequence.move(searchStart); do{ eob = !tokenSequence.moveNext(); } while (!eob && tokenSequence.token().id() != JavaTokenId.IDENTIFIER); if (!eob){ Token<?> identifier = tokenSequence.token(); startOffset = identifier.offset(info.getTokenHierarchy()); endOffset = startOffset + identifier.length(); } } } List<Integer> result = new ArrayList<Integer>(2); result.add(startOffset); result.add(endOffset ); return result; }
Example 18
Source File: LexUtilities.java From netbeans with Apache License 2.0 | 4 votes |
public static OffsetRange findFwdAlternativeSyntax(BaseDocument doc, TokenSequence<?extends PHPTokenId> ts, Token<?extends PHPTokenId> upToken) { int balance = 0; Token<?extends PHPTokenId> beginToken = LexUtilities.findPreviousToken(ts, Arrays.asList(PHPTokenId.PHP_IF, PHPTokenId.PHP_ELSE, PHPTokenId.PHP_ELSEIF, PHPTokenId.PHP_WHILE, PHPTokenId.PHP_FOR, PHPTokenId.PHP_FOREACH, PHPTokenId.PHP_SWITCH, PHPTokenId.PHP_CASE)); PHPTokenId beginTokenId = beginToken.id(); if (beginTokenId == PHPTokenId.PHP_ELSE || beginTokenId == PHPTokenId.PHP_ELSEIF) { beginTokenId = PHPTokenId.PHP_IF; } List<PHPTokenId> possibleEnd; if (beginTokenId == PHPTokenId.PHP_IF) { possibleEnd = Arrays.asList(PHPTokenId.PHP_IF, PHPTokenId.PHP_ELSE, PHPTokenId.PHP_ELSEIF, PHPTokenId.PHP_ENDIF); } else if (beginTokenId == PHPTokenId.PHP_WHILE) { possibleEnd = Arrays.asList(PHPTokenId.PHP_WHILE, PHPTokenId.PHP_ENDWHILE); } else if (beginTokenId == PHPTokenId.PHP_FOR) { possibleEnd = Arrays.asList(PHPTokenId.PHP_FOR, PHPTokenId.PHP_ENDFOR); } else if (beginTokenId == PHPTokenId.PHP_FOREACH) { possibleEnd = Arrays.asList(PHPTokenId.PHP_FOREACH, PHPTokenId.PHP_ENDFOREACH); } else if (beginTokenId == PHPTokenId.PHP_SWITCH) { possibleEnd = Arrays.asList(PHPTokenId.PHP_SWITCH, PHPTokenId.PHP_ENDSWITCH); } else if (beginTokenId == PHPTokenId.PHP_CASE) { possibleEnd = Arrays.asList(PHPTokenId.PHP_BREAK, PHPTokenId.PHP_ENDSWITCH); } else { return OffsetRange.NONE; } while (ts.moveNext()) { Token<?extends PHPTokenId> token = LexUtilities.findNextToken(ts, possibleEnd); if (token.id() == beginTokenId) { balance++; } else if (possibleEnd.contains(token.id())) { if (balance == 0) { return new OffsetRange(ts.offset(), ts.offset() + token.length()); } if (beginTokenId != PHPTokenId.PHP_IF || (beginTokenId == PHPTokenId.PHP_IF && token.id() == PHPTokenId.PHP_ENDIF)) { balance--; } } } return OffsetRange.NONE; }
Example 19
Source File: GroovyEmbeddingProvider.java From netbeans with Apache License 2.0 | 4 votes |
/** * Perform groovy translation. * * @param outputBuffer The buffer to emit the translation to * @param tokenHierarchy The token hierarchy for the RHTML code * @param tokenSequence The token sequence for the RHTML code */ private List<Embedding> translate(Snapshot snapshot, TokenSequence<GspTokenId> tokenSequence) { final List<Embedding> embeddings = new ArrayList<Embedding>(); embeddings.add(snapshot.create("def _buf ='';", GroovyTokenId.GROOVY_MIME_TYPE)); boolean skipNewline = false; while (tokenSequence.moveNext()) { Token<GspTokenId> token = tokenSequence.token(); int sourceStart = tokenSequence.offset(); int sourceEnd = sourceStart + token.length(); String text = token.text().toString(); switch (token.id()) { case HTML: // If there is leading whitespace in this token followed by a newline, // emit it directly first, then insert my buffer append. Otherwise, // insert a semicolon if we're on the same line as the previous output. boolean found = false; int i = 0; for (; i < text.length(); i++) { char c = text.charAt(i); if (c == '\n') { i++; // include it found = true; break; } else if (!Character.isWhitespace(c)) { break; } } if (found) { embeddings.add(snapshot.create(sourceStart, i, GroovyTokenId.GROOVY_MIME_TYPE)); text = text.substring(i); } embeddings.add(snapshot.create("_buf += \"\"\"", GroovyTokenId.GROOVY_MIME_TYPE)); if (skipNewline && text.startsWith("\n")) { // NOI18N text = text.substring(1); sourceEnd--; } embeddings.add(snapshot.create(text.replace("\"", "\\\""), GroovyTokenId.GROOVY_MIME_TYPE)); embeddings.add(snapshot.create("\"\"\";", GroovyTokenId.GROOVY_MIME_TYPE)); skipNewline = false; break; case COMMENT_HTML_STYLE_CONTENT: case COMMENT_GSP_STYLE_CONTENT: case COMMENT_JSP_STYLE_CONTENT: translateComment(snapshot, embeddings, sourceStart, text); break; case GSTRING_CONTENT: case SCRIPTLET_CONTENT: embeddings.add(snapshot.create(sourceStart, text.length(), GroovyTokenId.GROOVY_MIME_TYPE)); embeddings.add(snapshot.create(";", GroovyTokenId.GROOVY_MIME_TYPE)); skipNewline = false; break; case SCRIPTLET_OUTPUT_VALUE_CONTENT: embeddings.add(snapshot.create("_buf += (", GroovyTokenId.GROOVY_MIME_TYPE)); embeddings.add(snapshot.create(sourceStart, text.length(), GroovyTokenId.GROOVY_MIME_TYPE)); embeddings.add(snapshot.create(";)", GroovyTokenId.GROOVY_MIME_TYPE)); skipNewline = false; break; default: break; } } return embeddings; }
Example 20
Source File: BeanValidationConfigImpl.java From netbeans with Apache License 2.0 | 4 votes |
public HashMap<FileObject, ConstraintMapping> getConstraintMappings() { HashMap<FileObject, ConstraintMapping> constraintMap = new HashMap<FileObject, ConstraintMapping>(); getDocument(); insertOffset = 0; if (document != null) { try { document.readLock(); TokenHierarchy hi = TokenHierarchy.get(document); TokenSequence ts = hi.tokenSequence(); boolean started = false; boolean ended = false; boolean findInsertPoint = false; int startOffset = 0; int endOffset = 0; FileObject constraintFile = null; while (ts.moveNext()) { Token t = ts.token(); if (t.id() == XMLTokenId.TAG && t.text().equals("<validation-config")) { //NOI18N findInsertPoint = true; } if (findInsertPoint && t.id() == XMLTokenId.TAG && t.text().equals(">")) { //NOI18N insertOffset = t.offset(hi) +t.length(); findInsertPoint = false; } if (!started && t.id() == XMLTokenId.TAG && t.text().equals("<constraint-mapping")) { //NOI18N startOffset = t.offset(hi); endOffset = startOffset +t.length(); started = true; ended = true; } if (started && t.id() == XMLTokenId.TEXT) { endOffset += t.length(); String value = t.text().toString(); WebModule wm = WebModule.getWebModule(configFile); constraintFile = wm.getDocumentBase().getFileObject(value); } if (started && t.id() == XMLTokenId.TAG && t.text().equals("</constraint-mapping")) { //NOI18N endOffset += t.length()+1; ended = true; started = false; } if (ended && t.id() == XMLTokenId.TAG && t.text().equals(">")) { //NOI18N endOffset +=t.length(); ended = false; } if (!started && !ended && constraintFile != null) { ConstraintMapping constraintMapping = new ConstraintMappingImpl(constraintFile, startOffset, endOffset); constraintMap.put(constraintFile, constraintMapping); constraintFile = null; } } }finally{ document.readUnlock(); } } return constraintMap; }