This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 110910
Collapse All | Expand All

(-)openide/arch/arch-openide-datasystems.xml (+1 lines)
Lines 138-143 Link Here
138
            <li><api name="name" category="stable" group="property" type="export"> contains the name of the <a href="@TOP@/org/openide/loaders/DataObject.html">DataObject</a> that is being created</api></li>
138
            <li><api name="name" category="stable" group="property" type="export"> contains the name of the <a href="@TOP@/org/openide/loaders/DataObject.html">DataObject</a> that is being created</api></li>
139
            <li><api name="date" category="stable" group="property" type="export"> contains <code>String</code> representing the current day like <code>23. 3. 2007</code></api></li>
139
            <li><api name="date" category="stable" group="property" type="export"> contains <code>String</code> representing the current day like <code>23. 3. 2007</code></api></li>
140
            <li><api name="time" category="stable" group="property" type="export"> contains <code>String</code> the current time like <code>17:18:30</code></api></li>
140
            <li><api name="time" category="stable" group="property" type="export"> contains <code>String</code> the current time like <code>17:18:30</code></api></li>
141
            <li><api name="encoding" category="stable" group="property" type="export"> contains <code>String</code> the file encoding of the template instance</api></li>
141
        </ul>
142
        </ul>
142
        
143
        
143
        <p>
144
        <p>
(-)openide/loaders/manifest.mf (-2 / +2 lines)
Lines 1-5 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.openide.loaders
2
OpenIDE-Module: org.openide.loaders
3
OpenIDE-Module-Specification-Version: 6.5
3
OpenIDE-Module-Specification-Version: 6.6
4
OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties
4
OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties
5
5
OpenIDE-Module-Recommends: openide_templates_1_0
(-)openide/loaders/api/apichanges.xml (+16 lines)
Lines 84-89 Link Here
84
84
85
  <changes>
85
  <changes>
86
86
87
     <change id="templates.encoding">
88
        <api name="loaders"/>
89
        <summary>Templates may use ${encoding} property in their bodies</summary>
90
        <version major="6" minor="6"/>
91
        <date day="1" month="8" year="2007"/>
92
        <author login="mfukala"/>
93
        <compatibility semantic="compatible"/>
94
        <description>
95
            <p>
96
                Templates may use ${encoding} property in their bodies to be substituted 
97
                to the real encoding of the template instance file.
98
            </p>
99
        </description>
100
        <issue number="110910"/>
101
    </change>
102
      
87
    <change id="DataFolder.position">
103
    <change id="DataFolder.position">
88
        <api name="loaders"/>
104
        <api name="loaders"/>
89
        <summary>Folders may be ordered by numeric position</summary>
105
        <summary>Folders may be ordered by numeric position</summary>
(-)openide/templates/manifest.mf (-1 / +1 lines)
Lines 2-5 Link Here
2
OpenIDE-Module: org.netbeans.modules.templates
2
OpenIDE-Module: org.netbeans.modules.templates
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/templates/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/templates/Bundle.properties
4
OpenIDE-Module-Specification-Version: 1.0
4
OpenIDE-Module-Specification-Version: 1.0
5
5
OpenIDE-Module-Provides: openide_templates_1_0
(-)openide/templates/src/org/netbeans/modules/templates/ScriptingCreateFromTemplateHandler.java (-7 / +10 lines)
Lines 50-55 Link Here
50
    private static ScriptEngineManager manager;
50
    private static ScriptEngineManager manager;
51
    private static final Logger LOG = Logger.getLogger(ScriptingCreateFromTemplateHandler.class.getName());
51
    private static final Logger LOG = Logger.getLogger(ScriptingCreateFromTemplateHandler.class.getName());
52
    
52
    
53
    private static final String ENCODING_PROPERTY_NAME = "encoding"; //NOI18N
54
    
53
    protected boolean accept(FileObject orig) {
55
    protected boolean accept(FileObject orig) {
54
        return engine(orig) != null;
56
        return engine(orig) != null;
55
    }
57
    }
Lines 58-74 Link Here
58
                                            String name,
60
                                            String name,
