001: /*******************************************************************************
002: * Copyright (c) 2003, 2004 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jsp;
011:
012: import java.io.IOException;
013: import java.io.Reader;
014: import java.util.ArrayList;
015:
016: import org.eclipse.jface.text.source.translation.ITagHandler;
017: import org.eclipse.jface.text.source.translation.ITagHandlerFactory;
018: import org.eclipse.jface.text.source.translation.ITranslator;
019:
020: import org.eclipse.jdt.internal.ui.examples.jspeditor.JspTranslatorResultCollector;
021:
022: public class JspTranslator extends AbstractJspParser implements
023: ITranslator {
024:
025: private StringBuffer fDeclarations = new StringBuffer();
026: private StringBuffer fContent = new StringBuffer();
027: private StringBuffer fLocalDeclarations = new StringBuffer();
028:
029: private ArrayList fContentLines = new ArrayList();
030: private ArrayList fDeclarationLines = new ArrayList();
031: private ArrayList fLocalDeclarationLines = new ArrayList();
032: private int[] fSmap;
033:
034: private ITagHandlerFactory fTagHandlerFactor;
035: private ITagHandler fCurrentTagHandler;
036:
037: private JspTranslatorResultCollector fResultCollector;
038:
039: public JspTranslator() {
040:
041: // Links for passing parameters to the tag handlers
042: fResultCollector = new JspTranslatorResultCollector(
043: fDeclarations, fLocalDeclarations, fContent,
044: fDeclarationLines, fLocalDeclarationLines,
045: fContentLines);
046: }
047:
048: protected void startTag(boolean endTag, String name, int startName) {
049:
050: fCurrentTagHandler = fTagHandlerFactor.getHandler(name);
051: }
052:
053: protected void tagAttribute(String attrName, String value,
054: int startName, int startValue) {
055:
056: if (fCurrentTagHandler != null)
057: fCurrentTagHandler.addAttribute(attrName, value, fLines);
058: }
059:
060: protected void endTag(boolean end) {
061:
062: if (fCurrentTagHandler != null)
063: try {
064: fCurrentTagHandler.processEndTag(fResultCollector,
065: fLines);
066: } catch (IOException ex) {
067: ex.printStackTrace();
068: }
069: }
070:
071: protected void java(char ch, String java, int line) {
072:
073: if (ch == '!')
074: fCurrentTagHandler = fTagHandlerFactor.getHandler("<%!"); //$NON-NLS-1$
075: else
076: fCurrentTagHandler = fTagHandlerFactor.getHandler("<%"); //$NON-NLS-1$
077:
078: /*
079: * XXX: This is needed because the used parser does not treat
080: * "<%" like every other tag.
081: */
082: fCurrentTagHandler.addAttribute("source", java, line); //$NON-NLS-1$
083:
084: try {
085: fCurrentTagHandler.processEndTag(fResultCollector, line);
086: } catch (IOException e) {
087: e.printStackTrace();
088: }
089: }
090:
091: protected void text(String t, int line) {
092: int i = 0;
093: StringBuffer out = new StringBuffer();
094: while (i < t.length()) {
095: char c = t.charAt(i++);
096: if (c == '\n') {
097: fContent
098: .append(" System.out.println(\"" + out.toString() + "\"); //$NON-NLS-1$\n"); //$NON-NLS-1$//$NON-NLS-2$
099: fContentLines.add(new Integer(line++));
100: out.setLength(0);
101: } else {
102: out.append(c);
103: }
104: }
105: if (out.length() > 0) {
106: fContent
107: .append(" System.out.print(\"" + out.toString() + "\"); //$NON-NLS-1$\n"); //$NON-NLS-1$ //$NON-NLS-2$
108: fContentLines.add(new Integer(line));
109: }
110: }
111:
112: private void resetTranslator() {
113: fDeclarations.setLength(0);
114: fContent.setLength(0);
115: fLocalDeclarations.setLength(0);
116:
117: fLocalDeclarationLines.clear();
118: fContentLines.clear();
119: fDeclarationLines.clear();
120:
121: }
122:
123: public String translate(Reader reader, String name)
124: throws IOException {
125:
126: StringBuffer buffer = new StringBuffer();
127:
128: resetTranslator();
129: parse(reader);
130:
131: int lineCount = 2 + fDeclarationLines.size() + 1 + 1
132: + fLocalDeclarationLines.size() + fContentLines.size()
133: + 3;
134: fSmap = new int[lineCount];
135: int line = 0;
136: fSmap[line++] = 1;
137:
138: buffer.append("public class " + name + " {\n\n"); //$NON-NLS-1$ //$NON-NLS-2$
139: fSmap[line++] = 1;
140: fSmap[line++] = 1;
141:
142: buffer.append(fDeclarations.toString() + "\n"); //$NON-NLS-1$
143: System.out.println(fDeclarations.toString());
144: for (int i = 0; i < fDeclarationLines.size(); i++) {
145: fSmap[line++] = ((Integer) fDeclarationLines.get(i))
146: .intValue();
147: System.out
148: .println("" + ((Integer) fDeclarationLines.get(i)).intValue()); //$NON-NLS-1$
149: }
150: fSmap[line] = fSmap[line - 1] + 1;
151: line++;
152:
153: buffer.append(" public void out() {\n"); //$NON-NLS-1$
154: fSmap[line] = fSmap[line - 1] + 1;
155: line++;
156:
157: if (fLocalDeclarations.length() > 0) {
158: buffer.append(fLocalDeclarations.toString());
159: System.out.println(fLocalDeclarations);
160: for (int i = 0; i < fLocalDeclarationLines.size(); i++) {
161: System.out
162: .println("" + ((Integer) fLocalDeclarationLines.get(i)).intValue()); //$NON-NLS-1$
163: fSmap[line++] = ((Integer) fLocalDeclarationLines
164: .get(i)).intValue();
165: }
166: }
167:
168: buffer.append(fContent.toString());
169: System.out.println(fContent);
170: for (int i = 0; i < fContentLines.size(); i++) {
171: fSmap[line++] = ((Integer) fContentLines.get(i)).intValue();
172: System.out
173: .println("" + ((Integer) fContentLines.get(i)).intValue()); //$NON-NLS-1$
174: }
175:
176: buffer.append(" }\n"); //$NON-NLS-1$
177: fSmap[line] = fSmap[line - 1];
178:
179: line++;
180:
181: buffer.append("}\n"); //$NON-NLS-1$
182: fSmap[line] = fSmap[line - 2];
183:
184: for (int i = 0; i < fSmap.length; i++)
185: System.out.println("" + i + " -> " + fSmap[i]); //$NON-NLS-1$ //$NON-NLS-2$
186:
187: System.out.println(buffer.toString());
188:
189: return buffer.toString();
190: }
191:
192: public int[] getLineMapping() {
193: return fSmap;
194: }
195:
196: /*
197: * @see org.eclipse.jface.text.source.ITranslator#setTagHandlerFactory(org.eclipse.jface.text.source.ITagHandlerFactory)
198: */
199: public void setTagHandlerFactory(
200: ITagHandlerFactory tagHandlerFactory) {
201: fTagHandlerFactor = tagHandlerFactory;
202: }
203:
204: /*
205: * @see ITranslator#backTranslateOffsetInLine(String, String, int)
206: */
207: public int backTranslateOffsetInLine(String originalLine,
208: String translatedLine, int offsetInTranslatedLine,
209: String tag) {
210:
211: ITagHandler handler;
212: if (tag != null)
213: handler = fTagHandlerFactor.getHandler(tag);
214: else
215: handler = fTagHandlerFactor.findHandler(originalLine);
216:
217: if (handler != null)
218: return handler.backTranslateOffsetInLine(originalLine,
219: translatedLine, offsetInTranslatedLine);
220:
221: return -1;
222: }
223: }
|