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/loaders/manifest.mf (-1 / +1 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/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="25" month="7" 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="?"/>
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/loaders/test/unit/src/org/openide/loaders/CreateFromTemplateTest.java (+40 lines)
Lines 21-28 Link Here
21
21
22
22
23
import java.io.IOException;
23
import java.io.IOException;
24
import java.io.InputStream;
25
import java.io.OutputStream;
26
import java.nio.charset.Charset;
24
import java.util.Collections;
27
import java.util.Collections;
25
import java.util.Enumeration;
28
import java.util.Enumeration;
29
import org.netbeans.api.queries.FileEncodingQuery;
26
import org.netbeans.junit.MockServices;
30
import org.netbeans.junit.MockServices;
27
import org.netbeans.junit.NbTestCase;
31
import org.netbeans.junit.NbTestCase;
28
import org.openide.filesystems.FileObject;
32
import org.openide.filesystems.FileObject;
Lines 91-96 Link Here
91
        
95
        
92
        DataObject res = templ.createFromTemplate(f);
96
        DataObject res = templ.createFromTemplate(f);
93
        
97
        
98
        assertFalse("Not marked as template", res.isTemplate());
99
        assertEquals(SimpleLoader.class, res.getLoader().getClass());
100
    }
101
    
102
    public void testEncodingPropertyInTemplate() throws Exception {
103
        String folderName = "/Templates/";
104
        FileObject data = org.openide.filesystems.FileUtil.createData (
105
            Repository.getDefault ().getDefaultFileSystem ().getRoot (), 
106
            folderName + "/" + "Y.prima"
107
        );
108
        
109
        OutputStream os = data.getOutputStream();
110
        os.write("${encoding}".getBytes());
111
        os.flush();
112
        os.close();
113
        
114
        data.setAttribute ("template", Boolean.TRUE);
115
        data.setAttribute("javax.script.ScriptEngine", "freemarker");
116
        FileObject fo = data.getParent ();
117
        assertNotNull ("FileObject " + folderName + " found on DefaultFileSystem.", fo);
118
        DataFolder f = DataFolder.findFolder (fo);
119
        DataObject templ = DataObject.find(data);
120
        
121
        DataObject res = templ.createFromTemplate(f);
122
        
123
        Charset targetEnc = FileEncodingQuery.getEncoding(res.getPrimaryFile());
124
        assertNotNull("Template encoding is null", targetEnc);
125
        
126
        InputStream is = data.getInputStream();
127
        byte[] content = new byte[20];
128
        int read = is.read(content);
129
        is.close();
130
        
131
        String encoding = new String(content, 0, read);
132
        
133
        assertEquals("Encoding in template doesn't match", targetEnc, encoding);
94
        assertFalse("Not marked as template", res.isTemplate());
134
        assertFalse("Not marked as template", res.isTemplate());
95
        assertEquals(SimpleLoader.class, res.getLoader().getClass());
135
        assertEquals(SimpleLoader.class, res.getLoader().getClass());
96
    }
136
    }
