Author: yradtsevich
Date: 2009-04-20 14:51:36 -0400 (Mon, 20 Apr 2009)
New Revision: 14830
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeTemplateFile.java
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeTemplateFileList.java
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeTemplateManager.java
trunk/vpe/tests/org.jboss.tools.vpe.test/src/org/jboss/tools/vpe/editor/template/VpeTemplateManagerTest.java
Log:
RESOLVED - issue JBIDE-4131: Change saving of vpe auto templates
https://jira.jboss.org/jira/browse/JBIDE-4131
- Saving of user-defined templates in the workspace has been implemented.
- JUnit test for the issue has been written.
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeTemplateFile.java
===================================================================
---
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeTemplateFile.java 2009-04-20
18:05:15 UTC (rev 14829)
+++
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeTemplateFile.java 2009-04-20
18:51:36 UTC (rev 14830)
@@ -22,14 +22,18 @@
IConfigurationElement configElement;
VpeTemplateFile(String fileName,IConfigurationElement element) throws IOException {
+ this(VpeTemplateFileList.getFilePath(fileName, element), element);
+ }
+
+ VpeTemplateFile(final IPath path, final IConfigurationElement element) {
this(element);
- path = VpeTemplateFileList.getFilePath(fileName,element);
- File file = path.toFile();
+ this.path = path;
+ final File file = path.toFile();
if (file.exists() && file.isFile()) {
stamp = file.lastModified();
- }
+ }
}
-
+
VpeTemplateFile(IConfigurationElement element) {
configElement = element;
}
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeTemplateFileList.java
===================================================================
---
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeTemplateFileList.java 2009-04-20
18:05:15 UTC (rev 14829)
+++
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeTemplateFileList.java 2009-04-20
18:51:36 UTC (rev 14830)
@@ -38,7 +38,8 @@
void load() {
VpeTemplateFile newAutoTemplateFile = null;
try {
- newAutoTemplateFile = new VpeTemplateFile(VpeTemplateManager.AUTO_TEMPLATES_FILE_NAME,
null);
+ newAutoTemplateFile = new VpeTemplateFile(
+ VpeTemplateManager.getAutoTemplates(), null);
} catch (IOException e) {
VpePlugin.getPluginLog().logError("Default template for unknown tags loading
error ",e);
}
@@ -103,16 +104,12 @@
static IPath getFilePath(String name, IConfigurationElement confElement) throws
IOException {
VpePlugin plugin = VpePlugin.getDefault();
- Bundle bundle =
- confElement==null?plugin.getBundle():Platform.getBundle(confElement.getContributor().getName());
+ Bundle bundle = confElement == null
+ ? plugin.getBundle()
+ : Platform.getBundle(confElement.getContributor().getName());
URL url = bundle.getEntry("/"); //$NON-NLS-1$
IPath path = new Path(FileLocator.toFileURL(url).getFile());
path = path.append(name);
return path;
}
-
- static String getAutoTemplateFileName() throws IOException {
- IPath path = getFilePath(VpeTemplateManager.AUTO_TEMPLATES_FILE_NAME, null);
- return path.toOSString();
- }
}
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeTemplateManager.java
===================================================================
---
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeTemplateManager.java 2009-04-20
18:05:15 UTC (rev 14829)
+++
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeTemplateManager.java 2009-04-20
18:51:36 UTC (rev 14830)
@@ -11,9 +11,12 @@
package org.jboss.tools.vpe.editor.template;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -42,7 +45,10 @@
public class VpeTemplateManager {
- static final String AUTO_TEMPLATES_FILE_NAME =
"templates/vpe-templates-auto.xml"; //$NON-NLS-1$
+ private static final String AUTO_TEMPLATES_DEFAULT_FILE
+ = "templates/vpe-templates-auto.xml"; //$NON-NLS-1$
+ private static final String AUTO_TEMPLATES_WORKSPACE_FILE
+ = "templates/vpe-templates-auto.xml"; //$NON-NLS-1$
static final String TEMPLATES_FOLDER = File.separator + "templates" +
File.separator; //$NON-NLS-1$
public static final String VPE_PREFIX = "vpe:"; //$NON-NLS-1$
@@ -564,7 +570,7 @@
root = appendTaglib(prefixSet, document, root, data);
try {
- IPath path = VpeTemplateFileList.getFilePath(AUTO_TEMPLATES_FILE_NAME,null);
+ IPath path = getAutoTemplates();
XMLUtilities.serialize(root, path.toOSString());
} catch(IOException e) {
VpePlugin.reportProblem(e);
@@ -761,7 +767,7 @@
}
try {
- IPath path = VpeTemplateFileList.getFilePath(AUTO_TEMPLATES_FILE_NAME, null);
+ IPath path = getAutoTemplates();
// fixed bug [EFWPE-869] - uncomment this line
XMLUtilities.serialize(root, path.toOSString());
} catch(IOException e) {
@@ -815,7 +821,7 @@
private Element loadAutoTemplate() {
try {
- IPath path = VpeTemplateFileList.getFilePath(AUTO_TEMPLATES_FILE_NAME, null);
+ IPath path = getAutoTemplates();
Element root = XMLUtilities.getElement(path.toFile(), null);
if (root != null && TAG_TEMPLATES.equals(root.getNodeName())) {
return root;
@@ -1025,4 +1031,53 @@
return defaultTextFormattingData;
}
+
+ /**
+ * Returns the user's template file path.
+ * <P>
+ * If the file does not exist in the workspace, it is created
+ * by copying the default user template file to the workspace.
+ * </P>
+ *
+ * @return user's template file path
+ * @throws IOException
+ *
+ * @see <a
href="https://jira.jboss.org/jira/browse/JBIDE-4131" >
+ JBIDE-4131: Change saving of vpe auto templates</a>
+ */
+ public static IPath getAutoTemplates() throws IOException {
+ final IPath workspaceTemplatePath = VpePlugin.getDefault()
+ .getStateLocation().append(AUTO_TEMPLATES_WORKSPACE_FILE);
+
+ final File workspaceTemplateFile = workspaceTemplatePath.toFile();
+ if (!workspaceTemplateFile.exists()) {
+ final IPath dafaultTemplatePath = VpeTemplateFileList
+ .getFilePath(AUTO_TEMPLATES_DEFAULT_FILE, null);
+ final File defaultTemplateFile = dafaultTemplatePath.toFile();
+ copy(defaultTemplateFile, workspaceTemplateFile);
+ }
+
+ return workspaceTemplatePath;
+ }
+
+ /**
+ * Copies {@code src} file to {@code dst} file.
+ * If the {@code dst} file does not exist, it is created.
+ */
+ private static void copy(File src, File dst) throws IOException {
+ // make sure the parent directory of the dst file is exist
+ dst.getParentFile().mkdirs();
+
+ final InputStream in = new FileInputStream(src);
+ final OutputStream out = new FileOutputStream(dst);
+
+ // Transfer bytes from in to out
+ final byte[] buf = new byte[1024];
+ int len;
+ while ((len = in.read(buf)) > 0) {
+ out.write(buf, 0, len);
+ }
+ in.close();
+ out.close();
+ }
}
Modified:
trunk/vpe/tests/org.jboss.tools.vpe.test/src/org/jboss/tools/vpe/editor/template/VpeTemplateManagerTest.java
===================================================================
---
trunk/vpe/tests/org.jboss.tools.vpe.test/src/org/jboss/tools/vpe/editor/template/VpeTemplateManagerTest.java 2009-04-20
18:05:15 UTC (rev 14829)
+++
trunk/vpe/tests/org.jboss.tools.vpe.test/src/org/jboss/tools/vpe/editor/template/VpeTemplateManagerTest.java 2009-04-20
18:51:36 UTC (rev 14830)
@@ -10,17 +10,24 @@
******************************************************************************/
package org.jboss.tools.vpe.editor.template;
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
import java.util.Collections;
import java.util.List;
import junit.framework.TestCase;
+import org.eclipse.core.runtime.IPath;
+import org.jboss.tools.vpe.test.VpeTestPlugin;
+
/**
* Test for Template Manager
* @author mareshkau
*/
public class VpeTemplateManagerTest extends TestCase {
-
+ private static final String MANDATORY_TEMPLATE_FILE_ENTRY
+ = "<vpe:templates";
private List<VpeAnyData> oldTemplates;
/* (non-Javadoc)
@@ -73,4 +80,57 @@
assertNotNull("TemplateManager.getDefTemplate() cannot return
null",template); //$NON-NLS-1$
}
+ /**
+ * Tests {@link VpeTemplateManager#getAutoTemplates()}
+ */
+ public void testGetAutoTemplates() {
+ final String workspacePath = VpeTestPlugin.getDefault()
+ .getStateLocation().makeAbsolute().removeLastSegments(1)
+ .toPortableString();
+ try {
+ final IPath autoTemplate1 = VpeTemplateManager
+ .getAutoTemplates().makeAbsolute();
+
+ // check if the file is in the workspace
+ assertTrue(
+ autoTemplate1.toPortableString().startsWith(workspacePath));
+
+ // ensure the file is deleted
+ assertTrue("Cannot delete user's templates file.", //$NON-NLS-1$
+ autoTemplate1.toFile().delete());
+
+ final IPath autoTemplate2 = VpeTemplateManager
+ .getAutoTemplates().makeAbsolute();
+
+ // check if the new file has the same path as the old one
+ assertEquals("The path of the user's templates file" //$NON-NLS-1$
+ + "is changed.", //$NON-NLS-1$
+ autoTemplate1, autoTemplate2);
+
+ final BufferedReader in = new BufferedReader(
+ new FileReader(autoTemplate2.toFile()));
+ boolean hasMandatoryEntry = false;
+ String line;
+ while (!hasMandatoryEntry
+ && (line = in.readLine()) != null) {
+ if (line.contains(MANDATORY_TEMPLATE_FILE_ENTRY)) {
+ hasMandatoryEntry = true;
+ }
+ }
+ // check if the file contains MANDATORY_TEMPLATE_FILE_ENTRY
+ assertTrue(
+ "File '" + autoTemplate2.toPortableString() //$NON-NLS-1$
+ + "' does not contain string '" //$NON-NLS-1$
+ + MANDATORY_TEMPLATE_FILE_ENTRY + "'.", //$NON-NLS-1$
+ hasMandatoryEntry);
+
+ in.close();
+
+ // delete the file on exit and check if it is deleted
+ assertTrue("Cannot delete user's templates file.", //$NON-NLS-1$
+ autoTemplate2.toFile().delete());
+ } catch (IOException e) {
+ fail(e.toString());
+ }
+ }
}