001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.makeproject.runprofiles;
043:
044: import org.netbeans.modules.cnd.makeproject.api.runprofiles.Env;
045: import org.netbeans.modules.cnd.makeproject.api.runprofiles.RunProfile;
046: import org.netbeans.modules.cnd.api.xml.AttrValuePair;
047: import org.netbeans.modules.cnd.api.xml.VersionException;
048: import org.netbeans.modules.cnd.api.xml.XMLDecoder;
049: import org.netbeans.modules.cnd.api.xml.XMLEncoder;
050: import org.netbeans.modules.cnd.api.xml.XMLEncoderStream;
051: import org.netbeans.modules.cnd.makeproject.api.configurations.IntConfiguration;
052: import org.xml.sax.Attributes;
053:
054: public class RunProfileXMLCodec extends XMLDecoder implements
055: XMLEncoder {
056:
057: private RunProfile profile;
058:
059: // was: public RunProfile.PROFILE_ID
060: private static final String PROFILE_ID = "runprofile"; // NOI18N
061:
062: private final static String VARIABLE_ELEMENT = "variable"; // NOI18N
063: private final static String NAME_ATTR = "name"; // NOI18N
064: private final static String VALUE_ATTR = "value"; // NOI18N
065: private final static String ENVIRONMENT_ELEMENT = "environment"; // NOI18N
066: private final static String ARGS_ELEMENT = "args"; // NOI18N
067: private final static String RUNDIR_ELEMENT = "rundir"; // NOI18N
068: private final static String BUILD_FIRST_ELEMENT = "buildfirst"; // NOI18N
069: private final static String CONSOLE_TYPE_ELEMENT = "console-type"; // NOI18N
070: private final static String TERMINAL_TYPE_ELEMENT = "terminal-type"; // NOI18N
071:
072: public final static String TRUE_VALUE = "true"; // NOI18N
073: public final static String FALSE_VALUE = "false"; // NOI18N
074:
075: private final static int this version = 5;
076:
077: public RunProfileXMLCodec(RunProfile profile) {
078: this .profile = profile;
079: }
080:
081: public static int getVersion() {
082: return this version;
083: }
084:
085: // interface XMLDecoder
086: public String tag() {
087: return PROFILE_ID;
088: }
089:
090: // interface XMLDecoder
091: public void start(Attributes atts) throws VersionException {
092: String what = "run profile"; // NOI18N
093: int maxVersion = getVersion();
094: checkVersion(atts, what, maxVersion);
095: }
096:
097: // interface XMLDecoder
098: public void end() {
099: profile.clearChanged();
100: }
101:
102: // interface XMLDecoder
103: public void startElement(String element, Attributes atts) {
104: if (element.equals(VARIABLE_ELEMENT)) {
105: profile.getEnvironment().putenv(atts.getValue(0),
106: atts.getValue(1));
107: }
108: }
109:
110: // interface XMLDecoder
111: public void endElement(String element, String currentText) {
112: if (element.equals(ARGS_ELEMENT)) {
113: profile.setArgs(currentText);
114: } else if (element.equals(RUNDIR_ELEMENT)) {
115: profile.setRunDir(currentText);
116: } else if (element.equals(BUILD_FIRST_ELEMENT)) {
117: profile.setBuildFirst(currentText.equals(TRUE_VALUE));
118: } else {
119: int idx;
120: try {
121: idx = Integer.parseInt(currentText);
122: } catch (NumberFormatException ex) {
123: idx = 0;
124: }
125: if (element.equals(CONSOLE_TYPE_ELEMENT)) {
126: profile.getConsoleType().setValue(idx);
127: } else if (element.equals(TERMINAL_TYPE_ELEMENT)) {
128: profile.getTerminalType().setValue(idx);
129: }
130: }
131: }
132:
133: /*
134: * was: part of RunProfileHelper.java.writeEnvironmentBlock
135: */
136:
137: private static void encode(XMLEncoderStream xes, String[] pair) {
138: xes.element(VARIABLE_ELEMENT, new AttrValuePair[] {
139: new AttrValuePair(NAME_ATTR, "" + pair[0]), // NOI18N
140: new AttrValuePair(VALUE_ATTR, "" + pair[1]) // NOI18N
141: });
142: }
143:
144: /*
145: * was: RunProfileHelper.java.writeEnvironmentBlock
146: */
147:
148: private static void encode(XMLEncoderStream xes, Env env) {
149: String[][] environment = env.getenvAsPairs();
150: xes.elementOpen(ENVIRONMENT_ELEMENT);
151: for (int i = 0; i < environment.length; i++) {
152: encode(xes, environment[i]);
153: }
154: xes.elementClose(ENVIRONMENT_ELEMENT);
155: }
156:
157: /*
158: * was: RunProfileHelper.java.writeProfileBlock
159: */
160:
161: private static void encode(XMLEncoderStream xes, RunProfile profile) {
162: xes.elementOpen(PROFILE_ID, getVersion());
163: xes.element(ARGS_ELEMENT, profile.getArgsFlat());
164: xes.element(RUNDIR_ELEMENT, profile.getRunDir());
165: xes.element(BUILD_FIRST_ELEMENT, "" + profile.getBuildFirst()); // NOI18N
166: xes.element(CONSOLE_TYPE_ELEMENT, Integer.toString(profile
167: .getConsoleType().getValue()));
168: xes.element(TERMINAL_TYPE_ELEMENT, Integer.toString(profile
169: .getTerminalType().getValue()));
170: encode(xes, profile.getEnvironment());
171: xes.elementClose(PROFILE_ID);
172: }
173:
174: // interface XMLEncoder
175: public void encode(XMLEncoderStream xes) {
176: encode(xes, profile);
177: }
178: }
|