summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2006-03-21 10:25:50 (EST)
committerpelder2006-03-21 10:25:50 (EST)
commitdf822e3aadb0ca3cb46a8df766500d84345d84d0 (patch)
treebf4d7c16a44eef8f68aafbc83ff0c783e1201e4f
parentde39cdc66b3c785c5756398973abc8014b42edfc (diff)
downloadorg.eclipse.jet-df822e3aadb0ca3cb46a8df766500d84345d84d0.zip
org.eclipse.jet-df822e3aadb0ca3cb46a8df766500d84345d84d0.tar.gz
org.eclipse.jet-df822e3aadb0ca3cb46a8df766500d84345d84d0.tar.bz2
[1326688">1326688] Add <java:merge> tag
-rw-r--r--plugins/org.eclipse.jet/META-INF/MANIFEST.MF3
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/MergeTag.java196
2 files changed, 198 insertions, 1 deletions
diff --git a/plugins/org.eclipse.jet/META-INF/MANIFEST.MF b/plugins/org.eclipse.jet/META-INF/MANIFEST.MF
index f7c18d4..40c9f10 100644
--- a/plugins/org.eclipse.jet/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.jet/META-INF/MANIFEST.MF
@@ -15,7 +15,8 @@ Require-Bundle: org.eclipse.ant.core,
org.eclipse.emf.ecore.xmi,
org.eclipse.text;visibility:=reexport,
org.eclipse.core.expressions,
- org.eclipse.core.filebuffers
+ org.eclipse.core.filebuffers,
+ org.eclipse.jdt.core
Eclipse-AutoStart: true
Export-Package: org.eclipse.jet,
org.eclipse.jet.compiler,
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/MergeTag.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/MergeTag.java
new file mode 100644
index 0000000..85e9344
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/MergeTag.java
@@ -0,0 +1,196 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id$
+ */
+package org.eclipse.jet.internal.taglib.java;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.text.MessageFormat;
+
+import org.eclipse.core.filebuffers.FileBuffers;
+import org.eclipse.core.filebuffers.ITextFileBuffer;
+import org.eclipse.core.filebuffers.ITextFileBufferManager;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.emf.codegen.jmerge.JControlModel;
+import org.eclipse.emf.codegen.jmerge.JMerger;
+import org.eclipse.emf.codegen.merge.java.facade.jdom.JDOMFacadeHelper;
+import org.eclipse.jdt.core.jdom.DOMFactory;
+import org.eclipse.jdt.core.jdom.IDOMCompilationUnit;
+import org.eclipse.jdt.core.jdom.IDOMFactory;
+import org.eclipse.jet.IWriterListener;
+import org.eclipse.jet.JET2Context;
+import org.eclipse.jet.JET2Writer;
+import org.eclipse.jet.taglib.AbstractEmptyTag;
+import org.eclipse.jet.taglib.JET2TagException;
+import org.eclipse.jet.taglib.TagInfo;
+import org.eclipse.jet.transform.TransformContextExtender;
+import org.eclipse.jface.text.Position;
+
+/**
+ * Implement the Java tag &lt;java:merge&gt;.
+ */
+public class MergeTag extends AbstractEmptyTag
+{
+
+ /**
+ * Position marker for JMergeable content.
+ */
+ public static class MergePosition extends Position
+ {
+
+ private final URL rulesURL;
+
+ public MergePosition(URL rulesURL, int offset)
+ {
+ super(offset);
+ this.rulesURL = rulesURL;
+
+ }
+
+ /**
+ * @return Returns the rulesURL.
+ */
+ public final URL getRulesURL()
+ {
+ return rulesURL;
+ }
+
+ }
+
+/**
+ * Process a JMergeTag on the template content.
+ */
+ public static class JMergeProcessor implements IWriterListener
+ {
+
+ /**
+ *
+ */
+ public JMergeProcessor()
+ {
+ super();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.IWriterListener#finalizeContent(org.eclipse.jet.JET2Writer, java.lang.Object)
+ */
+ public void finalizeContent(JET2Writer writer, Object file) throws JET2TagException
+ {
+ if(file instanceof IFile)
+ {
+ IFile iFile = (IFile)file;
+ if(!"java".equals(iFile.getFileExtension())) //$NON-NLS-1$
+ {
+ throw new JET2TagException("This tag is only applicable to Java files.");
+ }
+
+ if(!iFile.exists())
+ {
+ return;
+ }
+
+ final Position[] positions = writer.getPositions(JMERGE_POSITION_CATEGORY);
+
+ final ITextFileBufferManager textFileBufferManager = FileBuffers.getTextFileBufferManager();
+ try
+ {
+ textFileBufferManager.connect(iFile.getLocation(), new NullProgressMonitor());
+ try
+ {
+
+ final ITextFileBuffer textFileBuffer = textFileBufferManager.getTextFileBuffer(iFile.getLocation());
+ IDOMFactory domFactory = new DOMFactory();
+
+ IDOMCompilationUnit sourceCU = domFactory.createCompilationUnit(
+ writer.toString(), iFile.getName());
+
+ IDOMCompilationUnit targetCU = domFactory.createCompilationUnit(
+ textFileBuffer.getDocument().get(), iFile.getName());
+ JControlModel controlModel = new JControlModel(((MergePosition)positions[0]).getRulesURL().toExternalForm());
+ JMerger merger = new JMerger(controlModel , sourceCU, targetCU);
+
+ merger.merge();
+
+ writer.getDocument().set(targetCU.getContents());
+ }
+ finally
+ {
+ textFileBufferManager.disconnect(iFile.getLocation(), new NullProgressMonitor());
+ }
+ }
+ catch (CoreException e)
+ {
+ final String msg = "Unable to read existing java file {0}";
+ throw new JET2TagException(MessageFormat.format(msg, new Object[] {iFile.getFullPath()}));
+ }
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.IWriterListener#postCommitContent(org.eclipse.jet.JET2Writer, java.lang.Object)
+ */
+ public void postCommitContent(JET2Writer writer, Object file)
+ {
+ // nothing to do.
+ }
+
+ }
+
+private static final String JMERGE_POSITION_CATEGORY = MergeTag.class.getName();;
+
+ /**
+ *
+ */
+ public MergeTag()
+ {
+ super();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.taglib.EmptyTag#doAction(org.eclipse.jet.taglib.TagInfo, org.eclipse.jet.JET2Context, org.eclipse.jet.JET2Writer)
+ */
+ public void doAction(TagInfo td, JET2Context context, JET2Writer out) throws JET2TagException
+ {
+ String rules = getAttribute("rules"); //$NON-NLS-1$
+ String rulesContext = getAttribute("rulesContext"); //$NON-NLS-1$
+ if(rulesContext == null)
+ {
+ rulesContext = "transform"; //$NON-NLS-1$
+ }
+
+ final URL baseURL = TransformContextExtender.getInstance(context).getBaseURL(rulesContext);
+
+ try
+ {
+ URL rulesURL = new URL(baseURL, rules);
+
+ out.getDocument().addPositionCategory(JMERGE_POSITION_CATEGORY);
+
+ out.addPosition(JMERGE_POSITION_CATEGORY, new MergePosition(rulesURL, out.getDocument().getLength()));
+
+ out.addEventListener(JMERGE_POSITION_CATEGORY, new JMergeProcessor());
+ }
+ catch (MalformedURLException e)
+ {
+ String msg = "Could not create URL({0}, {1})";
+ throw new JET2TagException(MessageFormat.format(msg,
+ new Object[] {baseURL.toExternalForm(), rules}));
+ }
+ }
+
+}