1 19 20 package org.netbeans.editor.ext.html; 21 22 import javax.swing.text.BadLocationException ; 23 import org.netbeans.editor.BaseDocument; 24 import org.netbeans.editor.Syntax; 25 import org.netbeans.editor.TokenItem; 26 import org.netbeans.editor.ext.ExtSyntaxSupport; 27 import org.netbeans.editor.ext.html.dtd.DTD; 28 import org.netbeans.editor.ext.html.dtd.DTD.Element; 29 import org.netbeans.modules.editor.structure.formatting.TagBasedFormatter; 30 31 35 36 public class HTMLFormatter extends TagBasedFormatter { 37 private static final String [] UNFORMATTABLE_TAGS = new String []{"pre", "script", "code"}; 39 40 public HTMLFormatter(Class kitClass) { 41 super(kitClass); 42 } 43 44 @Override protected boolean acceptSyntax(Syntax syntax) { 45 return (syntax instanceof HTMLSyntax); 46 } 47 48 @Override protected ExtSyntaxSupport getSyntaxSupport(BaseDocument doc){ 49 return (HTMLSyntaxSupport)(doc.getSyntaxSupport().get(HTMLSyntaxSupport.class)); 50 } 51 52 @Override protected TokenItem getTagTokenEndingAtPosition(BaseDocument doc, int position) throws BadLocationException { 53 if (position >= 0) { 54 HTMLSyntaxSupport sup = (HTMLSyntaxSupport)getSyntaxSupport(doc); 55 TokenItem token = sup.getTokenChain(position, position + 1); 56 57 if (token.getTokenID() == HTMLTokenContext.TAG_CLOSE_SYMBOL && 58 !token.getImage().endsWith("/>")){ 60 do { 61 token = token.getPrevious(); 62 } 63 while (token != null && !(isOpeningTag(token) || isClosingTag(token))); 64 65 return token; 66 } 67 } 68 return null; 69 } 70 71 @Override protected int getTagEndOffset(TokenItem token){ 72 TokenItem t = token.getNext(); 73 74 while (t != null && t.getTokenID() != HTMLTokenContext.TAG_CLOSE_SYMBOL){ 75 t = t.getNext(); 76 } 77 78 return t == null ? -1 : t.getOffset(); 79 } 80 81 @Override protected boolean isJustBeforeClosingTag(BaseDocument doc, int pos) throws BadLocationException { 82 return super.isJustBeforeClosingTag(doc, pos + "</".length()); } 85 86 @Override protected boolean isClosingTag(TokenItem token){ 87 return token != null && token.getTokenID() == HTMLTokenContext.TAG_CLOSE; 88 } 89 90 @Override protected boolean isOpeningTag(TokenItem token){ 91 return token != null && token.getTokenID() == HTMLTokenContext.TAG_OPEN; 92 } 93 94 @Override protected String extractTagName(TokenItem tknTag){ 95 return tknTag.getImage().trim(); 96 } 97 98 @Override protected boolean areTagNamesEqual(String tagName1, String tagName2){ 99 return tagName1.equalsIgnoreCase(tagName2); 100 } 101 102 @Override protected int getOpeningSymbolOffset(TokenItem tknTag){ 103 TokenItem tkn = tknTag; 104 105 do{ 106 tkn = tkn.getPrevious(); 107 } 108 while(tkn != null && tkn.getTokenID() != HTMLTokenContext.TAG_OPEN_SYMBOL); 109 110 if (tkn != null){ 111 return tkn.getOffset(); 112 } 113 114 return -1; 115 } 116 117 @Override protected boolean isClosingTagRequired(BaseDocument doc, String tagName) { 118 HTMLSyntaxSupport htmlsup = (HTMLSyntaxSupport)(doc.getSyntaxSupport().get(HTMLSyntaxSupport.class)); 119 DTD dtd = htmlsup.getDTD(); 120 121 if (dtd == null){ 122 return false; 124 } 125 126 Element elem = dtd.getElement(tagName.toUpperCase()); 127 128 if (elem == null){ 129 return true; 131 } 132 133 return !elem.isEmpty(); } 135 136 @Override protected boolean isUnformattableToken(TokenItem token) { 137 138 if (token.getTokenID() == HTMLTokenContext.BLOCK_COMMENT){ 139 return true; 140 } 141 142 return false; 143 } 144 145 @Override protected boolean isUnformattableTag(String tag) { 146 for(int i = 0; i < UNFORMATTABLE_TAGS.length; i++) { 147 if(tag.equalsIgnoreCase(UNFORMATTABLE_TAGS[i])) { 148 return true; 149 } 150 } 151 152 return false; 153 } 154 } 155 | Popular Tags |