(-)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 (+171 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.awt.Dialog;
24
import java.io.IOException;
25
import java.io.OutputStream;
26
import java.io.StringWriter;
27
import java.io.Writer;
28
import java.nio.charset.Charset;
29
import java.util.Collections;
30
import java.util.Collections;
31
import java.util.Enumeration;
32
import java.util.Map;
33
import javax.swing.text.Document;
34
import org.netbeans.api.queries.FileEncodingQuery;
35
import org.netbeans.junit.MockServices;
36
import org.netbeans.junit.NbTestCase;
37
import org.openide.DialogDescriptor;
38
import org.openide.DialogDisplayer;
39
import org.openide.NotifyDescriptor;
40
import org.openide.filesystems.FileObject;
41
import org.openide.filesystems.FileUtil;
42
import org.openide.loaders.DataFolder;
43
import org.openide.loaders.DataLoader;
44
import org.openide.loaders.DataLoaderPool;
45
import org.openide.loaders.DataObject;
46
import org.openide.loaders.DataObjectExistsException;
47
import org.openide.loaders.FileEntry;
48
import org.openide.loaders.MultiDataObject;
49
import org.openide.loaders.MultiFileLoader;
50
import org.openide.text.IndentEngine;
51
import org.openide.util.Enumerations;
52
53
/**
54
 *
55
 * @author Marek Fukala
56
 * @author Jaroslav Tulach
57
 */
58
public class ScriptingCreateFromTemplateTest extends NbTestCase {
59
    
60
    public ScriptingCreateFromTemplateTest(String testName) {
61
        super(testName);
62
    }
63
    
64
    protected boolean runInEQ() {
65
        return true;
66
    }
67
    
68
    @SuppressWarnings("deprecation")
69
    protected void setUp() throws Exception {
70
        MockServices.setServices(DD.class, Pool.class);
71
    }
72
73
    protected void tearDown() throws Exception {
74
        super.tearDown();
75
    }
76
77
    public void testCreateFromTemplateEndcodingProperty() throws Exception {
78
        FileObject root = FileUtil.createMemoryFileSystem().getRoot();
79
        FileObject fo = FileUtil.createData(root, "simpleObject.txt");
80
        OutputStream os = fo.getOutputStream();
81
        os.write("${encoding}".getBytes());
82
        os.close();
83
        fo.setAttribute ("template", Boolean.TRUE);
84
        fo.setAttribute("javax.script.ScriptEngine", "freemarker");
85
        
86
        DataObject obj = DataObject.find(fo);
87
        DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target"));
88
        
89
        Map<String,String> parameters = Collections.emptyMap();
90
        DataObject inst = obj.createFromTemplate(folder, "complex", parameters);
91
        
92
        FileObject instFO = inst.getPrimaryFile();
93
        
94
        Charset targetEnc = FileEncodingQuery.getEncoding(instFO);
95
        assertNotNull("Template encoding is null", targetEnc);
96
        assertEquals("Encoding in template doesn't match", targetEnc.name(), readFile(instFO));
97
    }
98
    
99
    private static String readFile(FileObject fo) throws IOException {
100
        byte[] arr = new byte[(int)fo.getSize()];
101
        int len = fo.getInputStream().read(arr);
102
        assertEquals("Fully read", arr.length, len);
103
        return new String(arr);
104
    }
105
    
106
    public static final class DD extends DialogDisplayer {
107
        public Object notify(NotifyDescriptor descriptor) {
108
            throw new UnsupportedOperationException("Not supported yet.");
109
        }
110
111
        public Dialog createDialog(final DialogDescriptor descriptor) {
112
            throw new UnsupportedOperationException("Not supported yet.");
113
        }
114
    }
115
    
116
    public static final class Pool extends DataLoaderPool {
117
        protected Enumeration<DataLoader> loaders() {
118
            return Enumerations.<DataLoader>singleton(SimpleLoader.getLoader(SimpleLoader.class));
119
        }
120
    }
121
    
122
    public static final class SimpleLoader extends MultiFileLoader {
123
        public SimpleLoader() {
124
            super(SimpleObject.class.getName());
125
        }
126
        protected String displayName() {
127
            return "SimpleLoader";
128
        }
129
        protected FileObject findPrimaryFile(FileObject fo) {
130
            if (fo.hasExt("prima")) {
131
                return fo;
132
            }
133
            return null;
134
        }
135
        protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException {
136
            return new SimpleObject(this, primaryFile);
137
        }
138
        protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
139
            return new FE(obj, primaryFile);
140
        }
141
        protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) {
142
            return new FileEntry(obj, secondaryFile);
143
        }
144
    }
145
    
146
    private static final class FE extends FileEntry {
147
        public FE(MultiDataObject mo, FileObject fo) {
148
            super(mo, fo);
149
        }
150
151
        @Override
152
        public FileObject createFromTemplate(FileObject f, String name) throws IOException {
153
            fail("I do not want to be called");
154
            return null;
155
        }
156
157
        
158
        
159
    }
160
    
161
    public static final class SimpleObject extends MultiDataObject {
162
        public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException {
163
            super(fo, l);
164
        }
165
        
166
        public String getName() {
167
            return getPrimaryFile().getNameExt();
168
        }
169
    }
170
171
}

Return to bug 110910