Added addLocationListener() to the PHPManualView.java file. This will catch the links...
[phpeclipse.git] / net.sourceforge.phpeclipse.launching / src / net / sourceforge / phpdt / internal / launching / PHPRuntime.java
1 package net.sourceforge.phpdt.internal.launching;
2
3 import java.io.BufferedOutputStream;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.FileReader;
8 import java.io.IOException;
9 import java.io.OutputStream;
10 import java.io.OutputStreamWriter;
11 import java.io.Reader;
12 import java.io.StringReader;
13 import java.io.Writer;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17
18 import javax.xml.parsers.SAXParserFactory;
19
20 import org.eclipse.core.runtime.IPath;
21 import org.xml.sax.Attributes;
22 import org.xml.sax.ContentHandler;
23 import org.xml.sax.InputSource;
24 import org.xml.sax.Locator;
25 import org.xml.sax.SAXException;
26 import org.xml.sax.XMLReader;
27
28 public class PHPRuntime {
29         protected static PHPRuntime runtime;
30
31         protected List installedInterpreters;
32
33         protected PHPInterpreter selectedInterpreter;
34
35         protected PHPRuntime() {
36                 super();
37         }
38
39         public static PHPRuntime getDefault() {
40                 if (runtime == null) {
41                         runtime = new PHPRuntime();
42                 }
43                 return runtime;
44         }
45
46         public PHPInterpreter getSelectedInterpreter() {
47                 if (selectedInterpreter == null) {
48                         loadRuntimeConfiguration();
49                 }
50                 return selectedInterpreter;
51         }
52
53         public PHPInterpreter getInterpreter(String installLocation) {
54                 Iterator interpreters = getInstalledInterpreters().iterator();
55                 while (interpreters.hasNext()) {
56                         PHPInterpreter each = (PHPInterpreter) interpreters.next();
57                         if (each.getInstallLocation().toString().equals(installLocation))
58                                 return each;
59                 }
60
61                 return getSelectedInterpreter();
62         }
63
64         public void setSelectedInterpreter(PHPInterpreter anInterpreter) {
65                 selectedInterpreter = anInterpreter;
66                 saveRuntimeConfiguration();
67         }
68
69         public void addInstalledInterpreter(PHPInterpreter anInterpreter) {
70                 getInstalledInterpreters().add(anInterpreter);
71                 if (getInstalledInterpreters().size() == 1)
72                         setSelectedInterpreter((PHPInterpreter) getInstalledInterpreters()
73                                         .get(0));
74
75                 saveRuntimeConfiguration();
76         }
77
78         public List getInstalledInterpreters() {
79                 if (installedInterpreters == null)
80                         loadRuntimeConfiguration();
81                 return installedInterpreters;
82         }
83
84         public void setInstalledInterpreters(List newInstalledInterpreters) {
85                 installedInterpreters = newInstalledInterpreters;
86                 if (installedInterpreters.size() > 0)
87                         setSelectedInterpreter((PHPInterpreter) installedInterpreters
88                                         .get(0));
89                 else
90                         setSelectedInterpreter(null);
91         }
92
93         protected void saveRuntimeConfiguration() {
94                 writeXML(getRuntimeConfigurationWriter());
95         }
96
97         protected Writer getRuntimeConfigurationWriter() {
98                 try {
99                         OutputStream stream = new BufferedOutputStream(
100                                         new FileOutputStream(getRuntimeConfigurationFile()));
101                         return new OutputStreamWriter(stream);
102                 } catch (FileNotFoundException e) {
103                 }
104
105                 return null;
106         }
107
108         protected void loadRuntimeConfiguration() {
109                 installedInterpreters = new ArrayList();
110                 try {
111                         File file = getRuntimeConfigurationFile();
112                         if (file.exists()) {
113                                 XMLReader reader = SAXParserFactory.newInstance()
114                                                 .newSAXParser().getXMLReader();
115                                 reader
116                                                 .setContentHandler(getRuntimeConfigurationContentHandler());
117                                 reader.parse(new InputSource(
118                                                 getRuntimeConfigurationReader(file)));
119                         }
120                 } catch (Exception e) {
121                         PHPLaunchingPlugin.log(e);
122                 }
123         }
124
125         protected Reader getRuntimeConfigurationReader(File file) {
126                 try {
127                         return new FileReader(file);
128                 } catch (FileNotFoundException e) {
129                 }
130                 return new StringReader("");
131         }
132
133         protected void writeXML(Writer writer) {
134                 try {
135                         writer
136                                         .write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><runtimeconfig>");
137                         Iterator interpretersIterator = installedInterpreters.iterator();
138                         while (interpretersIterator.hasNext()) {
139                                 writer.write("<interpreter name=\"");
140
141                                 PHPInterpreter entry = (PHPInterpreter) interpretersIterator
142                                                 .next();
143                                 // writer.write(entry.getName());
144                                 writer.write("\" path=\"");
145                                 writer.write(entry.getInstallLocation().toString());
146                                 writer.write("\"");
147                                 if (entry.equals(selectedInterpreter))
148                                         writer.write(" selected=\"true\"");
149
150                                 writer.write("/>");
151                         }
152                         writer.write("</runtimeconfig>");
153                         writer.flush();
154                 } catch (IOException e) {
155                         PHPLaunchingPlugin.log(e);
156                 }
157         }
158
159         protected ContentHandler getRuntimeConfigurationContentHandler() {
160                 return new ContentHandler() {
161                         public void setDocumentLocator(Locator locator) {
162                         }
163
164                         public void startDocument() throws SAXException {
165                         }
166
167                         public void endDocument() throws SAXException {
168                         }
169
170                         public void startPrefixMapping(String prefix, String uri)
171                                         throws SAXException {
172                         }
173
174                         public void endPrefixMapping(String prefix) throws SAXException {
175                         }
176
177                         public void startElement(String namespaceURI, String localName,
178                                         String qName, Attributes atts) throws SAXException {
179                                 if ("interpreter".equals(qName)) {
180                                         String interpreterName = atts.getValue("name");
181                                         java.io.File installLocation;
182                                         if (interpreterName != null) {
183                                                 installLocation = new File(atts.getValue("path")
184                                                                 + File.separatorChar + interpreterName);
185                                         } else {
186                                                 installLocation = new File(atts.getValue("path"));
187                                         }
188                                         PHPInterpreter interpreter = new PHPInterpreter(
189                                                         installLocation);
190                                         installedInterpreters.add(interpreter);
191                                         if (atts.getValue("selected") != null)
192                                                 selectedInterpreter = interpreter;
193                                 }
194                         }
195
196                         public void endElement(String namespaceURI, String localName,
197                                         String qName) throws SAXException {
198                         }
199
200                         public void characters(char[] ch, int start, int length)
201                                         throws SAXException {
202                         }
203
204                         public void ignorableWhitespace(char[] ch, int start, int length)
205                                         throws SAXException {
206                         }
207
208                         public void processingInstruction(String target, String data)
209                                         throws SAXException {
210                         }
211
212                         public void skippedEntity(String name) throws SAXException {
213                         }
214                 };
215         }
216
217         protected File getRuntimeConfigurationFile() {
218                 IPath stateLocation = PHPLaunchingPlugin.getDefault()
219                                 .getStateLocation();
220                 IPath fileLocation = stateLocation.append("runtimeConfiguration.xml");
221                 return new File(fileLocation.toOSString());
222         }
223 }