add XmlProperty task
Project: http://git-wip-us.apache.org/repos/asf/flex-utilities/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-utilities/commit/b5b21e76 Tree: http://git-wip-us.apache.org/repos/asf/flex-utilities/tree/b5b21e76 Diff: http://git-wip-us.apache.org/repos/asf/flex-utilities/diff/b5b21e76 Branch: refs/heads/develop Commit: b5b21e76b3448e386b98659cbafb28065a64842d Parents: f73d869 Author: Alex Harui <[email protected]> Authored: Wed Dec 11 21:07:22 2013 -0800 Committer: Alex Harui <[email protected]> Committed: Wed Dec 11 21:07:22 2013 -0800 ---------------------------------------------------------------------- ant_on_air/src/AntClasses.as | 1 + .../src/org/apache/flex/ant/tags/XmlProperty.as | 113 +++++++++++++++++++ ant_on_air/tests/test.xml | 22 ++++ ant_on_air/tests/test_data.xml | 42 +++++++ 4 files changed, 178 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-utilities/blob/b5b21e76/ant_on_air/src/AntClasses.as ---------------------------------------------------------------------- diff --git a/ant_on_air/src/AntClasses.as b/ant_on_air/src/AntClasses.as index 12e0c98..ab6b80b 100644 --- a/ant_on_air/src/AntClasses.as +++ b/ant_on_air/src/AntClasses.as @@ -47,6 +47,7 @@ package import org.apache.flex.ant.tags.Replace; Replace; import org.apache.flex.ant.tags.Untar; Untar; import org.apache.flex.ant.tags.Unzip; Unzip; + import org.apache.flex.ant.tags.XmlProperty; XmlProperty; } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/flex-utilities/blob/b5b21e76/ant_on_air/src/org/apache/flex/ant/tags/XmlProperty.as ---------------------------------------------------------------------- diff --git a/ant_on_air/src/org/apache/flex/ant/tags/XmlProperty.as b/ant_on_air/src/org/apache/flex/ant/tags/XmlProperty.as new file mode 100644 index 0000000..f5f126d --- /dev/null +++ b/ant_on_air/src/org/apache/flex/ant/tags/XmlProperty.as @@ -0,0 +1,113 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 +// +// 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 org.apache.flex.ant.tags +{ + import flash.filesystem.File; + import flash.filesystem.FileMode; + import flash.filesystem.FileStream; + + import mx.core.IFlexModuleFactory; + + import org.apache.flex.ant.Ant; + import org.apache.flex.ant.tags.supportClasses.TaskHandler; + import org.apache.flex.xml.XMLTagProcessor; + + [Mixin] + public class XmlProperty extends TaskHandler + { + public static function init(mf:IFlexModuleFactory):void + { + Ant.antTagProcessors["xmlproperty"] = XmlProperty; + } + + public function XmlProperty() + { + } + + override public function init(xml:XML, context:Object, xmlProcessor:XMLTagProcessor):void + { + super.init(xml, context, xmlProcessor); + } + + override public function execute(callbackMode:Boolean):Boolean + { + var f:File = new File(fileName); + var fs:FileStream = new FileStream(); + fs.open(f, FileMode.READ); + var data:String = fs.readUTFBytes(fs.bytesAvailable); + var xml:XML = XML(data); + createProperties(xml, xml.name()); + fs.close(); + return true; + } + + private function createProperties(xml:XML, prefix:String):void + { + var children:XMLList = xml.*; + for each (var node:XML in children) + { + var val:String; + var key:String; + if (node.nodeKind() == "text") + { + key = prefix; + val = node.toString(); + if (!context.hasOwnProperty(key)) + context[key] = val; + } + else if (node.nodeKind() == "element") + { + key = prefix + "." + node.name(); + createProperties(node, key); + } + } + if (collapse) + { + var attrs:XMLList = xml.attributes(); + var n:int = attrs.length(); + for (var i:int = 0; i < n; i++) + { + key = prefix + "." + attrs[i].name(); + val = xml.attribute(attrs[i].name()).toString(); + if (!context.hasOwnProperty(key)) + context[key] = val; + } + } + } + + private var fileName:String; + private var collapse:Boolean; + + override protected function processAttribute(name:String, value:String):void + { + if (name == "file") + { + fileName = value; + } + else if (name == "collapseAttributes") + { + collapse = value == "true"; + } + else + super.processAttribute(name, value); + } + + + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/flex-utilities/blob/b5b21e76/ant_on_air/tests/test.xml ---------------------------------------------------------------------- diff --git a/ant_on_air/tests/test.xml b/ant_on_air/tests/test.xml index 413f911..edab88c 100644 --- a/ant_on_air/tests/test.xml +++ b/ant_on_air/tests/test.xml @@ -186,6 +186,28 @@ </condition> </fail> <echo>Project.as ${project.doesnt.exist.after.delete}. Should NOT say: didn't get deleted</echo> + <xmlproperty file="${basedir}/test_data.xml" collapseAttributes="true" /> + <fail message="xmlproperty did not work set property"> + <condition> + <not> + <isset property="project.property.environment" /> + </not> + </condition> + </fail> + <fail message="xmlproperty did not work set property value"> + <condition> + <not> + <equals arg1="${project.property.environment}" arg2="env" /> + </not> + </condition> + </fail> + <fail message="xmlproperty did not work for text node"> + <condition> + <not> + <equals arg1="${project.target.echo}" arg2="FLEX_HOME" /> + </not> + </condition> + </fail> </target> <target name="build" depends="init,compile" http://git-wip-us.apache.org/repos/asf/flex-utilities/blob/b5b21e76/ant_on_air/tests/test_data.xml ---------------------------------------------------------------------- diff --git a/ant_on_air/tests/test_data.xml b/ant_on_air/tests/test_data.xml new file mode 100644 index 0000000..df07180 --- /dev/null +++ b/ant_on_air/tests/test_data.xml @@ -0,0 +1,42 @@ +<?xml version="1.0"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 + + 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. + +--> + +<project name="ant_on_air_data" default="build"> + + <!--load environment variables prefixed with env --> + <property environment="env"/> + + <!-- + Properties are immutable so value frozen first time property is set. + If FLEX_HOME is not set with -DFLEX_HOME=/path/to/flex/sdk on the ant command line + use the environment variable, if it exists. Else if windows, use FLEX_HOME_WIN + else use FLEX_HOME_MAC, which are both defined in build.properties. + --> + <condition property="FLEX_HOME" value="${env.FLEX_HOME}"> + <isset property="env.FLEX_HOME" /> + </condition> + + + <target name="test"> + <echo>FLEX_HOME</echo> + <mkdir dir="${basedir}/temp" /> + </target> + +</project>
