A placeholder module for archives, to support the CVS modules reorg
[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         protected PHPInterpreter selectedInterpreter;
33         protected PHPRuntime() {
34                 super();
35         }
36
37         public static PHPRuntime getDefault() {
38                 if (runtime == null) {
39                         runtime = new PHPRuntime();
40                 }
41                 return runtime;
42         }
43
44         public PHPInterpreter getSelectedInterpreter() {
45                 if (selectedInterpreter == null) {
46                         loadRuntimeConfiguration();
47                 }
48                 return selectedInterpreter;
49         }
50
51         public PHPInterpreter getInterpreter(String installLocation) {
52                 Iterator interpreters = getInstalledInterpreters().iterator();
53                 while(interpreters.hasNext()) {
54                         PHPInterpreter each = (PHPInterpreter) interpreters.next();
55                         if (each.getInstallLocation().toString().equals(installLocation))
56                                 return each;
57                 }
58
59                 return getSelectedInterpreter();
60         }
61
62         public void setSelectedInterpreter(PHPInterpreter anInterpreter) {
63                 selectedInterpreter = anInterpreter;
64                 saveRuntimeConfiguration();
65         }
66
67         public void addInstalledInterpreter(PHPInterpreter anInterpreter) {
68                 getInstalledInterpreters().add(anInterpreter);
69                 if (getInstalledInterpreters().size() == 1)
70                         setSelectedInterpreter((PHPInterpreter) getInstalledInterpreters().get(0));
71
72                 saveRuntimeConfiguration();
73         }
74
75         public List getInstalledInterpreters() {
76                 if (installedInterpreters == null)
77                         loadRuntimeConfiguration();
78                 return installedInterpreters;
79         }
80
81         public void setInstalledInterpreters(List newInstalledInterpreters) {
82                 installedInterpreters = newInstalledInterpreters;
83                 if (installedInterpreters.size() > 0)
84                         setSelectedInterpreter((PHPInterpreter)installedInterpreters.get(0));
85                 else
86                         setSelectedInterpreter(null);
87         }
88
89         protected void saveRuntimeConfiguration() {
90                 writeXML(getRuntimeConfigurationWriter());
91         }
92
93         protected Writer getRuntimeConfigurationWriter() {
94                 try {
95                         OutputStream stream = new BufferedOutputStream(new FileOutputStream(getRuntimeConfigurationFile()));
96                         return new OutputStreamWriter(stream);
97                 } catch (FileNotFoundException e) {}
98
99                 return null;
100         }
101
102         protected void loadRuntimeConfiguration() {
103                 installedInterpreters = new ArrayList();
104                 try {
105                         File file = getRuntimeConfigurationFile();
106                         if (file.exists()) {
107                             XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
108                             reader.setContentHandler(getRuntimeConfigurationContentHandler());
109                             reader.parse(new InputSource(getRuntimeConfigurationReader(file)));
110                         }
111                 } catch(Exception e) {
112                         PHPLaunchingPlugin.log(e);
113                 }
114         }
115
116         protected Reader getRuntimeConfigurationReader(File file) {
117                 try {
118                         return new FileReader(file);
119                 } catch(FileNotFoundException e) {}
120                 return new StringReader("");
121         }
122
123         protected void writeXML(Writer writer) {
124                 try {
125                         writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><runtimeconfig>");
126                         Iterator interpretersIterator = installedInterpreters.iterator();
127                         while (interpretersIterator.hasNext()) {
128                                 writer.write("<interpreter name=\"");
129
130                                 PHPInterpreter entry = (PHPInterpreter) interpretersIterator.next();
131 //                              writer.write(entry.getName());
132                                 writer.write("\" path=\"");
133                                 writer.write(entry.getInstallLocation().toString());
134                                 writer.write("\"");
135                                 if (entry.equals(selectedInterpreter))
136                                         writer.write(" selected=\"true\"");
137
138                                 writer.write("/>");
139                         }
140                         writer.write("</runtimeconfig>");
141                         writer.flush();
142                 } catch(IOException e) {
143                         PHPLaunchingPlugin.log(e);
144                 }
145         }
146
147         protected ContentHandler getRuntimeConfigurationContentHandler() {
148                 return new ContentHandler() {
149                         public void setDocumentLocator(Locator locator) {}
150                         public void startDocument() throws SAXException {}
151                         public void endDocument() throws SAXException {}
152                         public void startPrefixMapping(String prefix, String uri) throws SAXException {}
153                         public void endPrefixMapping(String prefix) throws SAXException {}
154                         public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
155                                 if ("interpreter".equals(qName)) {
156                                         String interpreterName = atts.getValue("name");
157                                         java.io.File installLocation;
158                                         if (interpreterName!=null) {
159                                           installLocation = new File(atts.getValue("path")+File.separatorChar+interpreterName);
160                                         } else {
161                                           installLocation = new File(atts.getValue("path"));
162                                         }
163                                         PHPInterpreter interpreter = new PHPInterpreter(installLocation);
164                                         installedInterpreters.add(interpreter);
165                                         if (atts.getValue("selected") != null)
166                                                 selectedInterpreter = interpreter;
167                                 }
168                         }
169                         public void endElement(String namespaceURI, String localName, String qName) throws SAXException {}
170                         public void characters(char[] ch, int start, int length) throws SAXException {}
171                         public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {}
172                         public void processingInstruction(String target, String data) throws SAXException {}
173                         public void skippedEntity(String name) throws SAXException {}
174                 };
175         }
176
177         protected File getRuntimeConfigurationFile() {
178                 IPath stateLocation = PHPLaunchingPlugin.getDefault().getStateLocation();
179                 IPath fileLocation = stateLocation.append("runtimeConfiguration.xml");
180                 return new File(fileLocation.toOSString());
181         }
182 }