diff --git a/ant.freeform/src/org/netbeans/modules/ant/freeform/FreeformProject.java b/ant.freeform/src/org/netbeans/modules/ant/freeform/FreeformProject.java --- a/ant.freeform/src/org/netbeans/modules/ant/freeform/FreeformProject.java +++ b/ant.freeform/src/org/netbeans/modules/ant/freeform/FreeformProject.java @@ -129,7 +129,7 @@ public final class FreeformProject imple new FreeformSharabilityQuery(helper()), //SharabilityQueryImplementation Accessor.DEFAULT.createProjectAccessor(this), //Access to AntProjectHelper and PropertyEvaluator FEQImpl, // FileEncodingQueryImplementation - new FreeformTemplateAttributesProvider(helper(), eval) + new FreeformTemplateAttributesProvider(helper(), eval, FEQImpl) ); return LookupProviderSupport.createCompositeLookup(baseLookup, "Projects/org-netbeans-modules-ant-freeform/Lookup"); //NOI18N } diff --git a/ant.freeform/src/org/netbeans/modules/ant/freeform/FreeformTemplateAttributesProvider.java b/ant.freeform/src/org/netbeans/modules/ant/freeform/FreeformTemplateAttributesProvider.java --- a/ant.freeform/src/org/netbeans/modules/ant/freeform/FreeformTemplateAttributesProvider.java +++ b/ant.freeform/src/org/netbeans/modules/ant/freeform/FreeformTemplateAttributesProvider.java @@ -41,7 +41,9 @@ package org.netbeans.modules.ant.freeform; +import java.nio.charset.Charset; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import org.netbeans.modules.ant.freeform.spi.support.Util; import org.netbeans.spi.project.support.ant.AntProjectHelper; @@ -65,19 +67,29 @@ public class FreeformTemplateAttributesP private final AntProjectHelper helper; private final PropertyEvaluator evaluator; + private final FreeformFileEncodingQueryImpl encodingQuery; - public FreeformTemplateAttributesProvider(AntProjectHelper helper, PropertyEvaluator eval) { + public FreeformTemplateAttributesProvider(AntProjectHelper helper, PropertyEvaluator eval, FreeformFileEncodingQueryImpl encodingQuery) { this.helper = helper; this.evaluator = eval; + this.encodingQuery = encodingQuery; } public Map attributesFor(DataObject template, DataFolder target, String name) { Element primData = Util.getPrimaryConfigurationData(helper); Element licenseEl = Util.findElement(primData, "project-license", Util.NAMESPACE); // NOI18N - if (licenseEl != null) { - return Collections.singletonMap("project", Collections.singletonMap("license", evaluator.evaluate(Util.findText(licenseEl)))); // NOI18N + Charset charset = encodingQuery.getEncoding(target.getPrimaryFile()); + if (licenseEl == null && charset == null) { + return null; } else { - return null; + Map values = new HashMap(); + if (licenseEl != null) { + values.put("license", evaluator.evaluate(Util.findText(licenseEl))); // NOI18N + } + if (charset != null) { + values.put("encoding", charset.name()); // NOI18N + } + return Collections.singletonMap("project", values); // NOI18N } } diff --git a/ant.freeform/test/unit/data/example-projects/simplewithlicense/nbproject/project.xml b/ant.freeform/test/unit/data/example-projects/simplewithlicense/nbproject/project.xml --- a/ant.freeform/test/unit/data/example-projects/simplewithlicense/nbproject/project.xml +++ b/ant.freeform/test/unit/data/example-projects/simplewithlicense/nbproject/project.xml @@ -15,6 +15,11 @@ java src UTF-8 + + + + java + src2 diff --git a/ant.freeform/test/unit/data/example-projects/simplewithlicense/src2/org/foo/AnotherClass.java b/ant.freeform/test/unit/data/example-projects/simplewithlicense/src2/org/foo/AnotherClass.java new file mode 100644 --- /dev/null +++ b/ant.freeform/test/unit/data/example-projects/simplewithlicense/src2/org/foo/AnotherClass.java @@ -0,0 +1,37 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, 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-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2007 Sun Microsystems, Inc. + */ + +package org.foo; + +/** + * + * @author Milan + */ +public class AnotherClass { + +} diff --git a/ant.freeform/test/unit/src/org/netbeans/modules/ant/freeform/FreeformTemplateAttributesProviderTest.java b/ant.freeform/test/unit/src/org/netbeans/modules/ant/freeform/FreeformTemplateAttributesProviderTest.java --- a/ant.freeform/test/unit/src/org/netbeans/modules/ant/freeform/FreeformTemplateAttributesProviderTest.java +++ b/ant.freeform/test/unit/src/org/netbeans/modules/ant/freeform/FreeformTemplateAttributesProviderTest.java @@ -42,11 +42,15 @@ package org.netbeans.modules.ant.freefor package org.netbeans.modules.ant.freeform; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import org.netbeans.api.project.Project; import org.netbeans.api.project.ProjectManager; +import org.netbeans.api.project.ProjectUtils; +import org.netbeans.api.project.SourceGroup; import org.openide.filesystems.FileObject; import org.openide.loaders.CreateFromTemplateAttributesProvider; +import org.openide.loaders.DataFolder; /** * Test for freeform template attributes provider, currently providing only @@ -64,9 +68,21 @@ public class FreeformTemplateAttributesP FileObject projdir = egdirFO.getFileObject("simplewithlicense"); Project simpleWithLicense = ProjectManager.getDefault().findProject(projdir); CreateFromTemplateAttributesProvider provider = simpleWithLicense.getLookup().lookup(CreateFromTemplateAttributesProvider.class); - Map expResult = Collections.singletonMap("project", Collections.singletonMap("license", "cddl-netbeans-sun"));; - Map result = provider.attributesFor(null, null, null); - assertEquals(expResult, result); + SourceGroup[] groups = ProjectUtils.getSources(simpleWithLicense).getSourceGroups("java"); // JavaProjectConstants.SOURCES_TYPE_JAVA + for (SourceGroup group : groups) { + FileObject root = group.getRootFolder(); + Map result = provider.attributesFor(null, DataFolder.findFolder(root), null); + assertEquals(1, result.size()); + Map values = (Map)result.get("project"); + if (root.getName().equals("src")) { + Map expected = new HashMap(); + expected.put("license", "cddl-netbeans-sun"); + expected.put("encoding", "UTF-8"); + assertEquals(expected, values); + } else { + assertEquals(Collections.singletonMap("license", "cddl-netbeans-sun"), values); + } + } } } diff --git a/apisupport.project/src/org/netbeans/modules/apisupport/project/queries/TemplateAttributesProvider.java b/apisupport.project/src/org/netbeans/modules/apisupport/project/queries/TemplateAttributesProvider.java --- a/apisupport.project/src/org/netbeans/modules/apisupport/project/queries/TemplateAttributesProvider.java +++ b/apisupport.project/src/org/netbeans/modules/apisupport/project/queries/TemplateAttributesProvider.java @@ -42,6 +42,7 @@ package org.netbeans.modules.apisupport. package org.netbeans.modules.apisupport.project.queries; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import org.netbeans.spi.project.support.ant.AntProjectHelper; import org.netbeans.spi.project.support.ant.EditableProperties; @@ -68,11 +69,12 @@ public class TemplateAttributesProvider if (license == null && netBeansOrg) { license = "cddl-netbeans-sun"; // NOI18N } - if (license == null) { - return null; - } else { - return Collections.singletonMap("project", Collections.singletonMap("license", license)); // NOI18N + Map values = new HashMap(); + if (license != null) { + values.put("license", license); // NOI18N } + values.put("encoding", "UTF-8"); // NOI18N + return Collections.singletonMap("project", values); // NOI18N } }