Index: openide/dialogs/apichanges.xml =================================================================== RCS file: /shared/data/ccvs/repository/openide/dialogs/apichanges.xml,v retrieving revision 1.8 retrieving revision 1.8.12.1 diff -u -r1.8 -r1.8.12.1 --- openide/dialogs/apichanges.xml 6 Aug 2006 15:58:36 -0000 1.8 +++ openide/dialogs/apichanges.xml 16 Jan 2007 18:54:03 -0000 1.8.12.1 @@ -23,6 +23,19 @@ Dialogs API + + + New method getProperties added to WizardDescriptor + + + + + + New method to get list of all properties of the WizardDescriptor + added to the class. + + + Interface ProgressInstantiatingIterator added Index: openide/dialogs/manifest.mf =================================================================== RCS file: /shared/data/ccvs/repository/openide/dialogs/manifest.mf,v retrieving revision 1.9 retrieving revision 1.9.12.1 diff -u -r1.9 -r1.9.12.1 --- openide/dialogs/manifest.mf 6 Aug 2006 15:58:36 -0000 1.9 +++ openide/dialogs/manifest.mf 16 Jan 2007 18:54:03 -0000 1.9.12.1 @@ -1,5 +1,5 @@ Manifest-Version: 1.0 OpenIDE-Module: org.openide.dialogs -OpenIDE-Module-Specification-Version: 7.1 +OpenIDE-Module-Specification-Version: 7.2 OpenIDE-Module-Localizing-Bundle: org/openide/Bundle.properties Index: openide/dialogs/nbproject/project.properties =================================================================== RCS file: /shared/data/ccvs/repository/openide/dialogs/nbproject/project.properties,v retrieving revision 1.7 retrieving revision 1.7.10.1 diff -u -r1.7 -r1.7.10.1 --- openide/dialogs/nbproject/project.properties 16 Aug 2006 06:28:31 -0000 1.7 +++ openide/dialogs/nbproject/project.properties 16 Jan 2007 18:54:04 -0000 1.7.10.1 @@ -16,6 +16,8 @@ # Microsystems, Inc. All Rights Reserved. is.autoload=true +javac.compilerargs=-Xlint:unchecked +javac.source=1.5 #javadoc.main.page=org/openide/doc-files/api.html javadoc.arch=${basedir}/../arch/arch-openide-dialogs.xml javadoc.apichanges=${basedir}/apichanges.xml Index: openide/dialogs/src/org/openide/WizardDescriptor.java =================================================================== RCS file: /shared/data/ccvs/repository/openide/dialogs/src/org/openide/WizardDescriptor.java,v retrieving revision 1.40 retrieving revision 1.40.4.1 diff -u -r1.40 -r1.40.4.1 --- openide/dialogs/src/org/openide/WizardDescriptor.java 11 Jan 2007 14:35:09 -0000 1.40 +++ openide/dialogs/src/org/openide/WizardDescriptor.java 16 Jan 2007 18:54:05 -0000 1.40.4.1 @@ -279,7 +279,7 @@ /** hashtable with additional settings that is usually used * by Panels to store their data */ - private Map properties; + private Map properties; ResourceBundle bundle = NbBundle.getBundle(WizardDescriptor.class); /** Request processor that is used for asynchronous jobs (background validation, @@ -348,22 +348,18 @@ baseListener = new Listener(); - try { - weakNextButtonListener = (ActionListener) WeakListeners.create( - Class.forName("java.awt.event.ActionListener"), baseListener, nextButton - ); // NOI18N - weakPreviousButtonListener = (ActionListener) WeakListeners.create( - Class.forName("java.awt.event.ActionListener"), baseListener, previousButton - ); // NOI18N - weakFinishButtonListener = (ActionListener) WeakListeners.create( - Class.forName("java.awt.event.ActionListener"), baseListener, finishButton - ); // NOI18N - weakCancelButtonListener = (ActionListener) WeakListeners.create( - Class.forName("java.awt.event.ActionListener"), baseListener, cancelButton - ); // NOI18N - } catch (ClassNotFoundException e) { - // cannot happen, java.awt.event.ActionListener listener can always be found - } + weakNextButtonListener = WeakListeners.create( + ActionListener.class, baseListener, nextButton + ); // NOI18N + weakPreviousButtonListener = WeakListeners.create( + ActionListener.class, baseListener, previousButton + ); // NOI18N + weakFinishButtonListener = WeakListeners.create( + ActionListener.class, baseListener, finishButton + ); // NOI18N + weakCancelButtonListener = WeakListeners.create( + ActionListener.class, baseListener, cancelButton + ); // NOI18N nextButton.addActionListener(weakNextButtonListener); previousButton.addActionListener(weakPreviousButtonListener); @@ -576,7 +572,7 @@ synchronized (this) { if (properties == null) { - properties = new HashMap(7); + properties = new HashMap(7); } oldValue = properties.get(name); @@ -616,6 +612,14 @@ public synchronized Object getProperty(String name) { return (properties == null) ? null : properties.get(name); } + + /** Read only map with stored properties. + * @return read only map of properties stored using {@link #putProperty} method + * @since 7.2 + */ + public synchronized Map getProperties() { + return properties == null ? Collections.emptyMap() : new HashMap(properties); + } public void setHelpCtx(final HelpCtx helpCtx) { if ((wizardPanel != null) && (helpCtx != null)) { @@ -2546,6 +2550,7 @@ /** Overriden to delegate call to user component. */ + @Deprecated public boolean requestDefaultFocus() { if (rightComponent instanceof JComponent) { return ((JComponent) rightComponent).requestDefaultFocus(); Index: openide/dialogs/test/unit/src/org/openide/PropertiesTest.java =================================================================== RCS file: openide/dialogs/test/unit/src/org/openide/PropertiesTest.java diff -N openide/dialogs/test/unit/src/org/openide/PropertiesTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/dialogs/test/unit/src/org/openide/PropertiesTest.java 16 Jan 2007 18:54:06 -0000 1.1.2.1 @@ -0,0 +1,40 @@ +/* + * 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 Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package org.openide; + +import junit.framework.TestCase; + +/** + * + * @author Jaroslav Tulach + */ +public class PropertiesTest extends TestCase { + + public PropertiesTest(String testName) { + super(testName); + } + + public void testGetProperties() throws Exception { + WizardDescriptor d = new WizardDescriptor(new WizardDescriptor.Panel[0]); + assertTrue(d.getProperties().isEmpty()); + d.putProperty("Kuk", "Ahoj"); + assertEquals("Ahoj", d.getProperties().get("Kuk")); + } +} Index: openide/loaders/nbproject/project.xml =================================================================== RCS file: /shared/data/ccvs/repository/openide/loaders/nbproject/project.xml,v retrieving revision 1.24 retrieving revision 1.24.6.1 diff -u -r1.24 -r1.24.6.1 --- openide/loaders/nbproject/project.xml 9 Nov 2006 16:53:53 -0000 1.24 +++ openide/loaders/nbproject/project.xml 16 Jan 2007 18:58:48 -0000 1.24.6.1 @@ -53,7 +53,7 @@ - 6.2 + 7.2 Index: openide/loaders/src/org/openide/loaders/CreateFromTemplateAttributesProvider.java =================================================================== RCS file: openide/loaders/src/org/openide/loaders/CreateFromTemplateAttributesProvider.java diff -N openide/loaders/src/org/openide/loaders/CreateFromTemplateAttributesProvider.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/loaders/src/org/openide/loaders/CreateFromTemplateAttributesProvider.java 16 Jan 2007 18:09:54 -0000 1.1.2.1 @@ -0,0 +1,31 @@ +/* + * 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.openide.loaders; + +import java.util.Map; + +/** + * + * @author Jaroslav Tulach + */ +public abstract class CreateFromTemplateAttributesProvider { + protected abstract Map attributesFor(DataObject template, DataFolder target, String name); +} Index: openide/loaders/src/org/openide/loaders/CreateFromTemplateHandler.java =================================================================== RCS file: openide/loaders/src/org/openide/loaders/CreateFromTemplateHandler.java diff -N openide/loaders/src/org/openide/loaders/CreateFromTemplateHandler.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/loaders/src/org/openide/loaders/CreateFromTemplateHandler.java 16 Jan 2007 18:09:53 -0000 1.1.2.1 @@ -0,0 +1,42 @@ +/* + * 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.openide.loaders; + +import java.io.IOException; +import java.util.Map; +import org.openide.filesystems.FileObject; + +/** + * + * @author Jaroslav Tulach + */ +public abstract class CreateFromTemplateHandler { + + protected abstract boolean accept(FileObject orig); + + protected abstract FileObject createFromTemplate( + FileObject orig, + FileObject f, + String name, + Map parameters + ) throws IOException; + +} Index: openide/loaders/src/org/openide/loaders/DataObject.java =================================================================== RCS file: /shared/data/ccvs/repository/openide/loaders/src/org/openide/loaders/DataObject.java,v retrieving revision 1.33 retrieving revision 1.33.2.2 diff -u -r1.33 -r1.33.2.2 --- openide/loaders/src/org/openide/loaders/DataObject.java 16 Jan 2007 14:09:50 -0000 1.33 +++ openide/loaders/src/org/openide/loaders/DataObject.java 30 Jan 2007 16:36:19 -0000 1.33.2.2 @@ -22,6 +22,7 @@ import java.beans.*; import java.io.*; +import java.text.DateFormat; import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; @@ -704,19 +705,31 @@ public final DataObject createFromTemplate ( final DataFolder f, final String name ) throws IOException { - final DataObject[] result = new DataObject[1]; - - invokeAtomicAction (f.getPrimaryFile (), new FileSystem.AtomicAction () { - public void run () throws IOException { - result[0] = handleCreateFromTemplate (f, name); - } - }, null); - + return createFromTemplate(f, name, Collections.emptyMap()); + } + + /** More generic way how to instantiate a {@link DataObject}. One can + * not only specify its name, but also pass a map of parameters that + * can influence the copying of the stream. + * + * @param f folder to create object in + * @param name name of object that should be created, or null if the + * name should be same as that of the template (or otherwise mechanically generated) + * @param parameters map of named objects that are going to be used when + * creating the new object + * @return the new data object + * @exception IOException if an error occured + * @since JST-PENDING + */ + public final DataObject createFromTemplate( + final DataFolder f, final String name, final Map parameters + ) throws IOException { + CreateAction create = new CreateAction(this, f, name, parameters); + invokeAtomicAction (f.getPrimaryFile (), create, null); fireOperationEvent ( - new OperationEvent.Copy (result[0], this), OperationEvent.TEMPL + new OperationEvent.Copy (create.result, this), OperationEvent.TEMPL ); - - return result[0]; + return create.result; } /** Create a new data object from template (implemented in subclasses). @@ -1137,4 +1150,68 @@ } } } + static final class CreateAction implements FileSystem.AtomicAction { + public DataObject result; + private String name; + private DataFolder f; + private DataObject orig; + private Map param; + + private static ThreadLocal CURRENT = new ThreadLocal(); + + public CreateAction(DataObject orig, DataFolder f, String name, Map param) { + this.orig = orig; + this.f = f; + this.name = name; + this.param = param; + } + + public void run () throws IOException { + CreateAction prev = CURRENT.get(); + try { + CURRENT.set(this); + result = orig.handleCreateFromTemplate(f, name); + } finally { + CURRENT.set(prev); + } + } + + public static Map findParameters(String name) { + CreateAction c = CURRENT.get(); + if (c == null) { + return Collections.emptyMap(); + } + HashMap all = new HashMap(); + for (CreateFromTemplateAttributesProvider provider : Lookup.getDefault().lookupAll(CreateFromTemplateAttributesProvider.class)) { + Map map = provider.attributesFor(c.orig, c.f, c.name); + if (map != null) { + for (Map.Entry e : map.entrySet()) { + all.put(e.getKey(), e.getValue()); + } + } + } + if (c.param != null) { + for (Map.Entry e : c.param.entrySet()) { + all.put(e.getKey(), e.getValue()); + } + } + + if (!all.containsKey("name") && name != null) { // NOI18N + all.put("name", name); // NOI18N + } + if (!all.containsKey("user")) { // NOI18N + all.put("user", System.getProperty("user.name")); // NOI18N + } + Date d = new Date(); + if (!all.containsKey("date")) { // NOI18N + all.put("date", DateFormat.getDateInstance().format(d)); // NOI18N + } + if (!all.containsKey("time")) { // NOI18N + all.put("time", DateFormat.getTimeInstance().format(d)); // NOI18N + } + + return Collections.unmodifiableMap(all); + } + + } // end of CreateAction } Index: openide/loaders/src/org/openide/loaders/FileEntry.java =================================================================== RCS file: /shared/data/ccvs/repository/openide/loaders/src/org/openide/loaders/FileEntry.java,v retrieving revision 1.3 retrieving revision 1.3.14.3 diff -u -r1.3 -r1.3.14.3 --- openide/loaders/src/org/openide/loaders/FileEntry.java 1 Jul 2006 09:08:21 -0000 1.3 +++ openide/loaders/src/org/openide/loaders/FileEntry.java 30 Jan 2007 16:36:19 -0000 1.3.14.3 @@ -20,8 +20,8 @@ package org.openide.loaders; import java.io.*; - import org.openide.filesystems.*; +import org.openide.util.Lookup; import org.openide.util.NbBundle; /** Entry that works with plain files. Copies, moves, @@ -122,7 +122,24 @@ getFile ().getName (), getFile ().getExt () ); } - FileObject fo = getFile().copy (f, name, getFile().getExt ()); + + + FileObject fo = null; + boolean done = false; + for (CreateFromTemplateHandler h : Lookup.getDefault().lookupAll(CreateFromTemplateHandler.class)) { + if (h.accept(getFile())) { + done = true; + fo = h.createFromTemplate(getFile(), f, name, DataObject.CreateAction.findParameters(name)); + assert fo != null; + break; + } + } + + if (!done) { + fo = getFile().copy (f, name, getFile().getExt ()); + } + + // unmark template state DataObject.setTemplate (fo, false); @@ -161,7 +178,26 @@ getFile ().getName (), ext ); } - FileObject fo = f.createData (name, ext); + + + FileObject fo = null; + boolean done = false; + for (CreateFromTemplateHandler h : Lookup.getDefault().lookupAll(CreateFromTemplateHandler.class)) { + if (h.accept(getFile())) { + done = true; + fo = h.createFromTemplate(getFile(), f, name, DataObject.CreateAction.findParameters(name)); + assert fo != null; + break; + } + } + + if (done) { + // unmark template state + DataObject.setTemplate (fo, false); + return fo; + } + + fo = f.createData (name, ext); java.text.Format frm = createFormat (f, name, ext); Index: openide/loaders/src/org/openide/loaders/MultiDataObject.java =================================================================== RCS file: /shared/data/ccvs/repository/openide/loaders/src/org/openide/loaders/MultiDataObject.java,v retrieving revision 1.25 retrieving revision 1.25.6.2 diff -u -r1.25 -r1.25.6.2 --- openide/loaders/src/org/openide/loaders/MultiDataObject.java 6 Nov 2006 08:23:47 -0000 1.25 +++ openide/loaders/src/org/openide/loaders/MultiDataObject.java 30 Jan 2007 16:36:19 -0000 1.25.6.2 @@ -659,18 +659,44 @@ protected DataObject handleCreateFromTemplate ( DataFolder df, String name ) throws IOException { - FileObject fo; - - if (name == null) { name = FileUtil.findFreeFileName( df.getPrimaryFile (), getPrimaryFile ().getName (), getPrimaryFile ().getExt () ); } - fo = getPrimaryEntry().createFromTemplate (df.getPrimaryFile (), name); + FileObject fo = null; + Map params = null; + for (CreateFromTemplateHandler h : Lookup.getDefault().lookupAll(CreateFromTemplateHandler.class)) { + FileObject current = getPrimaryEntry().getFile(); + if (h.accept(current)) { + if (params == null) { + params = DataObject.CreateAction.findParameters(name); + } + fo = h.createFromTemplate(current, df.getPrimaryFile(), name, params); + assert fo != null; + break; + } + } + if (params == null) { + // do the regular creation + fo = getPrimaryEntry().createFromTemplate (df.getPrimaryFile (), name); + } + + Iterator it = secondaryEntries().iterator(); - while (it.hasNext ()) { + NEXT_ENTRY: while (it.hasNext ()) { + for (CreateFromTemplateHandler h : Lookup.getDefault().lookupAll(CreateFromTemplateHandler.class)) { + FileObject current = getPrimaryEntry().getFile(); + if (h.accept(current)) { + if (params == null) { + params = DataObject.CreateAction.findParameters(name); + } + fo = h.createFromTemplate(current, df.getPrimaryFile(), name, params); + assert fo != null; + continue NEXT_ENTRY; + } + } ((Entry)it.next()).createFromTemplate (df.getPrimaryFile (), name); } Index: openide/loaders/src/org/openide/loaders/TemplateWizard.java =================================================================== RCS file: /shared/data/ccvs/repository/openide/loaders/src/org/openide/loaders/TemplateWizard.java,v retrieving revision 1.25 retrieving revision 1.25.4.1 diff -u -r1.25 -r1.25.4.1 --- openide/loaders/src/org/openide/loaders/TemplateWizard.java 23 Dec 2006 16:02:46 -0000 1.25 +++ openide/loaders/src/org/openide/loaders/TemplateWizard.java 16 Jan 2007 18:58:48 -0000 1.25.4.1 @@ -858,10 +858,7 @@ String n = wiz.getTargetName (); DataFolder folder = wiz.getTargetFolder (); DataObject template = wiz.getTemplate (); - DataObject obj = n == null ? - template.createFromTemplate (folder) - : - template.createFromTemplate (folder, n); + DataObject obj = template.createFromTemplate (folder, n, wiz.getProperties()); // run default action (hopefully should be here) final Node node = obj.getNodeDelegate (); Index: openide/loaders/test/unit/src/org/openide/loaders/CreateFromTemplateHandlerTest.java =================================================================== RCS file: openide/loaders/test/unit/src/org/openide/loaders/CreateFromTemplateHandlerTest.java diff -N openide/loaders/test/unit/src/org/openide/loaders/CreateFromTemplateHandlerTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/loaders/test/unit/src/org/openide/loaders/CreateFromTemplateHandlerTest.java 18 Jan 2007 15:31:15 -0000 1.1.2.4 @@ -0,0 +1,264 @@ +/* + * 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.openide.loaders; + +import java.awt.Dialog; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Map; +import java.util.Set; +import javax.swing.JButton; +import javax.swing.JDialog; +import org.netbeans.junit.MockServices; +import org.netbeans.junit.NbTestCase; +import org.openide.DialogDescriptor; +import org.openide.DialogDisplayer; +import org.openide.NotifyDescriptor; +import org.openide.WizardDescriptor; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.util.Enumerations; + +/** + * + * @author Jaroslav Tulach + */ +public class CreateFromTemplateHandlerTest extends NbTestCase { + + public CreateFromTemplateHandlerTest(String testName) { + super(testName); + } + + protected boolean runInEQ() { + return true; + } + + protected void setUp() throws Exception { + Hand.acceptObject = null; + Hand.fileObject = null; + Hand.origObject = null; + Hand.name = null; + Hand.parameters = null; + + MockServices.setServices(Hand.class, Attr.class, DD.class, Pool.class); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testCreateFromTemplate() throws Exception { + FileObject root = FileUtil.createMemoryFileSystem().getRoot(); + FileObject fo = FileUtil.createData(root, "simpleObject.txt"); + + DataObject obj = DataObject.find(fo); + + DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); + + Map parameters = Collections.singletonMap("type", "empty"); + DataObject n = obj.createFromTemplate(folder, "complex", parameters); + + assertEquals("Created in right place", folder, n.getFolder()); + assertEquals("Created with right name", "complex.txt", n.getName()); + + assertEquals("The right source", fo, Hand.origObject); + assertEquals("The right source in query", fo, Hand.acceptObject); + assertEquals("The right destiny folder", folder.getPrimaryFile(), Hand.fileObject); + assertEquals("The right name", "complex", Hand.name); + assertEquals("Two elements: " + Hand.parameters, 2, Hand.parameters.size()); + assertEquals("empty", Hand.parameters.get("type")); + assertEquals("complex", Hand.parameters.get("name")); + try { + Hand.parameters.put("kuk", "buk"); + } catch (UnsupportedOperationException ex) { + // ok + return; + } + fail("Modifications shall be unsupported"); + } + + public void testTemplateWizardCopiesItsPropertiesToMap() throws Exception { + doTemplateWizardCopiesItsPropertiesToMap("simpleObject.txt"); + } + + public void testTemplateWizardCopiesItsPropertiesToMapForOverridenEntry() throws Exception { + DataObject obj = doTemplateWizardCopiesItsPropertiesToMap("simpleObject.prima"); + assertEquals("The right loader", SimpleLoader.class, obj.getLoader().getClass()); + } + + private DataObject doTemplateWizardCopiesItsPropertiesToMap(String fileName) throws Exception { + FileObject root = FileUtil.createMemoryFileSystem().getRoot(); + FileObject fo = FileUtil.createData(root, fileName); + + DataObject obj = DataObject.find(fo); + + DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); + + TemplateWizard t = new TemplateWizard(); + t.putProperty("type", "empty"); + Set created = t.instantiate(obj, folder); + assertNotNull(created); + assertEquals("One is created: " + created, 1, created.size()); + + DataObject n = created.iterator().next(); + + assertEquals("Created in right place", folder, n.getFolder()); + assertEquals("Created with right name", fileName, n.getName()); + + assertEquals("The right source", fo, Hand.origObject); + assertEquals("The right source in query", fo, Hand.acceptObject); + assertEquals("The right destiny folder", folder.getPrimaryFile(), Hand.fileObject); + assertEquals("The right name", "simpleObject", Hand.name); + assertTrue("At least two elements: " + Hand.parameters, 2 <= Hand.parameters.size()); + assertEquals("empty", Hand.parameters.get("type")); + assertEquals("There was no name, just default", null, Hand.parameters.get("name")); + assertTrue("the argument is there", Hand.parameters.containsKey("name")); + try { + Hand.parameters.put("kuk", "buk"); + } catch (UnsupportedOperationException ex) { + // ok + return obj; + } + fail("Modifications shall be unsupported"); + throw new NullPointerException(); + } + + public static final class Hand extends CreateFromTemplateHandler { + public static FileObject fileObject, origObject, acceptObject; + public static String name; + public static Map parameters; + + protected boolean accept(FileObject fo) { + assertNull(acceptObject); + acceptObject = fo; + return true; + } + + protected FileObject createFromTemplate( + FileObject orig, FileObject f, String n, + Map p + ) throws IOException { + assertNull("Not yet filled", fileObject); + + origObject = orig; + fileObject = f; + name = n; + parameters = p; + + return FileUtil.copyFile(orig, fileObject, name); + } + } + + public static final class Attr extends CreateFromTemplateAttributesProvider { + + + protected Map attributesFor( + DataObject template, + DataFolder target, + String name + ) { + return Collections.singletonMap("name", name); + } + } + + public static final class DD extends DialogDisplayer { + public Object notify(NotifyDescriptor descriptor) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Dialog createDialog(final DialogDescriptor descriptor) { + return new JDialog() { + @Deprecated + public void show() { + for (Object object : descriptor.getOptions()) { + if (object instanceof JButton) { + JButton b = (JButton)object; + if (b.getText().equals("Finish")) { + descriptor.setValue(WizardDescriptor.FINISH_OPTION); + b.doClick(); + return; + } + } + } + fail("Cannot find Finish button: " + Arrays.asList(descriptor.getOptions())); + } + }; + } + } + + public static final class Pool extends DataLoaderPool { + protected Enumeration loaders() { + return Enumerations.singleton(SimpleLoader.getLoader(SimpleLoader.class)); + } + } + + 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(); + } + } + +} Index: openide/templates/.cvsignore =================================================================== RCS file: openide/templates/.cvsignore diff -N openide/templates/.cvsignore --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/templates/.cvsignore 22 Jan 2007 18:29:00 -0000 1.1.2.1 @@ -0,0 +1 @@ +build Index: openide/templates/build.xml =================================================================== RCS file: openide/templates/build.xml diff -N openide/templates/build.xml --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/templates/build.xml 22 Jan 2007 18:29:01 -0000 1.1.2.1 @@ -0,0 +1,5 @@ + + + Builds, tests, and runs the project org.netbeans.modules.templates + + Index: openide/templates/manifest.mf =================================================================== RCS file: openide/templates/manifest.mf diff -N openide/templates/manifest.mf --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/templates/manifest.mf 22 Jan 2007 18:29:00 -0000 1.1.2.1 @@ -0,0 +1,6 @@ +Manifest-Version: 1.0 +OpenIDE-Module: org.netbeans.modules.templates +OpenIDE-Module-Layer: org/netbeans/modules/templates/layer.xml +OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/templates/Bundle.properties +OpenIDE-Module-Specification-Version: 1.0 + Index: openide/templates/nbproject/.cvsignore =================================================================== RCS file: openide/templates/nbproject/.cvsignore diff -N openide/templates/nbproject/.cvsignore --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/templates/nbproject/.cvsignore 22 Jan 2007 18:29:03 -0000 1.1.2.1 @@ -0,0 +1 @@ +private Index: openide/templates/nbproject/project.properties =================================================================== RCS file: openide/templates/nbproject/project.properties diff -N openide/templates/nbproject/project.properties --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/templates/nbproject/project.properties 22 Jan 2007 18:29:04 -0000 1.1.2.1 @@ -0,0 +1,3 @@ +is.autoload=true +javac.compilerargs=-Xlint:unchecked +javac.source=1.5 Index: openide/templates/nbproject/project.xml =================================================================== RCS file: openide/templates/nbproject/project.xml diff -N openide/templates/nbproject/project.xml --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/templates/nbproject/project.xml 22 Jan 2007 18:29:03 -0000 1.1.2.1 @@ -0,0 +1,70 @@ + + + org.netbeans.modules.apisupport.project + + + org.netbeans.modules.templates + + + org.netbeans.libs.jsr223 + + + + 1 + 1.6 + + + + org.openide.dialogs + + + + 7.2 + + + + org.openide.filesystems + + + + 7.0 + + + + org.openide.loaders + + + + 6.0 + + + + org.openide.nodes + + + + 7.0 + + + + org.openide.util + + + + 7.7 + + + + + + unit + + org.netbeans.libs.freemarker + + + + + + + + Index: openide/templates/src/META-INF/services/org.openide.loaders.CreateFromTemplateHandler =================================================================== RCS file: openide/templates/src/META-INF/services/org.openide.loaders.CreateFromTemplateHandler diff -N openide/templates/src/META-INF/services/org.openide.loaders.CreateFromTemplateHandler --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/templates/src/META-INF/services/org.openide.loaders.CreateFromTemplateHandler 22 Jan 2007 18:29:02 -0000 1.1.2.1 @@ -0,0 +1 @@ +org.netbeans.modules.templates.ScriptingCreateFromTemplateHandler Index: openide/templates/src/org/netbeans/modules/templates/Bundle.properties =================================================================== RCS file: openide/templates/src/org/netbeans/modules/templates/Bundle.properties diff -N openide/templates/src/org/netbeans/modules/templates/Bundle.properties --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/templates/src/org/netbeans/modules/templates/Bundle.properties 22 Jan 2007 18:28:58 -0000 1.1.2.1 @@ -0,0 +1,6 @@ +OpenIDE-Module-Display-Category=Infrastructure +OpenIDE-Module-Long-Description=\ + This module binds the Datasystems API with Scripting API. \ + As a result it is possible to use any scripting and templating language to generate templates. +OpenIDE-Module-Name=Advanced Templating +OpenIDE-Module-Short-Description=Advanced Templating not only for Datasystems Index: openide/templates/src/org/netbeans/modules/templates/ScriptingCreateFromTemplateHandler.java =================================================================== RCS file: openide/templates/src/org/netbeans/modules/templates/ScriptingCreateFromTemplateHandler.java diff -N openide/templates/src/org/netbeans/modules/templates/ScriptingCreateFromTemplateHandler.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/templates/src/org/netbeans/modules/templates/ScriptingCreateFromTemplateHandler.java 30 Jan 2007 16:36:20 -0000 1.1.2.2 @@ -0,0 +1,104 @@ +/* + * 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.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.script.Bindings; +import javax.script.ScriptContext; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.loaders.CreateFromTemplateHandler; + + +/** Processeses templates that have associated attribute +* with name of the scripting engine. +* +* @author Jaroslav Tulach +*/ +public class ScriptingCreateFromTemplateHandler extends CreateFromTemplateHandler { + private static ScriptEngineManager manager; + private static final Logger LOG = Logger.getLogger(ScriptingCreateFromTemplateHandler.class.getName()); + + protected boolean accept(FileObject orig) { + return engine(orig) != null; + } + + protected FileObject createFromTemplate(FileObject template, FileObject f, + String name, + Map values) throws IOException { + + ScriptEngineManager mgr = new ScriptEngineManager(); + ScriptEngine eng = mgr.getEngineByName("freemarker"); + 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()); + + OutputStreamWriter w = null; + Reader is = null; + try { + w = new OutputStreamWriter(output.getOutputStream()); + eng.getContext().setWriter(w); + //eng.getContext().setBindings(bind, ScriptContext.ENGINE_SCOPE); + eng.getContext().setAttribute(FileObject.class.getName(), template, ScriptContext.ENGINE_SCOPE); + eng.getContext().setAttribute(ScriptEngine.FILENAME, template.getNameExt(), ScriptContext.ENGINE_SCOPE); + is = new InputStreamReader(template.getInputStream()); + eng.eval(is); + }catch (ScriptException ex) { + IOException io = new IOException(ex.getMessage()); + io.initCause(ex); + throw io; + } finally { + if (w != null) w.close(); + if (is != null) is.close(); + } + return output; + } + + private static ScriptEngine engine(FileObject fo) { + Object obj = fo.getAttribute("javax.script.ScriptEngine"); // NOI18N + if (obj instanceof ScriptEngine) { + return (ScriptEngine)obj; + } + if (obj instanceof String) { + synchronized (ScriptingCreateFromTemplateHandler.class) { + if (manager == null) { + manager = new ScriptEngineManager(); + } + } + return manager.getEngineByName((String)obj); + } + return null; + } +} Index: openide/templates/src/org/netbeans/modules/templates/layer.xml =================================================================== RCS file: openide/templates/src/org/netbeans/modules/templates/layer.xml diff -N openide/templates/src/org/netbeans/modules/templates/layer.xml --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/templates/src/org/netbeans/modules/templates/layer.xml 22 Jan 2007 18:28:57 -0000 1.1.2.1 @@ -0,0 +1,4 @@ + + + + Index: openide/templates/test/unit/src/org/netbeans/modules/templates/SCFTHandlerTest.java =================================================================== RCS file: openide/templates/test/unit/src/org/netbeans/modules/templates/SCFTHandlerTest.java diff -N openide/templates/test/unit/src/org/netbeans/modules/templates/SCFTHandlerTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/templates/test/unit/src/org/netbeans/modules/templates/SCFTHandlerTest.java 30 Jan 2007 16:36:19 -0000 1.1.2.2 @@ -0,0 +1,217 @@ +/* + * 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.awt.Dialog; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Map; +import javax.swing.JDialog; +import org.netbeans.junit.MockServices; +import org.netbeans.junit.NbTestCase; +import org.openide.DialogDescriptor; +import org.openide.DialogDisplayer; +import org.openide.NotifyDescriptor; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.loaders.DataFolder; +import org.openide.loaders.DataLoader; +import org.openide.loaders.DataLoaderPool; +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; +import org.openide.util.Enumerations; + +/** + * + * @author Jaroslav Tulach + */ +public class SCFTHandlerTest extends NbTestCase { + + public SCFTHandlerTest(String testName) { + super(testName); + } + + protected boolean runInEQ() { + return true; + } + + protected void setUp() throws Exception { + MockServices.setServices(DD.class, Pool.class); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testCreateFromTemplateUsingFreemarker() throws Exception { + FileObject root = FileUtil.createMemoryFileSystem().getRoot(); + FileObject fo = FileUtil.createData(root, "simpleObject.txt"); + OutputStream os = fo.getOutputStream(); + String txt = "

