1 19 20 21 package org.netbeans.modules.html.editor.coloring; 22 23 import java.util.List ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 import javax.swing.text.BadLocationException ; 27 import javax.swing.text.Document ; 28 import org.netbeans.api.html.lexer.HTMLTokenId; 29 import org.netbeans.api.lexer.Language; 30 import org.netbeans.api.lexer.Token; 31 import org.netbeans.api.lexer.TokenHierarchy; 32 import org.netbeans.api.lexer.TokenSequence; 33 import org.netbeans.editor.BaseDocument; 34 import org.netbeans.editor.BaseDocument; 35 import org.netbeans.editor.ext.html.parser.SyntaxElement; 36 import org.netbeans.editor.ext.html.parser.SyntaxParserListener; 37 38 43 public class EmbeddingUpdater implements SyntaxParserListener { 44 45 private static final String JAVASCRIPT_MIMETYPE = "text/javascript"; 47 private static final String CSS_MIMETYPE = "text/css2"; 50 private static final String HTML_SCRIPT_TAG_NAME = "script"; 52 private static final Logger LOGGER = Logger.getLogger(EmbeddingUpdater.class.getName()); 53 54 private final Document doc; 55 56 private int scriptStart; 57 58 public EmbeddingUpdater(Document doc) { 59 this.doc = doc; 60 } 61 62 public void parsingFinished(List <SyntaxElement> elements) { 63 scriptStart = -1; 64 for(SyntaxElement sel : elements) { 65 if(sel.getType() == SyntaxElement.TYPE_TAG) { 66 startTag((SyntaxElement.Tag)sel); 67 } else if(sel.getType() == SyntaxElement.TYPE_ENDTAG) { 68 endTag((SyntaxElement.Named)sel); 69 } 70 } 71 } 72 73 private void startTag(SyntaxElement.Tag sel) { 74 if(HTML_SCRIPT_TAG_NAME.equals(sel.getName())) { 76 scriptStart = sel.getElementOffset() + sel.getElementLength(); 77 } 78 for(SyntaxElement.TagAttribute tagattr : sel.getAttributes()) { 80 if("style".equalsIgnoreCase(tagattr.getName())) { createEmbedding(CSS_MIMETYPE, tagattr); 83 } else if(tagattr.getName().startsWith("on") || tagattr.getName().startsWith("ON")) { 84 createEmbedding(JAVASCRIPT_MIMETYPE, tagattr); 87 } 88 } 89 } 90 91 private void endTag(SyntaxElement.Named sel) { 92 if(HTML_SCRIPT_TAG_NAME.equals(sel.getName())) { 93 if(scriptStart != -1) { 94 createEmbedding(JAVASCRIPT_MIMETYPE, scriptStart, sel.getElementOffset() - 1, 0,0); 95 } 96 } 97 } 98 99 private void createEmbedding(String mimeType, SyntaxElement.TagAttribute tagAttr) { 100 if(tagAttr.getValue().charAt(0) == '\'' || tagAttr.getValue().charAt(0) == '"') { 101 createEmbedding(mimeType, tagAttr.getValueOffset(), tagAttr.getValueOffset() + tagAttr.getValue().length(), 1, 1); 103 } else { 104 createEmbedding(mimeType, tagAttr.getValueOffset(), tagAttr.getValueOffset() + tagAttr.getValue().length(), 0, 0); 105 } 106 } 107 108 private void createEmbedding(String mimeType, int startOffset, int endOffset, int startSkipLength, int endSkipLength ) { 109 Language lang = Language.find(mimeType); 110 if(lang == null) { 111 LOGGER.log(Level.WARNING, "No " + mimeType + " language found! (" + startOffset + " - " + endOffset + ")"); 112 return ; } 114 115 ((BaseDocument)doc).readLock(); 116 try { 117 TokenHierarchy th = TokenHierarchy.get(doc); 118 TokenSequence ts = tokenSequence(th, scriptStart); 119 120 ts.move(startOffset); 121 if(!ts.moveNext()) { 122 return ; } 124 125 do { 126 Token item = ts.token(); 127 if(!ts.createEmbedding(lang, startSkipLength, endSkipLength)) { 128 LOGGER.log(Level.WARNING, "Cannot create embedding for " + mimeType + "."); 129 } else { 130 LOGGER.log(Level.INFO, "Embedding for " + mimeType + " created [" + startOffset + " - " + endOffset + "] (" + item.text().toString() + ")"); 131 } 132 } while(ts.moveNext() && ts.offset() <= endOffset); 133 }finally { 134 ((BaseDocument)doc).readUnlock(); 135 } 136 } 137 138 139 private static TokenSequence tokenSequence(TokenHierarchy hi, int offset) { 140 TokenSequence ts = hi.tokenSequence(HTMLTokenId.language()); 141 if(ts == null) { 142 ts = hi.tokenSequence(); 144 ts.move(offset); 145 if(!ts.moveNext()) { 146 return null; } else { 148 ts = ts.embedded(HTMLTokenId.language()); 149 } 150 } 151 return ts; 152 } 153 154 155 156 } 157 | Popular Tags |