001: package org.acm.seguin.ide.jedit;
002:
003: import java.io.File;
004: import java.io.StringWriter;
005: import java.io.Writer;
006: import java.util.Enumeration;
007: import java.util.Iterator;
008: import java.util.Vector;
009:
010: import errorlist.DefaultErrorSource;
011: import net.sourceforge.jrefactory.ast.ASTCompilationUnit;
012: import net.sourceforge.jrefactory.factory.BufferParserFactory;
013: import net.sourceforge.jrefactory.parser.ParseException;
014: import net.sourceforge.jrefactory.parser.Token;
015:
016: import org.acm.seguin.awt.ExceptionPrinter;
017: import org.acm.seguin.ide.common.options.PropertiesFile;
018: import org.acm.seguin.pretty.PrettyPrintFile;
019: import org.acm.seguin.project.Project;
020: import org.gjt.sp.jedit.*;
021: import org.gjt.sp.jedit.io.VFSManager;
022: import org.gjt.sp.jedit.textarea.JEditTextArea;
023: import org.gjt.sp.util.Log;
024: import net.sourceforge.jrefactory.ast.SimpleNode;
025: import net.sourceforge.jrefactory.ast.ASTCompilationUnit;
026: import org.acm.seguin.util.FileSettings;
027: import net.sourceforge.jrefactory.factory.FileParserFactory;
028: import net.sourceforge.jrefactory.factory.ParserFactory;
029: import net.sourceforge.jrefactory.parser.JavaParser;
030:
031: // note: JEditPrettyPrinter is _not_ derived from PrettyPrintFromIDE
032: // (like it should be), because then we cannot handle the parse
033: // exceptions ourselves.
034: /**
035: * Description of the Class
036: *
037: * @author Mike Atkinson (Mike)
038: * @since 1.0
039: * @created 14 July 2003
040: */
041: public class JEditJSPPrettyPrinter extends JEditPrettyPrinter {
042:
043: /**
044: * Constructor for the JEditPrettyPrinter object
045: *
046: * @param jsPlugin Description of Parameter
047: * @param view Description of Parameter
048: * @param buffer Description of Parameter
049: * @since v 1.0
050: */
051: public JEditJSPPrettyPrinter(JavaStylePlugin jsPlugin, View view,
052: Buffer buffer) {
053: super (jsPlugin, view, buffer);
054: }
055:
056: /**
057: * Main processing method for the JEditPrettyPrinter object
058: *
059: * @since v 1.0
060: */
061: public void run() {
062: Log.log(Log.DEBUG, this , "formatting the buffer...");
063:
064: int caretPos = 0;
065: JEditTextArea textarea = null;
066:
067: try {
068: if (view != null) {
069: view.showWaitCursor();
070: textarea = view.getTextArea();
071: caretPos = textarea.getCaretPosition();
072: }
073:
074: setProjectData(view, buffer);
075: setSettings();
076:
077: // set settings from jEdit
078: // get text string
079: String before = buffer.getText(0, buffer.getLength());
080: // remember and remove all markers:
081: Vector markers = (Vector) buffer.getMarkers().clone();
082:
083: StringBuffer scriptCode = new StringBuffer();
084: String rest = before;
085: int index1 = 0;
086: int index2 = 0;
087: index1 = rest.indexOf("<%", index2);
088: if (index1 >= 0) {
089: index2 = rest.indexOf("%>", index1);
090: String x = rest.substring(0, index1 + 3);
091: String y = rest.substring(index1 + 3, index2);
092: char c = rest.charAt(index1 + 2);
093: rest = rest.substring(index2);
094: if (c == '!') {
095: scriptCode.append(y);
096: } else if (c == '=') {
097: scriptCode.append(y);
098: } else {
099: scriptCode.append(y);
100: }
101: }
102: System.out.println("scriptCode=" + scriptCode);
103: setInputString(scriptCode.toString());
104: apply(null);
105:
106: String contentsX = getOutputBuffer();
107: System.out.println("contentsX=" + contentsX);
108:
109: StringBuffer after = new StringBuffer();
110: rest = before;
111: index1 = 0;
112: index2 = 0;
113: index1 = rest.indexOf("<%", index2);
114: if (index1 >= 0) {
115: index2 = rest.indexOf("%>", index1);
116: String x = rest.substring(0, index1 + 3);
117: after.append(x);
118: String y = rest.substring(index1 + 3, index2);
119: char c = rest.charAt(index1 + 2);
120: rest = rest.substring(index2);
121: System.out.println("\"" + x + "\"");
122: System.out.println("\"" + y + "\"");
123: if (c == '!') {
124: after.append(y);
125: } else if (c == '=') {
126: after.append(y);
127: } else {
128: after.append(y);
129: }
130: }
131: System.out.println("\"" + rest + "\"");
132: after.append(rest);
133:
134: String contents = after.toString();
135:
136: // get the new contents back
137: if (exception != null) {
138: exception.printStackTrace();
139: // the JRefactory reformatting caused an exception, so handle it.
140: if (exception instanceof ParseException) {
141: Token currentToken = ((ParseException) exception).currentToken;
142: JavaStylePlugin.getErrorSource().clear();
143: JavaStylePlugin
144: .getErrorSource()
145: .addError(
146: DefaultErrorSource.ERROR,
147: buffer.getPath(),
148: Math
149: .max(
150: 0,
151: currentToken.next.beginLine - 1),
152: Math
153: .max(
154: 0,
155: currentToken.next.beginColumn - 1),
156: Math
157: .max(
158: 0,
159: currentToken.next.endColumn),
160: exception.getMessage());
161: } else {
162: exception.printStackTrace();
163: // no idea what caused this exception
164: Log.log(Log.ERROR, this , exception);
165: }
166: exception = null;
167:
168: } else if (contents == null || contents.length() == 0) {
169: // we did not get an exception but JRefactory still did not produce valid contents
170: GUIUtilities.error(view, "javastyle.error.other", null);
171: } else if (!contents.equals(before)) {
172: // The JRefactory reformatting caused a change in the text so perform the edit.
173: try {
174: buffer.beginCompoundEdit();
175: if (markers.size() > 0) {
176: buffer.removeAllMarkers();
177: }
178:
179: buffer.remove(0, buffer.getLength());
180: buffer.insert(0, contents);
181:
182: // restore markers:
183: Enumeration enumx = markers.elements();
184:
185: while (enumx.hasMoreElements()) {
186: Marker marker = (Marker) enumx.nextElement();
187:
188: buffer.addMarker(marker.getShortcut(), marker
189: .getPosition());
190: }
191: // restore caret position
192: if (textarea != null) {
193: int bufLen = textarea.getBufferLength();
194:
195: textarea
196: .setCaretPosition(caretPos > bufLen ? bufLen
197: : caretPos);
198: textarea.scrollToCaret(true);
199: }
200: } catch (Exception ex) {
201: ex.printStackTrace();
202: Log.log(Log.ERROR, this , ex);
203: } finally {
204: buffer.endCompoundEdit();
205: }
206: }
207:
208: } catch (Throwable ex) {
209: ex.printStackTrace();
210: Log.log(Log.ERROR, this , ex);
211: GUIUtilities.error(view, "javastyle.error.parse",
212: new Object[] { ex.toString() });
213: } finally {
214: if (view != null) {
215: view.hideWaitCursor();
216: }
217: }
218: }
219:
220: private String prettyPrintJSPDeclaration(String before) {
221: //setInputString(before);
222: //apply(null);
223: //// JRefactory reformat (may cause exception to be set).
224: //return getOutputBuffer();
225: System.out.println("before=" + before);
226: return before;
227: }
228:
229: private String prettyPrintJSPExpression(String before) {
230: //setInputString(before);
231: //apply(null);
232: //// JRefactory reformat (may cause exception to be set).
233: //return getOutputBuffer();
234: System.out.println("before=" + before);
235: return before;
236: }
237:
238: private String prettyPrintJSP(String before) {
239: //setInputString(before);
240: //apply(null);
241: //// JRefactory reformat (may cause exception to be set).
242: //return getOutputBuffer();
243: System.out.println("before=" + before);
244: return before;
245: }
246:
247: /**
248: * Sets the InputString attribute of the JEditPrettyPrinter object
249: *
250: * @param input The new InputString value
251: * @since v 1.0
252: */
253: protected void setInputString(String input) {
254: if (input == null) {
255: return;
256: }
257: setParserFactory(new ClassBodyParserFactory(input));
258: }
259:
260: /**
261: * Apply the refactoring
262: *
263: * @param inputFile the input file
264: * @param root Description of Parameter
265: */
266: /*
267: public void apply(File inputFile, SimpleNode root) {
268: if (root != null) {
269: FileSettings pretty = FileSettings.getRefactoryPrettySettings();
270:
271: pretty.setReloadNow(true);
272:
273: // Create the visitor
274: PrettyPrintVisitor printer = new PrettyPrintVisitor();
275:
276: // Create the appropriate print data
277: PrintData data = getPrintData(inputFile);
278:
279: if (root instanceof ASTCompilationUnit) {
280: printer.visit((ASTCompilationUnit) root, data);
281: } else {
282: printer.visit(root, data);
283: }
284:
285: data.close(); // Flush the output stream and close it
286: }
287: }
288: */
289:
290: private static class ClassBodyParserFactory extends
291: BufferParserFactory {
292: public ClassBodyParserFactory(String buffer) {
293: super (buffer);
294: }
295:
296: protected SimpleNode parse(JavaParser parser)
297: throws ParseException {
298: return parser.JSPBody();
299: }
300: }
301: }
|