001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.target;
011:
012: import java.io.PrintWriter;
013:
014: import org.eclipse.core.runtime.Platform;
015: import org.eclipse.pde.internal.core.itarget.IEnvironmentInfo;
016: import org.eclipse.pde.internal.core.itarget.ITargetModel;
017: import org.w3c.dom.Node;
018: import org.w3c.dom.NodeList;
019:
020: public class EnvironmentInfo extends TargetObject implements
021: IEnvironmentInfo {
022:
023: private static final long serialVersionUID = 1L;
024:
025: private String fOS;
026: private String fWS;
027: private String fArch;
028: private String fNL;
029:
030: public EnvironmentInfo(ITargetModel model) {
031: super (model);
032: }
033:
034: public String getOS() {
035: return fOS;
036: }
037:
038: public String getWS() {
039: return fWS;
040: }
041:
042: public String getArch() {
043: return fArch;
044: }
045:
046: public String getNL() {
047: return fNL;
048: }
049:
050: public void setOS(String os) {
051: String oldValue = fOS;
052: fOS = os;
053: firePropertyChanged(P_OS, oldValue, fOS);
054: }
055:
056: public void setWS(String ws) {
057: String oldValue = fWS;
058: fWS = ws;
059: firePropertyChanged(P_WS, oldValue, fWS);
060: }
061:
062: public void setArch(String arch) {
063: String oldValue = fArch;
064: fArch = arch;
065: firePropertyChanged(P_ARCH, oldValue, fArch);
066: }
067:
068: public void setNL(String nl) {
069: String oldValue = fNL;
070: fNL = nl;
071: firePropertyChanged(P_NL, oldValue, fNL);
072: }
073:
074: public void parse(Node node) {
075: NodeList list = node.getChildNodes();
076: for (int i = 0; i < list.getLength(); i++) {
077: Node child = list.item(i);
078: if (child.getNodeType() == Node.ELEMENT_NODE) {
079: if (child.getNodeName().equals(P_OS)) {
080: fOS = getText(child);
081: } else if (child.getNodeName().equals(P_WS)) {
082: fWS = getText(child);
083: } else if (child.getNodeName().equals(P_ARCH)) {
084: fArch = getText(child);
085: } else if (child.getNodeName().equals(P_NL)) {
086: fNL = getText(child);
087: }
088: }
089: }
090: }
091:
092: private String getText(Node node) {
093: node.normalize();
094: Node text = node.getFirstChild();
095: if (text != null && text.getNodeType() == Node.TEXT_NODE) {
096: return text.getNodeValue();
097: }
098: return ""; //$NON-NLS-1$
099: }
100:
101: public void write(String indent, PrintWriter writer) {
102: // if no elements set, then don't write anything
103: if ((fOS == null || fOS.length() == 0)
104: && (fWS == null || fWS.length() == 0)
105: && (fArch == null || fArch.length() == 0)
106: && (fNL == null || fNL.length() == 0))
107: return;
108: writer.println();
109: writer.println(indent + "<environment>"); //$NON-NLS-1$
110: if (fOS != null && fOS.length() > 0)
111: writer
112: .println(indent
113: + " <" + P_OS + ">" + getWritableString(fOS) + "</" + P_OS + ">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
114: if (fWS != null && fWS.length() > 0)
115: writer
116: .println(indent
117: + " <" + P_WS + ">" + getWritableString(fWS) + "</" + P_WS + ">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
118: if (fArch != null && fArch.length() > 0)
119: writer
120: .println(indent
121: + " <" + P_ARCH + ">" + getWritableString(fArch) + "</" + P_ARCH + ">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
122: if (fNL != null && fNL.length() > 0)
123: writer
124: .println(indent
125: + " <" + P_NL + ">" + getWritableString(fNL) + "</" + P_NL + ">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
126: writer.println(indent + "</environment>"); //$NON-NLS-1$
127: }
128:
129: public String getDisplayOS() {
130: if (fOS == null || fOS.trim().length() == 0)
131: return Platform.getOS();
132: return fOS.trim();
133: }
134:
135: public String getDisplayWS() {
136: if (fWS == null || fWS.trim().length() == 0)
137: return Platform.getWS();
138: return fWS.trim();
139: }
140:
141: public String getDisplayArch() {
142: if (fArch == null || fArch.trim().length() == 0)
143: return Platform.getOSArch();
144: return fArch.trim();
145: }
146:
147: public String getDisplayNL() {
148: if (fNL == null || fNL.trim().length() == 0)
149: return Platform.getNL();
150: return fNL.trim();
151: }
152:
153: }
|