001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2003 Jonathan Halliday
008: * Copyright 2002 Elmar Grom
009: *
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: */
022:
023: package com.izforge.izpack.panels;
024:
025: import java.util.Iterator;
026: import java.util.Map;
027: import java.util.Vector;
028:
029: import net.n3.nanoxml.XMLElement;
030:
031: import com.izforge.izpack.installer.AutomatedInstallData;
032: import com.izforge.izpack.installer.PanelAutomation;
033: import com.izforge.izpack.util.Debug;
034:
035: /**
036: * Functions to support automated usage of the UserInputPanel
037: *
038: * @author Jonathan Halliday
039: * @author Elmar Grom
040: */
041: public class UserInputPanelAutomationHelper implements PanelAutomation {
042:
043: // ------------------------------------------------------
044: // automatic script section keys
045: // ------------------------------------------------------
046: private static final String AUTO_KEY_USER_INPUT = "userInput";
047:
048: private static final String AUTO_KEY_ENTRY = "entry";
049:
050: // ------------------------------------------------------
051: // automatic script keys attributes
052: // ------------------------------------------------------
053: private static final String AUTO_ATTRIBUTE_KEY = "key";
054:
055: private static final String AUTO_ATTRIBUTE_VALUE = "value";
056:
057: // ------------------------------------------------------
058: // String-String key-value pairs
059: // ------------------------------------------------------
060: private Map<String, String> entries;
061:
062: /**
063: * Default constructor, used during automated installation.
064: */
065: public UserInputPanelAutomationHelper() {
066: this .entries = null;
067: }
068:
069: /**
070: *
071: * @param entries String-String key-value pairs representing the state of the Panel
072: */
073: public UserInputPanelAutomationHelper(Map<String, String> entries) {
074: this .entries = entries;
075: }
076:
077: /**
078: * Serialize state to XML and insert under panelRoot.
079: *
080: * @param idata The installation data.
081: * @param panelRoot The XML root element of the panels blackbox tree.
082: */
083: public void makeXMLData(AutomatedInstallData idata,
084: XMLElement panelRoot) {
085: XMLElement userInput;
086: XMLElement dataElement;
087:
088: // ----------------------------------------------------
089: // add the item that combines all entries
090: // ----------------------------------------------------
091: userInput = new XMLElement(AUTO_KEY_USER_INPUT);
092: panelRoot.addChild(userInput);
093:
094: // ----------------------------------------------------
095: // add all entries
096: // ----------------------------------------------------
097: Iterator<String> keys = this .entries.keySet().iterator();
098: while (keys.hasNext()) {
099: String key = keys.next();
100: String value = this .entries.get(key);
101:
102: dataElement = new XMLElement(AUTO_KEY_ENTRY);
103: dataElement.setAttribute(AUTO_ATTRIBUTE_KEY, key);
104: dataElement.setAttribute(AUTO_ATTRIBUTE_VALUE, value);
105:
106: userInput.addChild(dataElement);
107: }
108: }
109:
110: /**
111: * Deserialize state from panelRoot and set idata variables accordingly.
112: *
113: * @param idata The installation data.
114: * @param panelRoot The XML root element of the panels blackbox tree.
115: *
116: * @return true if the variables were found and set.
117: */
118: public boolean runAutomated(AutomatedInstallData idata,
119: XMLElement panelRoot) {
120: XMLElement userInput;
121: XMLElement dataElement;
122: String variable;
123: String value;
124:
125: // ----------------------------------------------------
126: // get the section containing the user entries
127: // ----------------------------------------------------
128: userInput = panelRoot.getFirstChildNamed(AUTO_KEY_USER_INPUT);
129:
130: if (userInput == null) {
131: return false;
132: }
133:
134: Vector<XMLElement> userEntries = userInput
135: .getChildrenNamed(AUTO_KEY_ENTRY);
136:
137: if (userEntries == null) {
138: return false;
139: }
140:
141: // ----------------------------------------------------
142: // retieve each entry and substitute the associated
143: // variable
144: // ----------------------------------------------------
145: for (int i = 0; i < userEntries.size(); i++) {
146: dataElement = userEntries.elementAt(i);
147: variable = dataElement.getAttribute(AUTO_ATTRIBUTE_KEY);
148: value = dataElement.getAttribute(AUTO_ATTRIBUTE_VALUE);
149:
150: Debug.trace("UserInputPanel: setting variable " + variable
151: + " to " + value);
152: idata.setVariable(variable, value);
153: }
154:
155: return true;
156: }
157: }
|