001: /*******************************************************************************
002: * Copyright (c) 2000, 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.jdt.internal.ui.examples.jspeditor;
011:
012: import java.io.IOException;
013:
014: import org.eclipse.core.runtime.Assert;
015:
016: import org.eclipse.jface.text.source.translation.ITagHandler;
017: import org.eclipse.jface.text.source.translation.ITranslatorResultCollector;
018:
019: /**
020: *
021: * @since 3.0
022: */
023: public class Jsp2JavaTagHandler implements ITagHandler {
024:
025: private boolean fInUseBean;
026: private boolean fInTagLib;
027: private String fTagLibValue;
028: private String fClass;
029: private String fId;
030: private String fSource;
031: private boolean fInDeclaration;
032: private boolean fInJavaSection;
033:
034: /*
035: * @see org.eclipse.jface.text.source.ITagHandler#canHandleTag(java.lang.String)
036: */
037: public boolean canHandleTag(String tag) {
038: return true;
039: }
040:
041: /*
042: * @see org.eclipse.jface.text.source.ITagHandler#canHandleText(java.lang.String)
043: */
044: public boolean canHandleText(String text) {
045: return true;
046: }
047:
048: public void reset(String startTag) {
049: fInUseBean = "jsp:useBean".equals(startTag); //$NON-NLS-1$
050: fInTagLib = "c:out".equals(startTag); //$NON-NLS-1$
051: fInJavaSection = "<%".equals(startTag); //$NON-NLS-1$
052: fInDeclaration = "<%!".equals(startTag); //$NON-NLS-1$
053: }
054:
055: /*
056: * @see org.eclipse.jface.text.source.ITagHandler#addAttribute(java.lang.String, java.lang.String)
057: */
058: public void addAttribute(String name, String value,
059: int sourceLineNumber) {
060: if (fInUseBean) {
061: if ("id".equals(name)) //$NON-NLS-1$
062: fId = value;
063: else if ("class".equals(name)) //$NON-NLS-1$
064: fClass = value;
065: }
066: if (fInTagLib) {
067: fTagLibValue = value;
068: }
069: if ("source".equals(name)) //$NON-NLS-1$
070: fSource = value;
071: }
072:
073: /*
074: * @see org.eclipse.jface.text.source.ITagHandler#backTranslateOffsetInLine(java.lang.String, int)
075: */
076: public int backTranslateOffsetInLine(String originalLine,
077: String translatedLine, int offsetInTranslatedLine) {
078: int javaPartitionStart = 0;
079: if (originalLine.indexOf("<%") != -1) //$NON-NLS-1$
080: javaPartitionStart = handleJavaSection(originalLine,
081: offsetInTranslatedLine);
082: else if (originalLine.indexOf("<jsp:useBean id=\"") != -1) { //$NON-NLS-1$
083: javaPartitionStart = handleUseBeanTag(originalLine,
084: offsetInTranslatedLine);
085: } else if (originalLine.indexOf("<c:out value=\"${") != -1) { //$NON-NLS-1$
086: javaPartitionStart = handleTagLib(originalLine,
087: offsetInTranslatedLine);
088: }
089: return javaPartitionStart;
090: }
091:
092: private int handleJavaSection(String jspLineStr,
093: int relativeLineOffsetInJava) {
094: return jspLineStr.indexOf("<%") + 3; //$NON-NLS-1$
095: }
096:
097: private int handleTagLib(String jspLineStr,
098: int relativeLineOffsetInJava) {
099: int javaFileOffset = "System.out.println(".length(); //$NON-NLS-1$
100: return jspLineStr.indexOf("<c:out value=\"${") + 16 - javaFileOffset; //$NON-NLS-1$
101: }
102:
103: /*
104: * This is a good example where the relative line offset in the Java
105: * document cannot be directly mapped back to Jsp document.
106: */
107: private int handleUseBeanTag(String jspLineStr,
108: int relativeLineOffsetInJava) {
109:
110: int javaPartitionStart;
111:
112: int variableNameStart = jspLineStr
113: .indexOf("<jsp:useBean id=\"") + 17; //$NON-NLS-1$
114: int variableNameLength = Math.max(0, jspLineStr.indexOf('"',
115: variableNameStart)
116: - variableNameStart);
117:
118: int typeStart = jspLineStr.indexOf("class=\"") + 7; //$NON-NLS-1$
119: int typeLength = Math.max(0, jspLineStr.indexOf('"', typeStart)
120: - typeStart);
121:
122: if (relativeLineOffsetInJava < typeLength) {
123: javaPartitionStart = typeStart;
124: } else if (relativeLineOffsetInJava < typeLength
125: + variableNameLength)
126: javaPartitionStart = variableNameStart;
127: else
128: javaPartitionStart = typeStart;
129:
130: // start relative to Jsp line start
131: return javaPartitionStart - relativeLineOffsetInJava;
132: }
133:
134: /*
135: * @see org.eclipse.jface.text.source.ITagHandler#processEndTag(ITranslatorResultCollector, int)
136: */
137: public void processEndTag(
138: ITranslatorResultCollector resultCollector,
139: int sourceLineNumber) throws IOException {
140: Assert
141: .isTrue(resultCollector instanceof JspTranslatorResultCollector);
142:
143: JspTranslatorResultCollector jspResultCollector = (JspTranslatorResultCollector) resultCollector;
144:
145: if (fInUseBean) {
146: if (fId != null && fClass != null) {
147: jspResultCollector
148: .appendLocalDeclaration(
149: fClass
150: + " " + fId + "= new " + fClass + "();\n", sourceLineNumber); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
151: fId = fClass = null;
152: }
153: fInUseBean = false;
154: }
155: if (fInTagLib && fTagLibValue != null) {
156: jspResultCollector
157: .appendContent(
158: "System.out.println(" + fTagLibValue.substring(2, fTagLibValue.length() - 1) + ");\n", sourceLineNumber); //$NON-NLS-1$ //$NON-NLS-2$
159: fTagLibValue = null;
160: fInTagLib = false;
161: }
162: if (fInJavaSection) {
163: int i = 0;
164: StringBuffer out = new StringBuffer();
165: while (i < fSource.length()) {
166: char c = fSource.charAt(i++);
167: if (c == '\n') {
168: jspResultCollector.appendContent(out.toString()
169: + "\n", sourceLineNumber++); //$NON-NLS-1$
170: out.setLength(0);
171: } else {
172: out.append(c);
173: }
174: }
175: if (out.length() > 0) {
176: jspResultCollector.appendContent(
177: out.toString() + "\n", sourceLineNumber); //$NON-NLS-1$
178: }
179: }
180: if (fInDeclaration) {
181: int i = 0;
182: StringBuffer out = new StringBuffer();
183: while (i < fSource.length()) {
184: char c = fSource.charAt(i++);
185: if (c == '\n') {
186: jspResultCollector.appendDeclaration(out.toString()
187: + "\n", sourceLineNumber++); //$NON-NLS-1$
188: out.setLength(0);
189: } else {
190: out.append(c);
191: }
192: }
193: if (out.length() > 0) {
194: jspResultCollector.appendDeclaration(out.toString()
195: + "\n", sourceLineNumber); //$NON-NLS-1$
196: }
197: }
198: }
199: }
|