a8cf2e22d91d103caf193b9130b8fdf121df1407
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / internal / Configuration.java
1 /**********************************************************************
2  * Copyright (c) 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7   *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpeclipse.wiki.internal;
12
13 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
14
15 /**
16  *  
17  */
18 public class Configuration implements IConfiguration, Comparable {
19   private static final String MEMENTO_ID = "id";
20
21   private static final String MEMENTO_NAME = "name";
22
23   private static final String MEMENTO_USER = "user";
24
25   private static final String MEMENTO_URL = "url";
26
27   private static final String MEMENTO_PASSWORD = "password";
28
29   private static final String MEMENTO_TYPE_ID = "type-id";
30
31   protected String fId = "";
32
33   protected String fName = "";
34
35   protected String fUrl = "";
36
37   protected String fPassword = "";
38
39   protected String fUser = "";
40
41   protected String fType = "";
42
43   private static final char[] SCRAMBLING_TABLE=new char[] {
44         0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
45         16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
46         114,120,53,79,96,109,72,108,70,64,76,67,116,74,68,87,
47         111,52,75,119,49,34,82,81,95,65,112,86,118,110,122,105,
48         41,57,83,43,46,102,40,89,38,103,45,50,42,123,91,35,
49         125,55,54,66,124,126,59,47,92,71,115,78,88,107,106,56,
50         36,121,117,104,101,100,69,73,99,63,94,93,39,37,61,48,
51         58,113,32,90,44,98,60,51,33,97,62,77,84,80,85,223,
52         225,216,187,166,229,189,222,188,141,249,148,200,184,136,248,190,
53         199,170,181,204,138,232,218,183,255,234,220,247,213,203,226,193,
54         174,172,228,252,217,201,131,230,197,211,145,238,161,179,160,212,
55         207,221,254,173,202,146,224,151,140,196,205,130,135,133,143,246,
56         192,159,244,239,185,168,215,144,139,165,180,157,147,186,214,176,
57         227,231,219,169,175,156,206,198,129,164,150,210,154,177,134,127,
58         182,128,158,208,162,132,167,209,149,241,153,251,237,236,171,195,
59         243,233,253,240,194,250,191,155,142,137,245,235,163,242,178,152
60         };
61   public Configuration() {
62     this(WikiEditorPlugin.HTTP_QUERY); // default type
63   }
64
65   public Configuration(String type) {
66     this.fType = type;
67   }
68
69   public String getId() {
70     return fId;
71   }
72
73   public String getName() {
74     return fName;
75   }
76
77   public String getURL() {
78     return fUrl;
79   }
80
81   public String getPassword() {
82     return fPassword;
83   }
84
85   /*
86    * (non-Javadoc)
87    * 
88    * @see org.eclipse.monitor.internal.IConfiguration#getLocalPort()
89    */
90   public String getUser() {
91     return fUser;
92   }
93
94   /**
95    */
96   public String getType() {
97     return fType;
98   }
99
100   /*
101    * (non-Javadoc)
102    * 
103    * @see org.eclipse.monitor.internal.IConfiguration#isRunning()
104    */
105   public boolean isActive() {
106     return ConfigurationManager.getInstance().isActive(this);
107   }
108
109   public void delete() {
110     ConfigurationManager.getInstance().removeConfiguration(this);
111   }
112
113   public boolean isWorkingCopy() {
114     return false;
115   }
116
117   public IConfigurationWorkingCopy getWorkingCopy() {
118     return new ConfigurationWorkingCopy(this);
119   }
120
121   protected void setInternal(IConfiguration monitor) {
122     fId = monitor.getId();
123     fName = monitor.getName();
124     fUrl = monitor.getURL();
125     fPassword = monitor.getPassword();
126     fUser = monitor.getUser();
127     fType = monitor.getType();
128   }
129
130   protected void save(IMemento memento) {
131     memento.putString(MEMENTO_ID, fId);
132     memento.putString(MEMENTO_NAME, fName);
133     memento.putString(MEMENTO_TYPE_ID, fType);
134     memento.putString(MEMENTO_USER, fUser);
135     memento.putString(MEMENTO_URL, fUrl);
136     String result = 'A'+scramblePassword(fPassword);
137     memento.putString(MEMENTO_PASSWORD, result);
138   }
139
140   protected void load(IMemento memento) {
141     fId = memento.getString(MEMENTO_ID);
142     if (fId == null) {
143       fId = "";
144     }
145     fName = memento.getString(MEMENTO_NAME);
146     if (fName == null) {
147       fName = "";
148     }
149     fType = memento.getString(MEMENTO_TYPE_ID);
150     if (fType == null) {
151       fType = "";
152     }
153     fUser = memento.getString(MEMENTO_USER);
154     if (fUser == null) {
155       fUser = "";
156     }
157     fUrl = memento.getString(MEMENTO_URL);
158     if (fUrl == null) {
159       fUrl = "";
160     }
161     String result = memento.getString(MEMENTO_PASSWORD);
162     
163     if (result == null) {
164       fPassword = "";
165     } else {
166       fPassword = scramblePassword(result.substring(1));
167     }
168   }
169
170   /*
171    * (non-Javadoc)
172    * 
173    * @see java.lang.Object#toString()
174    */
175   public String toString() {
176     StringBuffer buffer = new StringBuffer();
177     buffer.append(fName);
178     buffer.append(" - ");
179     buffer.append(fUser);
180     buffer.append(" - ");
181     buffer.append(fUrl);
182     buffer.append(" - ");
183     buffer.append(fType);
184     return buffer.toString();
185   }
186
187   /*
188    * (non-Javadoc)
189    * 
190    * @see java.lang.Comparable#compareTo(java.lang.Object)
191    */
192   public int compareTo(Object o) {
193     if (o instanceof IConfiguration) {
194       return fName.compareTo(((IConfiguration) o).getName());
195     }
196     return 1;
197   }
198
199   private static String scramblePassword(String password)  {
200     int length = password.length();
201     char[] out = new char[length];
202     for (int i = 0; i < length; i++) {
203       char value = password.charAt(i);
204       out[i] = SCRAMBLING_TABLE[value];
205     }
206     return new String(out);
207   }
208
209 }