${title}

"; + os.write(txt.getBytes()); + os.close(); + fo.setAttribute("javax.script.ScriptEngine", "freemarker"); + + + DataObject obj = DataObject.find(fo); + + DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); + + Map parameters = Collections.singletonMap("title", "Nazdar"); + DataObject n = obj.createFromTemplate(folder, "complex", parameters); + + assertEquals("Created in right place", folder, n.getFolder()); + assertEquals("Created with right name", "complex.txt", n.getName()); + + String exp = "

Nazdar

"; + assertEquals(exp, readFile(n.getPrimaryFile())); + + } + + + public void testBasePropertiesAlwaysPresent() throws Exception { + FileObject root = FileUtil.createMemoryFileSystem().getRoot(); + FileObject fo = FileUtil.createData(root, "simpleObject.txt"); + OutputStream os = fo.getOutputStream(); + String txt = "

${name}

" + + "

${date}

" + + "

${time}

" + + "

${user}

" + + ""; + os.write(txt.getBytes()); + os.close(); + fo.setAttribute("javax.script.ScriptEngine", "freemarker"); + + + DataObject obj = DataObject.find(fo); + + DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); + + Map parameters = Collections.singletonMap("title", "Nazdar"); + DataObject n = obj.createFromTemplate(folder, "complex", parameters); + + assertEquals("Created in right place", folder, n.getFolder()); + assertEquals("Created with right name", "complex.txt", n.getName()); + + String res = readFile(n.getPrimaryFile()); + + if (res.indexOf("date") >= 0) fail(res); + if (res.indexOf("time") >= 0) fail(res); + if (res.indexOf("user") >= 0) fail(res); + if (res.indexOf("name") >= 0) fail(res); + } + + 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 DD extends DialogDisplayer { + public Object notify(NotifyDescriptor descriptor) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Dialog createDialog(final DialogDescriptor descriptor) { + throw new UnsupportedOperationException("Not supported yet."); + /* + return new JDialog() { + @Deprecated + public void show() { + for (Object object : descriptor.getOptions()) { + if (object instanceof JButton) { + JButton b = (JButton)object; + if (b.getText().equals("Finish")) { + descriptor.setValue(WizardDescriptor.FINISH_OPTION); + b.doClick(); + return; + } + } + } + fail("Cannot find Finish button: " + Arrays.asList(descriptor.getOptions())); + } + }; + */ + } + } + + public static final class Pool extends DataLoaderPool { + protected Enumeration loaders() { + return Enumerations.singleton(SimpleLoader.getLoader(SimpleLoader.class)); + } + } + + 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(); + } + } + +} Index: libs/external/.cvsignore =================================================================== RCS file: /shared/data/ccvs/repository/libs/external/.cvsignore,v retrieving revision 1.24 retrieving revision 1.24.38.2 diff -u -r1.24 -r1.24.38.2 --- libs/external/.cvsignore 8 Sep 2005 18:10:45 -0000 1.24 +++ libs/external/.cvsignore 19 Jan 2007 09:45:38 -0000 1.24.38.2 @@ -1 +1,3 @@ +jsr223-api.jar dtds +freemarker-2.3.8.jar Index: libs/external/freemarker-2.3.8-license.txt =================================================================== RCS file: libs/external/freemarker-2.3.8-license.txt diff -N libs/external/freemarker-2.3.8-license.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libs/external/freemarker-2.3.8-license.txt 19 Jan 2007 09:45:38 -0000 1.1.2.1 @@ -0,0 +1,46 @@ +FreeMarker 1.x was released under the LGPL license. Later, by community +consensus, we have switched over to a BSD-style license. As of FreeMarker +2.2pre1, the original author, Benjamin Geer, has relinquished the copyright in +behalf of Visigoth Software Society. The current copyright holder is the +Visigoth Software Society. + +------------------------------------------------------------------------------ +Copyright (c) 2003 The Visigoth Software Society. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +2. The end-user documentation included with the redistribution, if any, must + include the following acknowlegement: + "This product includes software developed by the + Visigoth Software Society (http://www.visigoths.org/)." + Alternately, this acknowlegement may appear in the software itself, if and + wherever such third-party acknowlegements normally appear. + +3. Neither the name "FreeMarker", "Visigoth", nor any of the names of the + project contributors may be used to endorse or promote products derived + from this software without prior written permission. For written + permission, please contact visigoths@visigoths.org. + +4. Products derived from this software may not be called "FreeMarker" or + "Visigoth" nor may "FreeMarker" or "Visigoth" appear in their names + without prior written permission of the Visigoth Software Society. + +THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +VISIGOTH SOFTWARE SOCIETY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +------------------------------------------------------------------------------ + +This software consists of voluntary contributions made by many individuals on +behalf of the Visigoth Software Society. For more information on the Visigoth +Software Society, please see http://www.visigoths.org/ Index: libs/external/jsr223-api-license.txt =================================================================== RCS file: libs/external/jsr223-api-license.txt diff -N libs/external/jsr223-api-license.txt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libs/external/jsr223-api-license.txt 19 Jan 2007 07:58:45 -0000 1.1.2.1 @@ -0,0 +1,225 @@ +SUN MICROSYSTEMS, INC. ("SUN") IS WILLING TO LICENSE THIS SPECIFICATION +TO YOU ONLY UPON THE CONDITION THAT YOU ACCEPT ALL OF THE TERMS +CONTAINED IN THIS AGREEMENT. PLEASE READ THE TERMS AND CONDITIONS OF +THIS AGREEMENT CAREFULLY. BY DOWNLOADING THIS SPECIFICATION, YOU ACCEPT +THE TERMS AND CONDITIONS OF THE AGREEMENT. IF YOU ARE NOT WILLING TO BE +BOUND BY IT, SELECT THE "DECLINE" BUTTON AT THE BOTTOM OF THIS PAGE. + + +Specification: JSR-000223 Scripting for the Java(tm) Platform ("Specification") + + +Version: 1.0 + + +Status: Final Release + + +Release: 7 December 2006 + + +Copyright 2006 SUN MICROSYSTEMS, INC. + +4150 Network Circle, Santa Clara, California 95054, U.S.A + +All rights reserved. + + +LIMITED LICENSE GRANTS + + +1. _License for Evaluation Purposes_. Sun hereby grants you a +fully-paid, non-exclusive, non-transferable, worldwide, limited license +(without the right to sublicense), under Sun's applicable intellectual +property rights to view, download, use and reproduce the Specification +only for the purpose of internal evaluation. This includes (i) +developing applications intended to run on an implementation of the +Specification, provided that such applications do not themselves +implement any portion(s) of the Specification, and (ii) discussing the +Specification with any third party; and (iii) excerpting brief portions +of the Specification in oral or written communications which discuss the +Specification provided that such excerpts do not in the aggregate +constitute a significant portion of the Specification. + + +2. _License for the Distribution of Compliant Implementations_. Sun also +grants you a perpetual, non-exclusive, non-transferable, worldwide, +fully paid-up, royalty free, limited license (without the right to +sublicense) under any applicable copyrights or, subject to the +provisions of subsection 4 below, patent rights it may have covering the +Specification to create and/or distribute an Independent Implementation +of the Specification that: (a) fully implements the Specification +including all its required interfaces and functionality; (b) does not +modify, subset, superset or otherwise extend the Licensor Name Space, or +include any public or protected packages, classes, Java interfaces, +fields or methods within the Licensor Name Space other than those +required/authorized by the Specification or Specifications being +implemented; and (c) passes the Technology Compatibility Kit (including +satisfying the requirements of the applicable TCK Users Guide) for such +Specification ("Compliant Implementation"). In addition, the foregoing +license is expressly conditioned on your not acting outside its scope. +No license is granted hereunder for any other purpose (including, for +example, modifying the Specification, other than to the extent of your +fair use rights, or distributing the Specification to third parties). +Also, no right, title, or interest in or to any trademarks, service +marks, or trade names of Sun or Sun's licensors is granted hereunder. +Java, and Java-related logos, marks and names are trademarks or +registered trademarks of Sun Microsystems, Inc. in the U.S. and other +countries. + + +3. _Pass-through Conditions_. You need not include limitations (a)-(c) +from the previous paragraph or any other particular "pass through" +requirements in any license You grant concerning the use of your +Independent Implementation or products derived from it. However, except +with respect to Independent Implementations (and products derived from +them) that satisfy limitations (a)-(c) from the previous paragraph, You +may neither: (a) grant or otherwise pass through to your licensees any +licenses under Sun's applicable intellectual property rights; nor (b) +authorize your licensees to make any claims concerning their +implementation's compliance with the Specification in question. + + +4. _Reciprocity Concerning Patent Licenses_. + + +a. With respect to any patent claims covered by the license granted +under subparagraph 2 above that would be infringed by all technically +feasible implementations of the Specification, such license is +conditioned upon your offering on fair, reasonable and +non-discriminatory terms, to any party seeking it from You, a perpetual, +non-exclusive, non-transferable, worldwide license under Your patent +rights which are or would be infringed by all technically feasible +implementations of the Specification to develop, distribute and use a +Compliant Implementation. + + +b With respect to any patent claims owned by Sun and covered by the +license granted under subparagraph 2, whether or not their infringement +can be avoided in a technically feasible manner when implementing the +Specification, such license shall terminate with respect to such claims +if You initiate a claim against Sun that it has, in the course of +performing its responsibilities as the Specification Lead, induced any +other entity to infringe Your patent rights. + + +c Also with respect to any patent claims owned by Sun and covered by the +license granted under subparagraph 2 above, where the infringement of +such claims can be avoided in a technically feasible manner when +implementing the Specification such license, with respect to such +claims, shall terminate if You initiate a claim against Sun that its +making, having made, using, offering to sell, selling or importing a +Compliant Implementation infringes Your patent rights. + + +5. _Definitions_. For the purposes of this Agreement: "Independent +Implementation" shall mean an implementation of the Specification that +neither derives from any of Sun's source code or binary code materials +nor, except with an appropriate and separate license from Sun, includes +any of Sun's source code or binary code materials; "Licensor Name Space" +shall mean the public class or interface declarations whose names begin +with "java", "javax", "com.sun" or their equivalents in any subsequent +naming convention adopted by Sun through the Java Community Process, or +any recognized successors or replacements thereof; and "Technology +Compatibility Kit" or "TCK" shall mean the test suite and accompanying +TCK User's Guide provided by Sun which corresponds to the Specification +and that was available either (i) from Sun's 120 days before the first +release of Your Independent Implementation that allows its use for +commercial purposes, or (ii) more recently than 120 days from such +release but against which You elect to test Your implementation of the +Specification. + + +This Agreement will terminate immediately without notice from Sun if you +breach the Agreement or act outside the scope of the licenses granted above. + + +DISCLAIMER OF WARRANTIES + + +THE SPECIFICATION IS PROVIDED "AS IS". SUN MAKES NO REPRESENTATIONS OR +WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, +WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, +NON-INFRINGEMENT (INCLUDING AS A CONSEQUENCE OF ANY PRACTICE OR +IMPLEMENTATION OF THE SPECIFICATION), OR THAT THE CONTENTS OF THE +SPECIFICATION ARE SUITABLE FOR ANY PURPOSE. This document does not +represent any commitment to release or implement any portion of the +Specification in any product. In addition, the Specification could +include technical inaccuracies or typographical errors. + + +LIMITATION OF LIABILITY + + +TO THE EXTENT NOT PROHIBITED BY LAW, IN NO EVENT WILL SUN OR ITS +LICENSORS BE LIABLE FOR ANY DAMAGES, INCLUDING WITHOUT LIMITATION, LOST +REVENUE, PROFITS OR DATA, OR FOR SPECIAL, INDIRECT, CONSEQUENTIAL, +INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE +THEORY OF LIABILITY, ARISING OUT OF OR RELATED IN ANY WAY TO YOUR +HAVING, IMPLEMENTING OR OTHERWISE USING THE SPECIFICATION, EVEN +IF SUN AND/OR ITS LICENSORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + +You will indemnify, hold harmless, and defend Sun and its licensors from +any claims arising or resulting from: (i) your use of the Specification; +(ii) the use or distribution of your Java application, applet and/or +implementation; and/or (iii) any claims that later versions or releases +of any Specification furnished to you are incompatible with the +Specification provided to you under this license. + + +RESTRICTED RIGHTS LEGEND + + +U.S. Government: If this Specification is being acquired by or on behalf +of the U.S. Government or by a U.S. Government prime contractor or +subcontractor (at any tier), then the Government's rights in the +Software and accompanying documentation shall be only as set forth in +this license; this is in accordance with 48 C.F.R. 227.7201 through +227.7202-4 (for Department of Defense (DoD) acquisitions) and with 48 +C.F.R. 2.101 and 12.212 (for non-DoD acquisitions). + + +REPORT + + +If you provide Sun with any comments or suggestions concerning the +Specification ("Feedback"), you hereby: (i) agree that such Feedback is +provided on a non-proprietary and non-confidential basis, and (ii) grant +Sun a perpetual, non-exclusive, worldwide, fully paid-up, irrevocable +license, with the right to sublicense through multiple levels of +sublicensees, to incorporate, disclose, and use without limitation the +Feedback for any purpose. + + +GENERAL TERMS + + +Any action related to this Agreement will be governed by California law +and controlling U.S. federal law. The U.N. Convention for the +International Sale of Goods and the choice of law rules of any +jurisdiction will not apply. + + +The Specification is subject to U.S. export control laws and may be +subject to export or import regulations in other countries. Licensee +agrees to comply strictly with all such laws and regulations and +acknowledges that it has the responsibility to obtain such licenses to +export, re-export or import as may be required after delivery to Licensee. + + +This Agreement is the parties' entire agreement relating to its subject +matter. It supersedes all prior or contemporaneous oral or written +communications, proposals, conditions, representations and warranties +and prevails over any conflicting or additional terms of any quote, +order, acknowledgment, or other communication between the parties +relating to its subject matter during the term of this Agreement. No +modification to this Agreement will be binding, unless in writing and +signed by an authorized representative of each party. + + + + +Rev. April, 2006 + +Sun/Final/Full Index: libs/freemarker/.cvsignore =================================================================== RCS file: libs/freemarker/.cvsignore diff -N libs/freemarker/.cvsignore --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libs/freemarker/.cvsignore 19 Jan 2007 09:45:40 -0000 1.1.2.1 @@ -0,0 +1 @@ +build Index: libs/freemarker/build.xml =================================================================== RCS file: libs/freemarker/build.xml diff -N libs/freemarker/build.xml --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libs/freemarker/build.xml 19 Jan 2007 09:45:40 -0000 1.1.2.1 @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + +Going to download freemarker binaries! Distributed from http://freemarker.sourceforge.net/ +Or you need to provide path to the freemarker JAR using -Dfreemarker.jar=, download from: +http://mesh.dl.sourceforge.net/sourceforge/freemarker/freemarker-2.3.8.tar.gz + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + For the integration module: + + For the freemarker: + + + + + + + Index: libs/freemarker/manifest.mf =================================================================== RCS file: libs/freemarker/manifest.mf diff -N libs/freemarker/manifest.mf --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libs/freemarker/manifest.mf 22 Jan 2007 18:29:06 -0000 1.1.2.3 @@ -0,0 +1,7 @@ +Manifest-Version: 1.0 +OpenIDE-Module: org.netbeans.libs.freemarker/1 +OpenIDE-Module-Provides: javax.script.ScriptEngine.freemarker +OpenIDE-Module-Specification-Version: 2.3.8 +OpenIDE-Module-Implementation-Version: 238 +OpenIDE-Module-Localizing-Bundle: org/netbeans/libs/freemarker/Bundle.properties + Index: libs/freemarker/nbproject/.cvsignore =================================================================== RCS file: libs/freemarker/nbproject/.cvsignore diff -N libs/freemarker/nbproject/.cvsignore --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libs/freemarker/nbproject/.cvsignore 19 Jan 2007 09:45:36 -0000 1.1.2.1 @@ -0,0 +1 @@ +private Index: libs/freemarker/nbproject/project.properties =================================================================== RCS file: libs/freemarker/nbproject/project.properties diff -N libs/freemarker/nbproject/project.properties --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libs/freemarker/nbproject/project.properties 19 Jan 2007 10:27:08 -0000 1.1.2.2 @@ -0,0 +1,22 @@ +# 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 Copyright 1997-2006 Sun +# Microsystems, Inc. All Rights Reserved. + +is.autoload=true + + +javac.compilerargs=-Xlint:unchecked +javac.source=1.5 Index: libs/freemarker/nbproject/project.xml =================================================================== RCS file: libs/freemarker/nbproject/project.xml diff -N libs/freemarker/nbproject/project.xml --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libs/freemarker/nbproject/project.xml 19 Jan 2007 10:27:08 -0000 1.1.2.2 @@ -0,0 +1,61 @@ + + + + org.netbeans.modules.apisupport.project + + + org.netbeans.libs.freemarker + + + org.netbeans.libs.jsr223 + + + + 1 + 1.6 + + + + org.openide.filesystems + + + + 7.0 + + + + org.openide.util + + + + 7.7 + + + + + org.netbeans.libs.freemarker + + + ext/freemarker-2.3.8.jar + ../external/freemarker-2.3.8.jar + + + + Index: libs/freemarker/src/META-INF/services/javax.script.ScriptEngineFactory =================================================================== RCS file: libs/freemarker/src/META-INF/services/javax.script.ScriptEngineFactory diff -N libs/freemarker/src/META-INF/services/javax.script.ScriptEngineFactory --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libs/freemarker/src/META-INF/services/javax.script.ScriptEngineFactory 19 Jan 2007 22:25:29 -0000 1.1.2.1 @@ -0,0 +1 @@ +org.netbeans.libs.freemarker.FreemarkerFactory Index: libs/freemarker/src/org/netbeans/libs/freemarker/Bundle.properties =================================================================== RCS file: libs/freemarker/src/org/netbeans/libs/freemarker/Bundle.properties diff -N libs/freemarker/src/org/netbeans/libs/freemarker/Bundle.properties --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libs/freemarker/src/org/netbeans/libs/freemarker/Bundle.properties 19 Jan 2007 09:45:41 -0000 1.1.2.1 @@ -0,0 +1,24 @@ +# 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 Copyright 1997-2006 Sun +# Microsystems, Inc. All Rights Reserved. + +OpenIDE-Module-Name=Freemarker Integration +OpenIDE-Module-Display-Category=Libraries +OpenIDE-Module-Short-Description=This module bundles Freemarker. +OpenIDE-Module-Long-Description=\ + The module bundles FreeMarker http://freemarker.sf.net and integrates\ + it into NetBeans. + Index: libs/freemarker/src/org/netbeans/libs/freemarker/FreemarkerEngine.java =================================================================== RCS file: libs/freemarker/src/org/netbeans/libs/freemarker/FreemarkerEngine.java diff -N libs/freemarker/src/org/netbeans/libs/freemarker/FreemarkerEngine.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libs/freemarker/src/org/netbeans/libs/freemarker/FreemarkerEngine.java 20 Jan 2007 09:11:09 -0000 1.1.2.3 @@ -0,0 +1,205 @@ +/* + * 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 scripting.dev.java.net. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. + * + * Portions Copyrighted 2006 Sun Microsystems, Inc. + */ + +package org.netbeans.libs.freemarker; + + +import javax.script.*; +import java.io.*; +import java.util.Properties; +import java.util.Set; +import freemarker.template.*; +import org.openide.filesystems.FileObject; + +/* Taken from A. Sundararajan and adopted by Jaroslav Tulach + * for NetBeans needs. + * + * @author A. Sundararajan + */ +class FreemarkerEngine extends AbstractScriptEngine { + + public static final String STRING_OUTPUT_MODE = "com.sun.script.freemarker.stringOut"; + public static final String FREEMARKER_CONFIG = "com.sun.script.freemarker.config"; + public static final String FREEMARKER_PROPERTIES = "com.sun.script.freemarker.properties"; + public static final String FREEMARKER_TEMPLATE_DIR = "com.sun.script.freemarker.template.dir"; + public static final String FREEMARKER_TEMPLATE = "org.openide.filesystems.FileObject"; + + // my factory, may be null + private volatile ScriptEngineFactory factory; + private volatile Configuration conf; + + public FreemarkerEngine(ScriptEngineFactory factory) { + this.factory = factory; + } + + public FreemarkerEngine() { + this(null); + } + + // ScriptEngine methods + public Object eval(String str, ScriptContext ctx) + throws ScriptException { + return eval(new StringReader(str), ctx); + } + + public Object eval(Reader reader, ScriptContext ctx) + throws ScriptException { + ctx.setAttribute("context", ctx, ScriptContext.ENGINE_SCOPE); + initFreeMarkerConfiguration(ctx); + String fileName = getFilename(ctx); + boolean outputAsString = isStringOutputMode(ctx); + Writer out; + if (outputAsString) { + out = new StringWriter(); + } else { + out = ctx.getWriter(); + } + Bindings engineScope = ctx.getBindings(ScriptContext.ENGINE_SCOPE); + + try { + Template template = new Template(fileName, reader, conf); + template.process(engineScope, out); + out.flush(); + } catch (Exception exp) { + throw new ScriptException(exp); + } + return outputAsString? out.toString() : null; + } + + public ScriptEngineFactory getFactory() { + if (factory == null) { + synchronized (this) { + if (factory == null) { + factory = new FreemarkerFactory(); + } + } + } + return factory; + } + + public Bindings createBindings() { + return new SimpleBindings(); + } + + // internals only below this point + private static String getFilename(ScriptContext ctx) { + Object fileName = ctx.getAttribute(ScriptEngine.FILENAME); + return fileName != null? fileName.toString() : ""; + } + + private static boolean isStringOutputMode(ScriptContext ctx) { + Object flag = ctx.getAttribute(STRING_OUTPUT_MODE); + if (flag != null) { + return flag.equals(Boolean.TRUE); + } else { + return false; + } + } + + private void initFreeMarkerConfiguration(ScriptContext ctx) { + if (conf == null) { + synchronized (this) { + if (conf != null) { + return; + } + Object cfg = ctx.getAttribute(FREEMARKER_CONFIG); + if (cfg instanceof Configuration) { + conf = (Configuration) cfg; + return; + } + + Object tfo = ctx.getAttribute(FREEMARKER_TEMPLATE); + FileObject fo = tfo instanceof FileObject ? (FileObject)tfo : null; + + Configuration tmpConf = new RsrcLoader(fo, ctx); + try { + initConfProps(tmpConf, ctx); + initTemplateDir(tmpConf, fo, ctx); + } catch (RuntimeException rexp) { + throw rexp; + } catch (Exception exp) { + throw new RuntimeException(exp); + } + conf = tmpConf; + } + } + } + + private static void initConfProps(Configuration conf, ScriptContext ctx) { + try { + Properties props = null; + Object tmp = ctx.getAttribute(FREEMARKER_PROPERTIES); + if (props instanceof Properties) { + props = (Properties) tmp; + } else { + String propsName = System.getProperty(FREEMARKER_PROPERTIES); + if (propsName != null) { + File propsFile = new File(propsName); + if (propsFile.exists() && propsFile.canRead()) { + props = new Properties(); + props.load(new FileInputStream(propsFile)); + } + } + } + if (props != null) { + Set keys = props.keySet(); + for (Object obj : keys) { + String key; + if (obj instanceof String) { + key = (String) obj; + } else { + continue; + } + try { + conf.setSetting(key, props.get(key).toString()); + } catch (TemplateException te) { + // ignore + } + } + } + } catch (RuntimeException re) { + throw re; + } catch (Exception exp) { + throw new RuntimeException(exp); + } + } + + private static void initTemplateDir(Configuration conf, FileObject fo, ScriptContext ctx) { + try { + Object tmp = ctx.getAttribute(FREEMARKER_TEMPLATE_DIR); + String dirName; + if (tmp != null) { + dirName = tmp.toString(); + } else { + if (fo != null) { + return; + } + tmp = System.getProperty(FREEMARKER_TEMPLATE_DIR); + dirName = (tmp == null)? "." : tmp.toString(); + } + File dir = new File(dirName); + if (dir.exists() && dir.isDirectory()) { + conf.setDirectoryForTemplateLoading(dir); + } + } catch (IOException exp) { + throw new RuntimeException(exp); + } + } +} Index: libs/freemarker/src/org/netbeans/libs/freemarker/FreemarkerFactory.java =================================================================== RCS file: libs/freemarker/src/org/netbeans/libs/freemarker/FreemarkerFactory.java diff -N libs/freemarker/src/org/netbeans/libs/freemarker/FreemarkerFactory.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libs/freemarker/src/org/netbeans/libs/freemarker/FreemarkerFactory.java 19 Jan 2007 22:25:30 -0000 1.1.2.1 @@ -0,0 +1,144 @@ +/* + * 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 scripting.dev.java.net. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. + * + * Portions Copyrighted 2006 Sun Microsystems, Inc. + */ + +/* + * @author A. Sundararajan + */ +package org.netbeans.libs.freemarker; + +import javax.script.*; +import java.util.*; + +public class FreemarkerFactory implements ScriptEngineFactory { + public String getEngineName() { + return "freemarker"; + } + + public String getEngineVersion() { + return "2.3.8"; + } + + public List getExtensions() { + return extensions; + } + + public String getLanguageName() { + return "freemarker"; + } + + public String getLanguageVersion() { + return "2.3.8"; + } + + public String getMethodCallSyntax(String obj, String m, String... args) { + StringBuffer buf = new StringBuffer(); + buf.append("${"); + buf.append(obj); + buf.append("."); + buf.append(m); + buf.append("("); + if (args.length != 0) { + int i = 0; + for (; i < args.length - 1; i++) { + buf.append("$" + args[i]); + buf.append(", "); + } + buf.append("$" + args[i]); + } + buf.append(")}"); + return buf.toString(); + } + + public List getMimeTypes() { + return mimeTypes; + } + + public List getNames() { + return names; + } + + public String getOutputStatement(String toDisplay) { + StringBuffer buf = new StringBuffer(); + int len = toDisplay.length(); + buf.append("${context.getWriter().write(\""); + for (int i = 0; i < len; i++) { + char ch = toDisplay.charAt(i); + switch (ch) { + case '"': + buf.append("\\\""); + break; + case '\\': + buf.append("\\\\"); + break; + default: + buf.append(ch); + break; + } + } + buf.append("\")}"); + return buf.toString(); + } + + public String getParameter(String key) { + if (key.equals(ScriptEngine.NAME)) { + return getLanguageName(); + } else if (key.equals(ScriptEngine.ENGINE)) { + return getEngineName(); + } else if (key.equals(ScriptEngine.ENGINE_VERSION)) { + return getEngineVersion(); + } else if (key.equals(ScriptEngine.LANGUAGE)) { + return getLanguageName(); + } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) { + return getLanguageVersion(); + } else if (key.equals("THREADING")) { + return "MULTITHREADED"; + } else { + return null; + } + } + + public String getProgram(String... statements) { + StringBuffer buf = new StringBuffer(); + for (int i = 0; i < statements.length; i++) { + buf.append(statements[i]); + buf.append("\n"); + } + return buf.toString(); + } + + public ScriptEngine getScriptEngine() { + return new FreemarkerEngine(this); + } + + private static List names; + private static List extensions; + private static List mimeTypes; + static { + names = new ArrayList(2); + names.add("FreeMarker"); + names.add("freemarker"); + names = Collections.unmodifiableList(names); + extensions = new ArrayList(1); + extensions.add("fm"); + extensions = Collections.unmodifiableList(extensions); + mimeTypes = new ArrayList(0); + mimeTypes = Collections.unmodifiableList(mimeTypes); + } +} Index: libs/freemarker/src/org/netbeans/libs/freemarker/RsrcLoader.java =================================================================== RCS file: libs/freemarker/src/org/netbeans/libs/freemarker/RsrcLoader.java diff -N libs/freemarker/src/org/netbeans/libs/freemarker/RsrcLoader.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libs/freemarker/src/org/netbeans/libs/freemarker/RsrcLoader.java 22 Jan 2007 18:29:05 -0000 1.1.2.3 @@ -0,0 +1,128 @@ +/* +* The contents of this file are subject to the terms of the +* Common Development and Distribution License, Version 1.0 only +* (the "License"). You may not use this file except in compliance +* with the License. A copy of the license is available +* at http://www.opensource.org/licenses/cddl1.php +* +* See the License for the specific language governing permissions +* and limitations under the License. +* +* The Original Code is the nbdoclet.sf.net project. +* The Initial Developer of the Original Code is Petr Zajac. +* Portions created by Petr Zajac are Copyright (C) 2006. +* Portions created by Jaroslav Tulach are Copyright (C) 2006. +* Portions Copyrighted 2007 Sun Microsystems, Inc. +* All Rights Reserved. +*/ +package org.netbeans.libs.freemarker; + +import freemarker.cache.TemplateLoader; +import freemarker.template.Configuration; +import freemarker.template.TemplateModel; +import freemarker.template.TemplateModelException; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.Enumeration; +import java.util.LinkedHashSet; +import java.util.Set; +import javax.script.ScriptContext; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.Repository; +import org.openide.util.Exceptions; + +/** + * Velocity templates resource loader rewritten for Freemarker to + * access resources via FileSystem. + * + * @author Petr Zajac, adopted by Jaroslav Tulach + */ + +final class RsrcLoader extends Configuration implements TemplateLoader { + private FileObject fo; + private ScriptContext map; + + RsrcLoader(FileObject fo, ScriptContext map) { + this.fo = fo; + this.map = map; + setTemplateLoader(this); + } + + private FileObject getFile(String name) { + FileObject fo = (getFolder() == null) ? null : getFolder().getFileObject(name); + return fo; + } + + private FileObject getFolder() { + return fo != null ? fo.getParent() : Repository.getDefault().getDefaultFileSystem().getRoot(); + } + + public Object findTemplateSource(String string) throws IOException { + FileObject fo = getFile(string); + return fo == null ? null : new Wrap(fo); + } + + public long getLastModified(Object object) { + return ((Wrap)object).fo.lastModified().getTime(); + } + + public Reader getReader(Object object, String encoding) throws IOException { + Wrap w = (Wrap)object; + if (w.reader == null) { + w.reader = new InputStreamReader(w.fo.getInputStream(), encoding); + } + return w.reader; + } + + public void closeTemplateSource(Object object) throws IOException { + Wrap w = (Wrap)object; + if (w.reader != null) { + w.reader.close(); + } + } + + public Object put(String string, Object object) { + assert false; + return null; + } + + public TemplateModel getSharedVariable(String string) { + Object value = map == null ? null : map.getAttribute(string); + if (value == null || fo != null) { + value = fo.getAttribute(string); + } + try { + return getObjectWrapper().wrap(value); + } catch (TemplateModelException ex) { + Exceptions.printStackTrace(ex); + return null; + } + } + + public Set getSharedVariableNames() { + LinkedHashSet keys = new LinkedHashSet(); + + if (map != null) { + keys.addAll(map.getBindings(map.ENGINE_SCOPE).keySet()); + } + + if (fo != null) { + Enumeration en = fo.getAttributes(); + while (en.hasMoreElements()) { + keys.add(en.nextElement()); + } + } + + return keys; + } + + private static final class Wrap { + public FileObject fo; + public Reader reader; + + public Wrap(FileObject fo) { + this.fo = fo; + } + } // end Wrap +} Index: libs/freemarker/test/unit/src/org/netbeans/api/templates/ProcessorTest.java =================================================================== RCS file: libs/freemarker/test/unit/src/org/netbeans/api/templates/ProcessorTest.java diff -N libs/freemarker/test/unit/src/org/netbeans/api/templates/ProcessorTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libs/freemarker/test/unit/src/org/netbeans/api/templates/ProcessorTest.java 22 Jan 2007 18:28:59 -0000 1.1.2.4 @@ -0,0 +1,215 @@ +/* +* The contents of this file are subject to the terms of the +* Common Development and Distribution License, Version 1.0 only +* (the "License"). You may not use this file except in compliance +* with the License. A copy of the license is available +* at http://www.opensource.org/licenses/cddl1.php +* +* See the License for the specific language governing permissions +* and limitations under the License. +* +* The Original Code is the dvbcentral.sf.net project. +* The Initial Developer of the Original Code is Jaroslav Tulach. +* Portions created by Jaroslav Tulach are Copyright (C) 2006. +* All Rights Reserved. +*/ +package org.netbeans.api.templates; + +import java.awt.Color; +import java.awt.Panel; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.StringWriter; +import java.io.Writer; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import javax.script.ScriptContext; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import junit.framework.TestCase; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.filesystems.Repository; + +/** + * + * @author Jaroslav Tulach + */ +public class ProcessorTest extends TestCase { + FileObject root; + + public ProcessorTest(String testName) { + super(testName); + } + + protected void setUp() throws Exception { + root = Repository.getDefault().getDefaultFileSystem().getRoot(); + for (FileObject f : root.getChildren()) { + f.delete(); + } + } + + protected void tearDown() throws Exception { + } + + public void testApply() throws Exception { + FileObject template = FileUtil.createData(root, "some.txt"); + OutputStream os = template.getOutputStream(); + String txt = "

${title}

"; + os.write(txt.getBytes()); + os.close(); + template.setAttribute("title", "Nazdar"); + + StringWriter w = new StringWriter(); + + apply(template, w); + + String exp = "

Nazdar

"; + assertEquals(exp, w.toString()); + } + + public void testCanHandleComplexData() throws Exception { + Panel p = new Panel(); + p.setForeground(Color.BLUE); + + FileObject template = FileUtil.createData(root, "some.txt"); + OutputStream os = template.getOutputStream(); + String txt = "

${panel.foreground.red} ${panel.foreground.green} ${panel.foreground.blue}

"; + os.write(txt.getBytes()); + os.close(); + template.setAttribute("panel", p); + + StringWriter w = new StringWriter(); + + apply(template, w); + + String exp = "

0 0 255

"; + assertEquals(exp, w.toString()); + } + + public void testCanHandleImport() throws Exception { + Panel p = new Panel(); + p.setForeground(Color.BLUE); + + FileObject imp = FileUtil.createData(root, "import.txt"); + { + OutputStream os = imp.getOutputStream(); + String txt = "${panel.foreground.blue}"; + os.write(txt.getBytes()); + os.close(); + } + + FileObject template = FileUtil.createData(root, "some.txt"); + { + OutputStream os = template.getOutputStream(); + String txt = "

<#include \"import.txt\">

"; + os.write(txt.getBytes()); + os.close(); + template.setAttribute("panel", p); + } + StringWriter w = new StringWriter(); + + apply(template, w); + + String exp = "

255

"; + assertEquals(exp, w.toString()); + } + public void testImportCanInheritVariable() throws Exception { + Panel p = new Panel(); + p.setForeground(Color.BLUE); + + FileObject imp = FileUtil.createData(root, "import.txt"); + { + OutputStream os = imp.getOutputStream(); + String txt = "${prefix} First Line\n" + + "${prefix} Second Line\n"; + os.write(txt.getBytes()); + os.close(); + } + + FileObject template = FileUtil.createData(root, "some.txt"); + { + OutputStream os = template.getOutputStream(); + String txt = "<#assign prefix = \"#\">" + + "<#include \"import.txt\">"; + os.write(txt.getBytes()); + os.close(); + template.setAttribute("panel", p); + } + StringWriter w = new StringWriter(); + + apply(template, w); + + String exp = "# First Line\n" + + "# Second Line\n"; + assertEquals(exp, w.toString()); + } + public void testImportCanInheritVariableInSubFolder() throws Exception { + Panel p = new Panel(); + p.setForeground(Color.BLUE); + + FileObject imp = FileUtil.createData(root, "sub/import.txt"); + { + OutputStream os = imp.getOutputStream(); + String txt = "${prefix} First Line\n" + + "${prefix} Second Line\n"; + os.write(txt.getBytes()); + os.close(); + } + + FileObject template = FileUtil.createData(root, "sub/some.txt"); + { + OutputStream os = template.getOutputStream(); + String txt = "<#assign prefix=\"#\">" + + "<#include \"import.txt\">"; + os.write(txt.getBytes()); + os.close(); + template.setAttribute("panel", p); + } + StringWriter w = new StringWriter(); + + apply(template, w); + + String exp = "# First Line\n" + + "# Second Line\n"; + assertEquals(exp, w.toString()); + } + public void testAbilityToSendOwnTemplate() throws Exception { + Map myValues = new HashMap(); + myValues.put("prefix", "#"); + + FileObject template = FileUtil.createData(root, "some.txt"); + { + OutputStream os = template.getOutputStream(); + String txt = "${prefix} First Line\n" + + "${prefix} Second Line\n"; + os.write(txt.getBytes()); + os.close(); + template.setAttribute("prefix", " * "); + } + StringWriter w = new StringWriter(); + + apply(template, w, myValues); + + String exp = "# First Line\n" + + "# Second Line\n"; + assertEquals(exp, w.toString()); + } + + private static void apply(FileObject template, Writer w) throws Exception { + apply(template, w, Collections.emptyMap()); + } + + private static void apply(FileObject template, Writer w, Map values) throws Exception { + ScriptEngineManager mgr = new ScriptEngineManager(); + ScriptEngine eng = mgr.getEngineByName("freemarker"); + assertNotNull("We do have such engine", eng); + eng.getContext().setWriter(w); + eng.getContext().setAttribute(FileObject.class.getName(), template, ScriptContext.ENGINE_SCOPE); + eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE).putAll(values); + eng.eval(new InputStreamReader(template.getInputStream())); + } + +}