|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.web.core.syntax; import java.io.IOException; import java.io.Writer; import javax.swing.text.BadLocationException; import javax.swing.text.JTextComponent; import org.netbeans.editor.BaseDocument; import org.netbeans.editor.Settings; import org.netbeans.editor.Syntax; import org.netbeans.editor.TokenContext; import org.netbeans.editor.TokenContextPath; import org.netbeans.editor.TokenItem; import org.netbeans.editor.Utilities; import org.netbeans.editor.ext.AbstractFormatLayer; import org.netbeans.editor.ext.ExtFormatter; import org.netbeans.editor.ext.FormatSupport; import org.netbeans.editor.ext.FormatWriter; import org.netbeans.editor.ext.html.HTMLFormatter; import org.netbeans.editor.ext.html.HTMLTokenContext; import org.netbeans.editor.ext.java.JavaFormatSupport; import org.netbeans.editor.ext.java.JavaFormatter; import org.netbeans.editor.ext.java.JavaSettingsInitializer; import org.netbeans.editor.ext.java.JavaTokenContext; import org.netbeans.modules.editor.html.HTMLKit; import org.netbeans.modules.editor.java.JavaKit; import org.openide.ErrorManager; /** * Formatter for jsp and tag files. * @author Petr Pisl */ public class JspFormatter extends ExtFormatter { JavaFormatter jFormatter; HTMLFormatter hFormatter; /** Creates a new instance of HTMLFormater */ public JspFormatter(Class kitClass) { super(kitClass); jFormatter = new JspJavaFormatter (JavaKit.class); hFormatter = new HTMLFormatter(HTMLKit.class); } protected boolean acceptSyntax(Syntax syntax) { return (syntax instanceof JspMultiSyntax); } public Writer reformat(BaseDocument doc, int startOffset, int endOffset, boolean indentOnly) throws BadLocationException, IOException { if (startOffset == endOffset){ return super.reformat(doc, startOffset, endOffset, indentOnly); } int pos = Utilities.getRowStart(doc, endOffset); TokenItem token = null; String tag; JspSyntaxSupport sup = (JspSyntaxSupport)(doc.getSyntaxSupport().get(JspSyntaxSupport.class)); do { try{ int fnw = Utilities.getRowFirstNonWhite(doc, pos); if (fnw == -1) fnw = pos; token = sup.getTokenChain(fnw, fnw+1); if (token != null){ isCloseTag(token); if ((tag = isCloseTag(token)) != null){ int poss = -1; while ( token != null){ if (canBeOpenTag(tag, token)){ if (poss == 0){ //indent the close tag on the same position as the open tag is doc.remove(pos, fnw-pos); fnw = Utilities.getRowFirstNonWhite(doc, token.getOffset()); poss = Utilities.getRowStart(doc, fnw); doc.insertString(pos, doc.getText(poss, fnw-poss), null); int tagIndentation = Utilities.getRowIndent(doc, pos); // If it's reformat action, then reformat text between the open and close tag as well if (!indentOnly){ int rowOffset = Utilities.getRowStart(doc, Utilities.getRowStart(doc, pos) - 1); int indentation = Utilities.getRowIndent(doc, pos) + this.getShiftWidth(); int delta = 0; int deltahelp; while (rowOffset > poss){ deltahelp = Utilities.getRowFirstNonWhite(doc, rowOffset ); if (deltahelp > -1){ token = sup.getTokenChain (deltahelp, deltahelp+1); if (token != null && (token.getTokenContextPath().contains(HTMLTokenContext.contextPath) || (token.getTokenContextPath().contains(JspTagTokenContext.contextPath) && token.getTokenID().getNumericID() != JspTagTokenContext.SYMBOL2_ID))){ changeRowIndent(doc, rowOffset, indentation); int htmlindent = Utilities.getRowIndent(doc, rowOffset); delta = delta + Utilities.getRowFirstNonWhite(doc, rowOffset ) - deltahelp; } } rowOffset = Utilities.getRowStart(doc, rowOffset-1); } pos = pos + delta; } break; } else{ poss--; } } else { if (token.getImage().indexOf(tag) > -1){ poss++; } } token = token.getPrevious(); } } } } catch (Exception e){ ErrorManager.getDefault().notify(ErrorManager.WARNING, e); } pos = Utilities.getRowStart(doc, pos-1); } while (pos > startOffset && pos > 0); return null; } public int[] getReformatBlock(JTextComponent target, String typedText) { int offset = target.getCaret().getDot(); JspSyntaxSupport sup = (JspSyntaxSupport)Utilities.getSyntaxSupport(target); try{ TokenItem tokenItem = sup.getItemAtOrBefore(offset); if (tokenItem != null){ TokenContextPath tcp = tokenItem.getTokenContextPath(); //test whether the offset is in a java scripting language if(tcp.contains(JavaTokenContext.contextPath)) return jFormatter.getReformatBlock(target, typedText); else return hFormatter.getReformatBlock(target, typedText); } } catch (Exception e){ ErrorManager.getDefault().notify(ErrorManager.WARNING, e); } return super.getReformatBlock (target, typedText); } protected void initFormatLayers() { addFormatLayer(new SwitchLayer()); } /** Method returns string of close tag * @return : null if the token is not close tag, else returns the image of the close tag */ private String isCloseTag (TokenItem token){ String value = null; if (token == null) return null; if (token.getTokenID().getNumericID() == HTMLTokenContext.TAG_ID && token.getTokenContextPath().contains(HTMLTokenContext.contextPath)) { if (token.getImage().length() > 1 && token.getImage().charAt(1) == '/') value = token.getImage().substring(2).trim(); } else { if (token.getTokenID().getNumericID() == JspTagTokenContext.SYMBOL_ID && token.getTokenContextPath().contains(JspTagTokenContext.contextPath) && token.getImage().equals(" |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.