diff options
author | Tor Norbye <tnorbye@google.com> | 2012-09-12 07:55:40 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-09-12 07:55:40 -0700 |
commit | c2548413ad5c85a5e2863fb178f478a9fad3d0f6 (patch) | |
tree | 4dbd2302aa9a10d1c4cfd5ed88a02bee4d661ec7 | |
parent | 866d1389388fbfd26101b3e7d2fa2c6c9d67f026 (diff) | |
download | sdk-c2548413ad5c85a5e2863fb178f478a9fad3d0f6.zip sdk-c2548413ad5c85a5e2863fb178f478a9fad3d0f6.tar.gz sdk-c2548413ad5c85a5e2863fb178f478a9fad3d0f6.tar.bz2 |
Add class to resource name conversion for templates
This is to support a template name update in issue 36890.
Change-Id: I47458c094c8dd601e086da0cb1c6fafb7680b58c
10 files changed, 408 insertions, 2 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/.classpath b/eclipse/plugins/com.android.ide.eclipse.adt/.classpath index dfbcb1a..c035541 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/.classpath +++ b/eclipse/plugins/com.android.ide.eclipse.adt/.classpath @@ -10,9 +10,9 @@ <classpathentry exported="true" kind="lib" path="libs/lint_checks.jar" sourcepath="/lint-checks"/> <classpathentry exported="true" kind="lib" path="libs/assetstudio.jar" sourcepath="/assetstudio"/> <classpathentry exported="true" kind="lib" path="libs/ant-glob.jar"/> + <classpathentry exported="true" kind="lib" path="libs/freemarker-2.3.19.jar"/> <classpathentry exported="true" kind="lib" path="libs/manifmerger.jar" sourcepath="/ManifestMerger"/> <classpathentry kind="var" path="ANDROID_SRC/sdk/eclipse/plugins/com.android.ide.eclipse.adt/libs/propertysheet.jar" sourcepath="/ANDROID_SRC/external/eclipse-windowbuilder/propertysheet/src"/> - <classpathentry kind="var" path="ANDROID_SRC/prebuilts/tools/common/freemarker/freemarker-2.3.19.jar" sourcepath="/ANDROID_SRC/prebuilts/tools/common/freemarker/src.zip"/> <classpathentry kind="var" path="ANDROID_SRC/prebuilts/tools/common/asm-tools/asm-4.0.jar" sourcepath="/ANDROID_SRC/prebuilts/tools/common/asm-tools/src.zip"/> <classpathentry kind="var" path="ANDROID_SRC/prebuilts/tools/common/asm-tools/asm-tree-4.0.jar" sourcepath="/ANDROID_SRC/prebuilts/tools/common/asm-tools/src.zip"/> <classpathentry kind="var" path="ANDROID_SRC/prebuilts/tools/common/asm-tools/asm-analysis-4.0.jar" sourcepath="/ANDROID_SRC/prebuilts/tools/common/asm-tools/src.zip"/> diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/META-INF/MANIFEST.MF b/eclipse/plugins/com.android.ide.eclipse.adt/META-INF/MANIFEST.MF index f6cd70b..738bf5f 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/META-INF/MANIFEST.MF +++ b/eclipse/plugins/com.android.ide.eclipse.adt/META-INF/MANIFEST.MF @@ -52,7 +52,7 @@ Require-Bundle: com.android.ide.eclipse.base, org.eclipse.ltk.ui.refactoring, org.eclipse.core.expressions, org.eclipse.compare -Eclipse-LazyStart: true +Bundle-ActivationPolicy: lazy Export-Package: com.android.assetstudiolib;x-friends:="com.android.ide.eclipse.tests", com.android.ide.common.api;x-friends:="com.android.ide.eclipse.tests", com.android.ide.common.layout;x-friends:="com.android.ide.eclipse.tests", @@ -137,6 +137,8 @@ Export-Package: com.android.assetstudiolib;x-friends:="com.android.ide.eclipse.t com.android.tools.lint.client.api;x-friends:="com.android.ide.eclipse.tests", com.android.tools.lint.detector.api;x-friends:="com.android.ide.eclipse.tests", com.android.util;x-friends:="com.android.ide.eclipse.tests", + freemarker.cache;x-friends:="com.android.ide.eclipse.tests", + freemarker.template;x-friends:="com.android.ide.eclipse.tests", org.kxml2.io;x-friends:="com.android.ide.eclipse.tests", org.kxml2.kdom;x-friends:="com.android.ide.eclipse.tests", org.kxml2.wap;x-friends:="com.android.ide.eclipse.tests", diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/FmClassNameToResourceMethod.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/FmClassNameToResourceMethod.java new file mode 100644 index 0000000..366de9a --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/FmClassNameToResourceMethod.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Eclipse Public License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.eclipse.org/org/documents/epl-v10.php + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.ide.eclipse.adt.internal.wizards.templates; + +import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectPage.ACTIVITY_NAME_SUFFIX; + +import com.android.ide.eclipse.adt.AdtUtils; + +import freemarker.template.SimpleScalar; +import freemarker.template.TemplateMethodModel; +import freemarker.template.TemplateModel; +import freemarker.template.TemplateModelException; + +import java.util.List; + +/** + * Similar to {@link FmCamelCaseToUnderscoreMethod}, but strips off common class + * suffixes such as "Activity", "Fragment", etc. + */ +public class FmClassNameToResourceMethod implements TemplateMethodModel { + @Override + public TemplateModel exec(List args) throws TemplateModelException { + if (args.size() != 1) { + throw new TemplateModelException("Wrong arguments"); + } + + String name = args.get(0).toString(); + + if (name.isEmpty()) { + return new SimpleScalar(""); + } + + name = stripSuffix(name, ACTIVITY_NAME_SUFFIX); + name = stripSuffix(name, "Fragment"); //$NON-NLS-1$ + name = stripSuffix(name, "Service"); //$NON-NLS-1$ + name = stripSuffix(name, "Provider"); //$NON-NLS-1$ + + return new SimpleScalar(AdtUtils.camelCaseToUnderlines(name)); + } + + // Strip off the end portion of the activity name. The user might be typing + // the activity name such that only a portion has been entered so far (e.g. + // "MainActivi") and we want to chop off that portion too such that we don't + private static String stripSuffix(String name, String suffix) { + int suffixStart = name.lastIndexOf(suffix.charAt(0)); + if (suffixStart != -1 && name.regionMatches(suffixStart, suffix, 0, + name.length() - suffixStart)) { + name = name.substring(0, suffixStart); + } + assert !name.endsWith(suffix) : name; + + return name; + } +}
\ No newline at end of file diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/TemplateHandler.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/TemplateHandler.java index 16f891f..5bb5565 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/TemplateHandler.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/templates/TemplateHandler.java @@ -314,6 +314,7 @@ class TemplateHandler { paramMap.put("underscoreToCamelCase", new FmUnderscoreToCamelCaseMethod()); //$NON-NLS-1$ paramMap.put("activityToLayout", new FmActivityToLayoutMethod()); //$NON-NLS-1$ paramMap.put("layoutToActivity", new FmLayoutToActivityMethod()); //$NON-NLS-1$ + paramMap.put("classToResource", new FmClassNameToResourceMethod()); //$NON-NLS-1$ // This should be handled better: perhaps declared "required packages" as part of the // inputs? (It would be better if we could conditionally disable template based diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmActivityToLayoutMethodTest.java b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmActivityToLayoutMethodTest.java new file mode 100644 index 0000000..9caeab2 --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmActivityToLayoutMethodTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Eclipse Public License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.eclipse.org/org/documents/epl-v10.php + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.ide.eclipse.adt.internal.wizards.templates; + +import freemarker.template.SimpleScalar; +import freemarker.template.TemplateModelException; + +import java.util.Collections; +import java.util.List; + +import junit.framework.TestCase; + +@SuppressWarnings("javadoc") +public class FmActivityToLayoutMethodTest extends TestCase { + @SuppressWarnings("rawtypes") + private void check(String s, String expected) throws TemplateModelException { + FmActivityToLayoutMethod method = new FmActivityToLayoutMethod(); + List list = Collections.singletonList(new SimpleScalar(s)); + assertEquals(expected, ((SimpleScalar) method.exec(list)).getAsString()); + } + + public void test1() throws Exception { + check("FooActivity", "activity_foo"); + } + + public void test2() throws Exception { + check("FooActiv", "activity_foo"); + } + + public void test3() throws Exception { + check("Foo", "activity_foo"); + } + + public void test4() throws Exception { + check("", ""); + } +} diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmCamelCaseToUnderscoreMethodTest.java b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmCamelCaseToUnderscoreMethodTest.java new file mode 100644 index 0000000..dee0ce6 --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmCamelCaseToUnderscoreMethodTest.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Eclipse Public License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.eclipse.org/org/documents/epl-v10.php + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.ide.eclipse.adt.internal.wizards.templates; + +import freemarker.template.SimpleScalar; +import freemarker.template.TemplateModelException; + +import java.util.Collections; +import java.util.List; + +import junit.framework.TestCase; + +@SuppressWarnings("javadoc") +public class FmCamelCaseToUnderscoreMethodTest extends TestCase { + @SuppressWarnings("rawtypes") + private void check(String s, String expected) throws TemplateModelException { + FmCamelCaseToUnderscoreMethod method = new FmCamelCaseToUnderscoreMethod(); + List list = Collections.singletonList(new SimpleScalar(s)); + assertEquals(expected, ((SimpleScalar) method.exec(list)).getAsString()); + } + + public void test1() throws Exception { + check("", ""); + } + + public void test2() throws Exception { + check("foo", "foo"); + } + + public void test3() throws Exception { + check("Foo", "foo"); + } + + public void test4() throws Exception { + check("FooBar", "foo_bar"); + } + + public void test5() throws Exception { + check("testXML", "test_xml"); + } + + public void test6() throws Exception { + check("testFoo", "test_foo"); + } +} diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmClassNameToResourceMethodTest.java b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmClassNameToResourceMethodTest.java new file mode 100644 index 0000000..ff90ea0 --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmClassNameToResourceMethodTest.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Eclipse Public License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.eclipse.org/org/documents/epl-v10.php + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.ide.eclipse.adt.internal.wizards.templates; + +import freemarker.template.SimpleScalar; +import freemarker.template.TemplateModelException; + +import java.util.Collections; +import java.util.List; + +import junit.framework.TestCase; + +@SuppressWarnings("javadoc") +public class FmClassNameToResourceMethodTest extends TestCase { + @SuppressWarnings("rawtypes") + private void check(String s, String expected) throws TemplateModelException { + FmClassNameToResourceMethod method = new FmClassNameToResourceMethod(); + List list = Collections.singletonList(new SimpleScalar(s)); + assertEquals(expected, ((SimpleScalar) method.exec(list)).getAsString()); + } + + public void test1() throws Exception { + check("FooActivity", "foo"); + } + + public void test2() throws Exception { + check("FooActiv", "foo"); + } + + public void test3() throws Exception { + check("Foo", "foo"); + } + + public void test4() throws Exception { + check("", ""); + } + + public void test5() throws Exception { + check("FooFragment", "foo"); + } + + public void test6() throws Exception { + check("FooService", "foo"); + } + + public void test7() throws Exception { + check("FooProvider", "foo"); + } + + public void test8() throws Exception { + check("FooBar", "foo_bar"); + } +} diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmLayoutToActivityMethodTest.java b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmLayoutToActivityMethodTest.java new file mode 100644 index 0000000..af0a1db --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmLayoutToActivityMethodTest.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Eclipse Public License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.eclipse.org/org/documents/epl-v10.php + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.ide.eclipse.adt.internal.wizards.templates; + +import freemarker.template.SimpleScalar; +import freemarker.template.TemplateModelException; + +import java.util.Collections; +import java.util.List; + +import junit.framework.TestCase; + +@SuppressWarnings("javadoc") +public class FmLayoutToActivityMethodTest extends TestCase { + @SuppressWarnings("rawtypes") + private void check(String s, String expected) throws TemplateModelException { + FmLayoutToActivityMethod method = new FmLayoutToActivityMethod(); + List list = Collections.singletonList(new SimpleScalar(s)); + assertEquals(expected, ((SimpleScalar) method.exec(list)).getAsString()); + } + + public void test1() throws Exception { + check("foo", "FooActivity"); + } + + public void test2() throws Exception { + check("activity_foo", "FooActivity"); + } + + public void test3() throws Exception { + check("activity_", "MyActivity"); + } + + public void test4() throws Exception { + check("activ", "ActivActivity"); + } + + public void test5() throws Exception { + check("", "MyActivity"); + } +} diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmSlashedPackageNameMethodTest.java b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmSlashedPackageNameMethodTest.java new file mode 100644 index 0000000..4391102 --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmSlashedPackageNameMethodTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Eclipse Public License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.eclipse.org/org/documents/epl-v10.php + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.ide.eclipse.adt.internal.wizards.templates; + +import freemarker.template.SimpleScalar; +import freemarker.template.TemplateModelException; + +import java.util.Collections; +import java.util.List; + +import junit.framework.TestCase; + +@SuppressWarnings("javadoc") +public class FmSlashedPackageNameMethodTest extends TestCase { + @SuppressWarnings("rawtypes") + private void check(String s, String expected) throws TemplateModelException { + FmSlashedPackageNameMethod method = new FmSlashedPackageNameMethod(); + List list = Collections.singletonList(new SimpleScalar(s)); + assertEquals(expected, ((SimpleScalar) method.exec(list)).getAsString()); + } + + public void test1() throws Exception { + check("", ""); + } + + public void test2() throws Exception { + check("foo", "foo"); + } + + public void test3() throws Exception { + check("foo.bar.baz", "foo/bar/baz"); + } + + public void test4() throws Exception { + check("foo/bar/baz", "foo/bar/baz"); + } +} diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmUnderscoreToCamelCaseMethodTest.java b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmUnderscoreToCamelCaseMethodTest.java new file mode 100644 index 0000000..4955dae --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.tests/unittests/com/android/ide/eclipse/adt/internal/wizards/templates/FmUnderscoreToCamelCaseMethodTest.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Eclipse Public License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.eclipse.org/org/documents/epl-v10.php + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.ide.eclipse.adt.internal.wizards.templates; + +import freemarker.template.SimpleScalar; +import freemarker.template.TemplateModelException; + +import java.util.Collections; +import java.util.List; + +import junit.framework.TestCase; + +@SuppressWarnings("javadoc") +public class FmUnderscoreToCamelCaseMethodTest extends TestCase { + @SuppressWarnings("rawtypes") + private void check(String s, String expected) throws TemplateModelException { + FmUnderscoreToCamelCaseMethod method = new FmUnderscoreToCamelCaseMethod(); + List list = Collections.singletonList(new SimpleScalar(s)); + assertEquals(expected, ((SimpleScalar) method.exec(list)).getAsString()); + } + + public void test1() throws Exception { + check("", ""); + } + + public void test2() throws Exception { + check("_", ""); + } + + public void test3() throws Exception { + check("foo", "Foo"); + } + + public void test4() throws Exception { + check("foo_bar", "FooBar"); + } + + public void test5() throws Exception { + check("foo__bar", "FooBar"); + } + + public void test6() throws Exception { + check("foo_", "Foo"); + } +} |