59
                                            Map<String, Object> values) throws IOException {
61
                                            Map<String, Object> values) throws IOException {
60
        
62
        
61
        ScriptEngine eng = engine(template);
62
        Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
63
        bind.putAll(values);
64
        for (Map.Entry<String, Object> entry : values.entrySet()) {
65
            eng.getContext().setAttribute(entry.getKey(), entry.getValue(), ScriptContext.ENGINE_SCOPE);
66
        }
67
        
68
        String nameUniq = FileUtil.findFreeFileName(f, name, template.getExt());
63
        String nameUniq = FileUtil.findFreeFileName(f, name, template.getExt());
69
        FileObject output = FileUtil.createData(f, nameUniq + '.' + template.getExt());
64
        FileObject output = FileUtil.createData(f, nameUniq + '.' + template.getExt());
70
        Charset targetEnc = FileEncodingQuery.getEncoding(output);
65
        Charset targetEnc = FileEncodingQuery.getEncoding(output);
71
        Charset sourceEnc = FileEncodingQuery.getEncoding(template);
66
        Charset sourceEnc = FileEncodingQuery.getEncoding(template);
67
        
68
        ScriptEngine eng = engine(template);
69
        Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
70
        bind.putAll(values);
71
        
72
        if(!values.containsKey(ENCODING_PROPERTY_NAME)) {
73
            bind.put(ENCODING_PROPERTY_NAME, targetEnc.name());
74
        }
72
        
75
        
73
        Writer w = null;
76
        Writer w = null;
74
        Reader is = null;
77
        Reader is = null;
(-)openide/templates/test/unit/src/org/netbeans/modules/templates/ScriptingCreateFromTemplateTest.java (+137 lines)
Added Link Here
1
/*
2
 * The contents of this file are subject to the terms of the Common Development
3
 * and Distribution License (the License). You may not use this file except in
4
 * compliance with the License.
5
 *
6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7
 * or http://www.netbeans.org/cddl.txt.
8
 *
9
 * When distributing Covered Code, include this CDDL Header Notice in each file
10
 * and include the License file at http://www.netbeans.org/cddl.txt.
11
 * If applicable, add the following below the CDDL Header, with the fields
12
 * enclosed by brackets [] replaced by your own identifying information:
13
 * "Portions Copyrighted [year] [name of copyright owner]"
14
 *
15
 * The Original Software is NetBeans. The Initial Developer of the Original
16
 * Software is Sun Microsystems, Inc. 
17
 *
18
 * Portions Copyrighted 2007 Sun Microsystems, Inc.
19
 */
20
21
package org.netbeans.modules.templates;
22
23
import java.io.IOException;
24
import java.io.OutputStream;
25
import java.nio.charset.Charset;
26
import java.util.Collections;
27
import java.util.Collections;
28
import java.util.Map;
29
import org.netbeans.api.queries.FileEncodingQuery;
30
import org.netbeans.junit.MockServices;
31
import org.netbeans.junit.NbTestCase;
32
import org.openide.filesystems.FileObject;
33
import org.openide.filesystems.FileUtil;
34
import org.openide.loaders.DataFolder;
35
import org.openide.loaders.DataObject;
36
import org.openide.loaders.DataObjectExistsException;
37
import org.openide.loaders.FileEntry;
38
import org.openide.loaders.MultiDataObject;
39
import org.openide.loaders.MultiFileLoader;
40
41
/**
42
 *
43
 * @author Marek Fukala
44
 * @author Jaroslav Tulach
45
 */
46
public class ScriptingCreateFromTemplateTest extends NbTestCase {
47
    
48
    public ScriptingCreateFromTemplateTest(String testName) {
49
        super(testName);
50
    }
51
    
52
    @Override
53
    protected boolean runInEQ() {
54
        return true;
55
    }
56
    
57
    @Override
58
    protected void setUp() throws Exception {
59
        MockServices.setServices(SimpleLoader.class);
60
    }
61
62
    public void testCreateFromTemplateEndcodingProperty() throws Exception {
63
        FileObject root = FileUtil.createMemoryFileSystem().getRoot();
64
        FileObject fo = FileUtil.createData(root, "simpleObject.txt");
65
        OutputStream os = fo.getOutputStream();
66
        os.write("${encoding}".getBytes());
67
        os.close();
68
        fo.setAttribute ("template", Boolean.TRUE);
69
        fo.setAttribute("javax.script.ScriptEngine", "freemarker");
70
        
71
        DataObject obj = DataObject.find(fo);
72
        DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target"));
73
        
74
        Map<String,String> parameters = Collections.emptyMap();
75
        DataObject inst = obj.createFromTemplate(folder, "complex", parameters);
76
        
77
        FileObject instFO = inst.getPrimaryFile();
78
        
79
        Charset targetEnc = FileEncodingQuery.getEncoding(instFO);
80
        assertNotNull("Template encoding is null", targetEnc);
81
        assertEquals("Encoding in template doesn't match", targetEnc.name(), readFile(instFO));
82
    }
83
    
84
    private static String readFile(FileObject fo) throws IOException {
85
        byte[] arr = new byte[(int)fo.getSize()];
86
        int len = fo.getInputStream().read(arr);
87
        assertEquals("Fully read", arr.length, len);
88
        return new String(arr);
89
    }
90
    
91
    public static final class SimpleLoader extends MultiFileLoader {
92
        public SimpleLoader() {
93
            super(SimpleObject.class.getName());
94
        }
95
        protected String displayName() {
96
            return "SimpleLoader";
97
        }
98
        protected FileObject findPrimaryFile(FileObject fo) {
99
            if (fo.hasExt("prima")) {
100
                return fo;
101
            }
102
            return null;
103
        }
104
        protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException {
105
            return new SimpleObject(this, primaryFile);
106
        }
107
        protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
108
            return new FE(obj, primaryFile);
109
        }
110
        protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) {
111
            return new FileEntry(obj, secondaryFile);
112
        }
113
    }
114
    
115
    private static final class FE extends FileEntry {
116
        public FE(MultiDataObject mo, FileObject fo) {
117
            super(mo, fo);
118
        }
119
120
        @Override
121
        public FileObject createFromTemplate(FileObject f, String name) throws IOException {
122
            fail("I do not want to be called");
123
            return null;
124
        }
125
    }
126
    
127
    public static final class SimpleObject extends MultiDataObject {
128
        public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException {
129
            super(fo, l);
130
        }
131
        
132
        public String getName() {
133
            return getPrimaryFile().getNameExt();
134
        }
135
    }
136
137
}

Return to bug 110910