? openide/text/lib
Index: openide/arch/arch-openide-datasystems.xml
===================================================================
RCS file: /cvs/openide/arch/arch-openide-datasystems.xml,v
retrieving revision 1.37
diff -u -u -r1.37 arch-openide-datasystems.xml
--- openide/arch/arch-openide-datasystems.xml 23 Mar 2007 16:23:14 -0000 1.37
+++ openide/arch/arch-openide-datasystems.xml 30 Jul 2007 14:48:27 -0000
@@ -138,6 +138,7 @@
contains the name of the DataObject that is being created
contains String
representing the current day like 23. 3. 2007
contains String
the current time like 17:18:30
+ contains String
the file encoding of the template instance
Index: openide/loaders/manifest.mf
===================================================================
RCS file: /cvs/openide/loaders/manifest.mf,v
retrieving revision 1.34
diff -u -u -r1.34 manifest.mf
--- openide/loaders/manifest.mf 16 Jun 2007 10:08:26 -0000 1.34
+++ openide/loaders/manifest.mf 30 Jul 2007 14:48:27 -0000
@@ -1,5 +1,5 @@
Manifest-Version: 1.0
OpenIDE-Module: org.openide.loaders
-OpenIDE-Module-Specification-Version: 6.5
+OpenIDE-Module-Specification-Version: 6.6
OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties
-
+OpenIDE-Module-Recommends: openide_templates_1_0
Index: openide/loaders/api/apichanges.xml
===================================================================
RCS file: /cvs/openide/loaders/api/apichanges.xml,v
retrieving revision 1.30
diff -u -u -r1.30 apichanges.xml
--- openide/loaders/api/apichanges.xml 16 Jun 2007 10:08:26 -0000 1.30
+++ openide/loaders/api/apichanges.xml 30 Jul 2007 14:48:27 -0000
@@ -84,6 +84,22 @@
+
+
+ Templates may use ${encoding} property in their bodies
+
+
+
+
+
+
+ Templates may use ${encoding} property in their bodies to be substituted
+ to the real encoding of the template instance file.
+
+
+
+
+
Folders may be ordered by numeric position
Index: openide/templates/manifest.mf
===================================================================
RCS file: /cvs/openide/templates/manifest.mf,v
retrieving revision 1.3
diff -u -u -r1.3 manifest.mf
--- openide/templates/manifest.mf 14 Feb 2007 20:52:49 -0000 1.3
+++ openide/templates/manifest.mf 30 Jul 2007 14:48:28 -0000
@@ -2,4 +2,4 @@
OpenIDE-Module: org.netbeans.modules.templates
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/templates/Bundle.properties
OpenIDE-Module-Specification-Version: 1.0
-
+OpenIDE-Module-Provides: openide_templates_1_0
Index: openide/templates/src/org/netbeans/modules/templates/ScriptingCreateFromTemplateHandler.java
===================================================================
RCS file: /cvs/openide/templates/src/org/netbeans/modules/templates/ScriptingCreateFromTemplateHandler.java,v
retrieving revision 1.3
diff -u -u -r1.3 ScriptingCreateFromTemplateHandler.java
--- openide/templates/src/org/netbeans/modules/templates/ScriptingCreateFromTemplateHandler.java 22 Mar 2007 09:19:43 -0000 1.3
+++ openide/templates/src/org/netbeans/modules/templates/ScriptingCreateFromTemplateHandler.java 30 Jul 2007 14:48:28 -0000
@@ -50,6 +50,8 @@
private static ScriptEngineManager manager;
private static final Logger LOG = Logger.getLogger(ScriptingCreateFromTemplateHandler.class.getName());
+ private static final String ENCODING_PROPERTY_NAME = "encoding"; //NOI18N
+
protected boolean accept(FileObject orig) {
return engine(orig) != null;
}
@@ -58,17 +60,18 @@
String name,
Map values) throws IOException {
- ScriptEngine eng = engine(template);
- Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
- bind.putAll(values);
- for (Map.Entry entry : values.entrySet()) {
- eng.getContext().setAttribute(entry.getKey(), entry.getValue(), ScriptContext.ENGINE_SCOPE);
- }
-
String nameUniq = FileUtil.findFreeFileName(f, name, template.getExt());
FileObject output = FileUtil.createData(f, nameUniq + '.' + template.getExt());
Charset targetEnc = FileEncodingQuery.getEncoding(output);
Charset sourceEnc = FileEncodingQuery.getEncoding(template);
+
+ ScriptEngine eng = engine(template);
+ Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
+ bind.putAll(values);
+
+ if(!values.containsKey(ENCODING_PROPERTY_NAME)) {
+ bind.put(ENCODING_PROPERTY_NAME, targetEnc.name());
+ }
Writer w = null;
Reader is = null;
Index: openide/templates/test/unit/src/org/netbeans/modules/templates/ScriptingCreateFromTemplateTest.java
===================================================================
RCS file: openide/templates/test/unit/src/org/netbeans/modules/templates/ScriptingCreateFromTemplateTest.java
diff -N openide/templates/test/unit/src/org/netbeans/modules/templates/ScriptingCreateFromTemplateTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ openide/templates/test/unit/src/org/netbeans/modules/templates/ScriptingCreateFromTemplateTest.java 30 Jul 2007 14:48:28 -0000
@@ -0,0 +1,137 @@
+/*
+ * The contents of this file are subject to the terms of the Common Development
+ * and Distribution License (the License). You may not use this file except in
+ * compliance with the License.
+ *
+ * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
+ * or http://www.netbeans.org/cddl.txt.
+ *
+ * When distributing Covered Code, include this CDDL Header Notice in each file
+ * and include the License file at http://www.netbeans.org/cddl.txt.
+ * If applicable, add the following below the CDDL Header, with the fields
+ * enclosed by brackets [] replaced by your own identifying information:
+ * "Portions Copyrighted [year] [name of copyright owner]"
+ *
+ * The Original Software is NetBeans. The Initial Developer of the Original
+ * Software is Sun Microsystems, Inc.
+ *
+ * Portions Copyrighted 2007 Sun Microsystems, Inc.
+ */
+
+package org.netbeans.modules.templates;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.charset.Charset;
+import java.util.Collections;
+import java.util.Collections;
+import java.util.Map;
+import org.netbeans.api.queries.FileEncodingQuery;
+import org.netbeans.junit.MockServices;
+import org.netbeans.junit.NbTestCase;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.loaders.DataFolder;
+import org.openide.loaders.DataObject;
+import org.openide.loaders.DataObjectExistsException;
+import org.openide.loaders.FileEntry;
+import org.openide.loaders.MultiDataObject;
+import org.openide.loaders.MultiFileLoader;
+
+/**
+ *
+ * @author Marek Fukala
+ * @author Jaroslav Tulach
+ */
+public class ScriptingCreateFromTemplateTest extends NbTestCase {
+
+ public ScriptingCreateFromTemplateTest(String testName) {
+ super(testName);
+ }
+
+ @Override
+ protected boolean runInEQ() {
+ return true;
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ MockServices.setServices(SimpleLoader.class);
+ }
+
+ public void testCreateFromTemplateEndcodingProperty() throws Exception {
+ FileObject root = FileUtil.createMemoryFileSystem().getRoot();
+ FileObject fo = FileUtil.createData(root, "simpleObject.txt");
+ OutputStream os = fo.getOutputStream();
+ os.write("${encoding}".getBytes());
+ os.close();
+ fo.setAttribute ("template", Boolean.TRUE);
+ fo.setAttribute("javax.script.ScriptEngine", "freemarker");
+
+ DataObject obj = DataObject.find(fo);
+ DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target"));
+
+ Map parameters = Collections.emptyMap();
+ DataObject inst = obj.createFromTemplate(folder, "complex", parameters);
+
+ FileObject instFO = inst.getPrimaryFile();
+
+ Charset targetEnc = FileEncodingQuery.getEncoding(instFO);
+ assertNotNull("Template encoding is null", targetEnc);
+ assertEquals("Encoding in template doesn't match", targetEnc.name(), readFile(instFO));
+ }
+
+ private static String readFile(FileObject fo) throws IOException {
+ byte[] arr = new byte[(int)fo.getSize()];
+ int len = fo.getInputStream().read(arr);
+ assertEquals("Fully read", arr.length, len);
+ return new String(arr);
+ }
+
+ public static final class SimpleLoader extends MultiFileLoader {
+ public SimpleLoader() {
+ super(SimpleObject.class.getName());
+ }
+ protected String displayName() {
+ return "SimpleLoader";
+ }
+ protected FileObject findPrimaryFile(FileObject fo) {
+ if (fo.hasExt("prima")) {
+ return fo;
+ }
+ return null;
+ }
+ protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException {
+ return new SimpleObject(this, primaryFile);
+ }
+ protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
+ return new FE(obj, primaryFile);
+ }
+ protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) {
+ return new FileEntry(obj, secondaryFile);
+ }
+ }
+
+ private static final class FE extends FileEntry {
+ public FE(MultiDataObject mo, FileObject fo) {
+ super(mo, fo);
+ }
+
+ @Override
+ public FileObject createFromTemplate(FileObject f, String name) throws IOException {
+ fail("I do not want to be called");
+ return null;
+ }
+ }
+
+ public static final class SimpleObject extends MultiDataObject {
+ public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException {
+ super(fo, l);
+ }
+
+ public String getName() {
+ return getPrimaryFile().getNameExt();
+ }
+ }
+
+}