summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2006-03-21 12:05:50 (EST)
committer pelder2006-03-21 12:05:50 (EST)
commit2916afaa773b72142601af76f2b71310eb9f1294 (patch)
treef185e13d55383e9737bc3e3a2f55071e48105f0d
parentbea4ac33c408a1f30ea2246abbe5ab34181a2596 (diff)
downloadorg.eclipse.jet-2916afaa773b72142601af76f2b71310eb9f1294.zip
org.eclipse.jet-2916afaa773b72142601af76f2b71310eb9f1294.tar.gz
org.eclipse.jet-2916afaa773b72142601af76f2b71310eb9f1294.tar.bz2
[132691] Control tag clean-up.
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/control/InitialCodeTag.java6
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/control/UserRegionTag.java148
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/UserRegionHelper.java182
3 files changed, 186 insertions, 150 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/control/InitialCodeTag.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/control/InitialCodeTag.java
index 197c8d5..22c4673 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/control/InitialCodeTag.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/control/InitialCodeTag.java
@@ -25,7 +25,7 @@ import org.eclipse.jet.internal.l10n.JET2Messages;
import org.eclipse.jet.taglib.AbstractContainerTag;
import org.eclipse.jet.taglib.JET2TagException;
import org.eclipse.jet.taglib.TagInfo;
-import org.eclipse.jface.text.Position;
+import org.eclipse.jet.taglib.UserRegionHelper;
/**
@@ -64,9 +64,7 @@ public class InitialCodeTag extends AbstractContainerTag
throw new JET2TagException(MessageFormat.format(JET2Messages.AnyTag_IllegalParent, new Object []{ "userRegion" })); //$NON-NLS-1$
}
- int initialCodeEnd = out.getDocument().getLength();
- out.addPositionCategory(UserRegionTag.INITIALCODE_POSITION_CATEGORY);
- out.addPosition(UserRegionTag.INITIALCODE_POSITION_CATEGORY, new Position(initialCodeOffset, initialCodeEnd - initialCodeOffset));
+ UserRegionHelper.markInitialCode(out, initialCodeOffset, out.getDocument().getLength());
}
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/control/UserRegionTag.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/control/UserRegionTag.java
index 4e8851e..f0a839f 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/control/UserRegionTag.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/control/UserRegionTag.java
@@ -17,24 +17,13 @@
package org.eclipse.jet.internal.taglib.control;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.jet.IWriterListener;
import org.eclipse.jet.JET2Context;
import org.eclipse.jet.JET2Writer;
-import org.eclipse.jet.internal.InternalJET2Platform;
import org.eclipse.jet.taglib.AbstractContainerTag;
import org.eclipse.jet.taglib.JET2TagException;
import org.eclipse.jet.taglib.TagInfo;
-import org.eclipse.jface.text.BadLocationException;
-import org.eclipse.jface.text.IDocument;
-import org.eclipse.jface.text.Position;
+import org.eclipse.jet.taglib.UserRegionHelper;
/**
@@ -44,135 +33,6 @@ import org.eclipse.jface.text.Position;
public class UserRegionTag extends AbstractContainerTag
{
- /**
- * JET2Writer listener that implements user regions.
- *
- */
- private static final class UserRegionWriteListener implements IWriterListener
- {
- public void finalizeContent(JET2Writer writer, Object file)
- {
- if (file instanceof IFile)
- {
- IFile iFile = (IFile)file;
- if (iFile.exists())
- {
- String fileContents;
- try
- {
- fileContents = getContents(iFile.getContents(), iFile.getCharset());
- }
- catch (CoreException e)
- {
- return;
- }
-
- Position[] rtPositions = writer.getPositions(USERREGION_POSITION_CATEGORY);
- Position[] icPositions = writer.getPositions(INITIALCODE_POSITION_CATEGORY);
- IDocument document = writer.getDocument();
- for (int i = 0; i < rtPositions.length; i++)
- {
- try
- {
- // don't assume there will be an initial code
- // position, find it...
- final Position userRegionPosition = rtPositions[i];
- Position initialCodePosition = findContainedInitialCodePosition(userRegionPosition, icPositions);
-
- final Position beginMarkerPosition = new Position(userRegionPosition.offset, initialCodePosition.offset
- - userRegionPosition.offset);
- int endMarkerOffset = initialCodePosition.offset + initialCodePosition.length;
- final Position endMarkerPosition = new Position(endMarkerOffset, userRegionPosition.offset + userRegionPosition.length
- - endMarkerOffset);
-
- final String beginMarker = document.get(beginMarkerPosition.offset, beginMarkerPosition.length);
- final String endMarker = document.get(endMarkerPosition.offset, endMarkerPosition.length);
-
- final int existingRegionOffset = fileContents.indexOf(beginMarker);
- if (existingRegionOffset != -1)
- {
- final int existingEndMarkerOffset = fileContents.indexOf(endMarker, existingRegionOffset + beginMarker.length());
- if (existingEndMarkerOffset != -1)
- {
- final String existingRegion = fileContents.substring(existingRegionOffset, existingEndMarkerOffset + endMarker.length());
- document.replace(userRegionPosition.offset, userRegionPosition.length, existingRegion);
- }
- }
-
- }
- catch (BadLocationException e)
- {
- // should not happen
- throw new RuntimeException(e);
- }
- }
-
- }
- }
-
- }
-
- private Position findContainedInitialCodePosition(Position userRegionPosition, Position[] icPositions)
- {
- // simple linear search. Could be faster as a binary search...
- for (int i = 0; i < icPositions.length; i++)
- {
- Position pos = icPositions[i];
- if (pos.offset + pos.length > userRegionPosition.offset + userRegionPosition.length)
- {
- return null;
- }
- else if (userRegionPosition.offset <= pos.offset)
- {
- return pos;
- }
- }
- return null;
- }
-
- private String getContents(InputStream in, String charset) throws CoreException
- {
- try
- {
- InputStreamReader reader = new InputStreamReader(in, charset);
- StringBuffer contents = new StringBuffer();
- char[] buffer = new char [1024];
- while (true)
- {
- int read = reader.read(buffer);
- if (read > 0)
- {
- contents.append(buffer, 0, read);
- }
- else
- {
- break;
- }
- }
- return contents.toString();
- }
- catch (UnsupportedEncodingException e)
- {
- throw new CoreException(InternalJET2Platform.newStatus(IStatus.ERROR, e.getLocalizedMessage(), e));
- }
- catch (IOException e)
- {
- throw new CoreException(InternalJET2Platform.newStatus(IStatus.ERROR, e.getLocalizedMessage(), e));
- }
- }
-
- public void postCommitContent(JET2Writer writer, Object file)
- {
- // nothing to do here...
- }
- }
-
- public static final String INITIALCODE_POSITION_CATEGORY = "org.eclipse.jet.internal.taglib.control.initialcode"; //$NON-NLS-1$
-
- private static final String ROUNDTRIP_LISTENER_CATEGORY = "org.eclipse.jet.internal.taglib.control.userRegion"; //$NON-NLS-1$
-
- public static final String USERREGION_POSITION_CATEGORY = "org.eclipse.jet.internal.taglib.control.userRegion"; //$NON-NLS-1$
-
private int userRegionOffset;
/**
@@ -188,11 +48,7 @@ public class UserRegionTag extends AbstractContainerTag
*/
public void doAfterBody(TagInfo td, JET2Context context, JET2Writer out) throws JET2TagException
{
- int userRegionEnd = out.getDocument().getLength();
- out.addPositionCategory(USERREGION_POSITION_CATEGORY);
- out.addPosition(USERREGION_POSITION_CATEGORY, new Position(userRegionOffset, userRegionEnd - userRegionOffset));
-
- out.addEventListener(ROUNDTRIP_LISTENER_CATEGORY, new UserRegionWriteListener());
+ UserRegionHelper.markUserRegion(out, userRegionOffset, out.getDocument().getLength());
}
/* (non-Javadoc)
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/UserRegionHelper.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/UserRegionHelper.java
new file mode 100644
index 0000000..fe68c3b
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/UserRegionHelper.java
@@ -0,0 +1,182 @@
+package org.eclipse.jet.taglib;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.jet.IWriterListener;
+import org.eclipse.jet.JET2Writer;
+import org.eclipse.jet.internal.InternalJET2Platform;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.Position;
+
+/**
+ * Utility class that allows implementation of user regions like &lt;c:userRegion&;gt.
+ * <p>
+ * To consume this class, the document must include two regions, one nested inside the
+ * other. Two static methods are used to mark these regions:
+ * <bl>
+ * <li> {@link #markUserRegion(JET2Writer, int, int) markUserRegion()}</li>
+ * <li> {@link #markInitialCode(JET2Writer, int, int) markInitialCode()}</li>
+ * </bl>
+ * </p>
+ *
+ */
+public final class UserRegionHelper implements IWriterListener
+{
+ private static final String INITIALCODE_POSITION_CATEGORY = "org.eclipse.jet.internal.taglib.control.initialcode"; //$NON-NLS-1$
+ private static final String ROUNDTRIP_LISTENER_CATEGORY = "org.eclipse.jet.internal.taglib.control.userRegion"; //$NON-NLS-1$
+ private static final String USERREGION_POSITION_CATEGORY = "org.eclipse.jet.internal.taglib.control.userRegion"; //$NON-NLS-1$
+
+ private UserRegionHelper()
+ {
+ // do nothing, but prevent external construction.
+ }
+
+ public void finalizeContent(JET2Writer writer, Object file)
+ {
+ if (file instanceof IFile)
+ {
+ IFile iFile = (IFile)file;
+ if (iFile.exists())
+ {
+ String fileContents;
+ try
+ {
+ fileContents = getContents(iFile.getContents(), iFile.getCharset());
+ }
+ catch (CoreException e)
+ {
+ return;
+ }
+
+ Position[] rtPositions = writer.getPositions(UserRegionHelper.USERREGION_POSITION_CATEGORY);
+ Position[] icPositions = writer.getPositions(UserRegionHelper.INITIALCODE_POSITION_CATEGORY);
+ IDocument document = writer.getDocument();
+ for (int i = 0; i < rtPositions.length; i++)
+ {
+ try
+ {
+ // don't assume there will be an initial code
+ // position, find it...
+ final Position userRegionPosition = rtPositions[i];
+ Position initialCodePosition = findContainedInitialCodePosition(userRegionPosition, icPositions);
+
+ final Position beginMarkerPosition = new Position(userRegionPosition.offset, initialCodePosition.offset
+ - userRegionPosition.offset);
+ int endMarkerOffset = initialCodePosition.offset + initialCodePosition.length;
+ final Position endMarkerPosition = new Position(endMarkerOffset, userRegionPosition.offset + userRegionPosition.length
+ - endMarkerOffset);
+
+ final String beginMarker = document.get(beginMarkerPosition.offset, beginMarkerPosition.length);
+ final String endMarker = document.get(endMarkerPosition.offset, endMarkerPosition.length);
+
+ final int existingRegionOffset = fileContents.indexOf(beginMarker);
+ if (existingRegionOffset != -1)
+ {
+ final int existingEndMarkerOffset = fileContents.indexOf(endMarker, existingRegionOffset + beginMarker.length());
+ if (existingEndMarkerOffset != -1)
+ {
+ final String existingRegion = fileContents.substring(existingRegionOffset, existingEndMarkerOffset + endMarker.length());
+ document.replace(userRegionPosition.offset, userRegionPosition.length, existingRegion);
+ }
+ }
+
+ }
+ catch (BadLocationException e)
+ {
+ // should not happen
+ throw new RuntimeException(e);
+ }
+ }
+
+ }
+ }
+
+ }
+
+ private Position findContainedInitialCodePosition(Position userRegionPosition, Position[] icPositions)
+ {
+ // simple linear search. Could be faster as a binary search...
+ for (int i = 0; i < icPositions.length; i++)
+ {
+ Position pos = icPositions[i];
+ if (pos.offset + pos.length > userRegionPosition.offset + userRegionPosition.length)
+ {
+ return null;
+ }
+ else if (userRegionPosition.offset <= pos.offset)
+ {
+ return pos;
+ }
+ }
+ return null;
+ }
+
+ private String getContents(InputStream in, String charset) throws CoreException
+ {
+ try
+ {
+ InputStreamReader reader = new InputStreamReader(in, charset);
+ StringBuffer contents = new StringBuffer();
+ char[] buffer = new char [1024];
+ while (true)
+ {
+ int read = reader.read(buffer);
+ if (read > 0)
+ {
+ contents.append(buffer, 0, read);
+ }
+ else
+ {
+ break;
+ }
+ }
+ return contents.toString();
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ throw new CoreException(InternalJET2Platform.newStatus(IStatus.ERROR, e.getLocalizedMessage(), e));
+ }
+ catch (IOException e)
+ {
+ throw new CoreException(InternalJET2Platform.newStatus(IStatus.ERROR, e.getLocalizedMessage(), e));
+ }
+ }
+
+ public void postCommitContent(JET2Writer writer, Object file)
+ {
+ // nothing to do here...
+ }
+
+ /**
+ * Mark the initial code portion of the user region.
+ * @param out a JET2Writer
+ * @param initialCodeStart the inclusive offset (zero-based) of the start of the region's initial code block (immediately after the start tag).
+ * @param initialCodeEnd the exclusive offset (zero-based) of the end of the region's initial code block (offset of the end tag).
+ */
+ public static void markInitialCode(JET2Writer out, final int initialCodeStart, int initialCodeEnd)
+ {
+ out.addPositionCategory(INITIALCODE_POSITION_CATEGORY);
+ out.addPosition(INITIALCODE_POSITION_CATEGORY, new Position(initialCodeStart, initialCodeEnd - initialCodeStart));
+ }
+
+ /**
+ * Mark the user region on the output writer
+ * @param out a JET2Writer
+ * @param regionStart the inclusive offset (zero-based) of the start of the region's content (immediately after the start tag).
+ * @param regionEnd the exclusive offset (zero-based) of the end of the region's content (offset of the end tag).
+ */
+ public static void markUserRegion(JET2Writer out, int regionStart, int regionEnd)
+ {
+ out.addPositionCategory(USERREGION_POSITION_CATEGORY);
+ out.addPosition(USERREGION_POSITION_CATEGORY, new Position(regionStart, regionEnd - regionStart));
+
+ out.addEventListener(ROUNDTRIP_LISTENER_CATEGORY, new UserRegionHelper());
+ }
+} \ No newline at end of file