Refine the code for PCD tools.
[mirror_edk2.git] / Tools / Source / PcdTools / org / tianocore / pcd / action / BuildAction.java
1 /** @file
2 BuildAction class.
3
4 BuildAction is the parent class for all action related to ant Task. This class will
5 define some common utility functionality, such as logMsg, warningMsg..etc.
6
7 Copyright (c) 2006, Intel Corporation
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17 package org.tianocore.pcd.action;
18
19 import org.apache.tools.ant.Task;
20 import org.apache.tools.ant.Project;
21 import org.tianocore.pcd.exception.BuildActionException;
22
23 /** BuildAction is the parent class for all action related to ant Task. This class will
24 define some common utility functionality, such as logMsg, warningMsg..etc.
25 **/
26 public abstract class BuildAction extends Task {
27 ///
28 /// Original message level before this action. This value will
29 /// be restored when quit this action.
30 ///
31 private int originalMessageLevel;
32
33 /**
34 checkParameter function check all parameter valid.
35
36 This function will be overrided by child class.
37 **/
38 public abstract void checkParameter() throws BuildActionException;
39
40 /**
41 performAction is to execute the detail action.
42
43 This function will be overrided by child class.
44 **/
45 public abstract void performAction() throws BuildActionException;
46
47 /**
48 setMessageLevel function set current message for task instance object.
49
50 The message should be restored when this action exit.
51
52 @param messageLevel The message level for this action.
53 **/
54 public void setMessageLevel(int messageLevel) {
55 originalMessageLevel = ActionMessage.messageLevel;
56 ActionMessage.messageLevel = messageLevel;
57 }
58
59 /**
60 logMsg function provide common log information functionality for all
61 PCD tool extends from ANT task class.
62
63 This function will use the log function in Ant task class.
64
65 @param action The class object who want log information.
66 @param logStr The string contains log information.
67 **/
68 public static void logMsg(Object action, String logStr) {
69 try {
70 ((Task) action).log(logStr, Project.MSG_INFO);
71 } catch (Exception exp) {
72 }
73 }
74
75 /**
76 warningMsg function provide common warning information functionality for all
77 PCD tool.
78
79 This function will dispatch message to special class such as BuildAction
80 Class, Entity Class etc.
81
82 @param action The class object who want warn information.
83 @param warningStr The string contains warning information.
84 **/
85 public static void warningMsg(Object action, String warningStr) {
86 ((Task) action).log(warningStr, Project.MSG_WARN);
87 }
88
89 /**
90 execute function is the main flow for all build action class.
91
92 This workflow will be:
93 1) Check paramet of this action.
94 2) Perform the child class action function.
95 3) Restore the message level.
96
97 @throws BuildActionException
98 **/
99 public void execute() throws BuildActionException {
100 checkParameter();
101 performAction();
102
103 //
104 // Restore orignal message level when exist the action.
105 //
106 ActionMessage.messageLevel = originalMessageLevel;
107 }
108 }