+++ /dev/null
-/** @file\r
- PropertyManager class.\r
- \r
- PropertyManager class wraps Project.setProperty and tracks overrided properties.\r
- \r
-Copyright (c) 2006, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution. The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-**/\r
-package org.tianocore.build.global;\r
-\r
-import java.util.HashMap;\r
-import java.util.Hashtable;\r
-import java.util.Iterator;\r
-import java.util.Set;\r
-import java.util.Stack;\r
-\r
-import org.apache.tools.ant.Project;\r
-import org.apache.tools.ant.PropertyHelper;\r
-\r
-/**\r
- PropertyManager uses a incremental way to to track overrided properties when \r
- setProperty. This is useful for property recovery in nestly calling build files.\r
- Another functionality of this class is to prevent warning message printed when\r
- building with "verbose" mode.\r
- **/\r
-public class PropertyManager {\r
- //\r
- // Property table stack, keeps track the history of properties changes\r
- // \r
- private static Stack<HashMap<String, String>> propertyTableStack = new Stack<HashMap<String, String>>();\r
- //\r
- // The very original properties\r
- // \r
- private static HashMap<String, String> orgPropertyTable = null;\r
- //\r
- // The last changes of properties\r
- // \r
- private static HashMap<String, String> oldPropertyTable = null;\r
- //\r
- // The current changes of properties\r
- // \r
- private static HashMap<String, String> bakPropertyTable = null;\r
- //\r
- // The Project of tracking properties\r
- // \r
- private static Project prj = null;\r
- //\r
- // PropertyHelper of this project for setting property quietly\r
- // \r
- private static PropertyHelper ph = null;\r
-\r
- /**\r
- Backup properties that have been overrided onto the stack for later recovery.\r
- **/\r
- public static void save() {\r
- //\r
- // If this is the first time to save properties changes, keep all properties\r
- // of this project as the original property table.\r
- // \r
- if (orgPropertyTable == null) {\r
- Hashtable prjProperties = prj.getProperties();\r
- orgPropertyTable = new HashMap<String, String>();\r
-\r
- Set keys = prjProperties.keySet();\r
- Iterator iter = keys.iterator();\r
- while (iter.hasNext()) {\r
- String item = (String)iter.next();\r
- orgPropertyTable.put(item, (String)prjProperties.get(item));\r
- }\r
- }\r
-\r
- //\r
- // If there're already overrided properties, push it onto stack; otherwise\r
- // prepare taking new overrided property by allocating space for it.\r
- // \r
- if (bakPropertyTable != null) {\r
- propertyTableStack.push(bakPropertyTable);\r
- oldPropertyTable = bakPropertyTable;\r
- } else {\r
- oldPropertyTable = orgPropertyTable;\r
- }\r
- bakPropertyTable = new HashMap<String, String>();\r
- }\r
-\r
- /**\r
- Restore the properties backup\r
- **/\r
- public static void restore() {\r
- if (bakPropertyTable == null) {\r
- //\r
- // No properties backup, do nothing \r
- // \r
- return;\r
- }\r
- Set keys = bakPropertyTable.keySet();\r
-\r
- //\r
- // Re-set properties in backup\r
- // \r
- Iterator iter = keys.iterator();\r
- while (iter.hasNext()) {\r
- String name = (String)iter.next();\r
- String value = (String)bakPropertyTable.get(name);\r
- ph.setProperty(null, name, value, false);\r
- }\r
-\r
- //\r
- // If there's backup history, get top one for next recovery\r
- // \r
- if (propertyTableStack.size() > 0) {\r
- bakPropertyTable = (HashMap<String, String>)propertyTableStack.pop();\r
- } else {\r
- bakPropertyTable = null; // no recovery any more\r
- }\r
-\r
- //\r
- // Determine last overrided properties for incremental judgement\r
- // \r
- if (propertyTableStack.size() == 0) {\r
- oldPropertyTable = orgPropertyTable;\r
- } else {\r
- oldPropertyTable = (HashMap<String, String>)propertyTableStack.peek();\r
- }\r
- }\r
-\r
- /**\r
- Set current Project for save() and restore() use.\r
-\r
- @param prj\r
- **/\r
- public static void setProject(Project prj) {\r
- PropertyManager.prj = prj;\r
- PropertyManager.ph = PropertyHelper.getPropertyHelper(prj);\r
- }\r
-\r
- /**\r
- Set a property for current project. It will also be put into property\r
- history record if the record table has been setup.\r
-\r
- @param name Property name\r
- @param value Property value\r
- **/\r
- public static void setProperty(String name, String value) {\r
- if (prj == null) {\r
- return;\r
- }\r
-\r
- setProperty(prj, name, value);\r
- }\r
-\r
- /**\r
- Set a property for current project. It will also be put into property\r
- history record if the record table has been setup.\r
-\r
- @param project The Project for which the property will be set\r
- @param name Property name\r
- @param value Property value\r
- **/\r
- public static void setProperty(Project project, String name, String value) {\r
- if (project == null) {\r
- if (prj == null) {\r
- return; // a Project must be given; otherwise nothing can be set\r
- }\r
- project = prj;\r
- }\r
-\r
- //\r
- // Using PropertyHelper to set a property can be quiet (no override\r
- // warning preset).\r
- // \r
- PropertyHelper.getPropertyHelper(project).setProperty(null, name, value, false);\r
-\r
- //\r
- // If no property override history record is found, do nothing further\r
- // \r
- if (oldPropertyTable == null || bakPropertyTable == null) {\r
- return;\r
- }\r
-\r
- //\r
- // Put a copy of given property in history record.\r
- // \r
- String oldValue = oldPropertyTable.get(name);\r
- if (oldValue == null) {\r
- oldValue = value;\r
- }\r
- bakPropertyTable.put(name, oldValue);\r
- }\r
-}\r
-\r