1 package net.sourceforge.phpdt.internal.launching;
3 import java.io.BufferedOutputStream;
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;
18 import javax.xml.parsers.SAXParserFactory;
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;
28 public class PHPRuntime {
29 protected static PHPRuntime runtime;
31 protected List installedInterpreters;
32 protected PHPInterpreter selectedInterpreter;
33 protected PHPRuntime() {
37 public static PHPRuntime getDefault() {
38 if (runtime == null) {
39 runtime = new PHPRuntime();
44 public PHPInterpreter getSelectedInterpreter() {
45 if (selectedInterpreter == null) {
46 loadRuntimeConfiguration();
48 return selectedInterpreter;
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))
59 return getSelectedInterpreter();
62 public void setSelectedInterpreter(PHPInterpreter anInterpreter) {
63 selectedInterpreter = anInterpreter;
64 saveRuntimeConfiguration();
67 public void addInstalledInterpreter(PHPInterpreter anInterpreter) {
68 getInstalledInterpreters().add(anInterpreter);
69 if (getInstalledInterpreters().size() == 1)
70 setSelectedInterpreter((PHPInterpreter) getInstalledInterpreters().get(0));
72 saveRuntimeConfiguration();
75 public List getInstalledInterpreters() {
76 if (installedInterpreters == null)
77 loadRuntimeConfiguration();
78 return installedInterpreters;
81 public void setInstalledInterpreters(List newInstalledInterpreters) {
82 installedInterpreters = newInstalledInterpreters;
83 if (installedInterpreters.size() > 0)
84 setSelectedInterpreter((PHPInterpreter)installedInterpreters.get(0));
86 setSelectedInterpreter(null);
89 protected void saveRuntimeConfiguration() {
90 writeXML(getRuntimeConfigurationWriter());
93 protected Writer getRuntimeConfigurationWriter() {
95 OutputStream stream = new BufferedOutputStream(new FileOutputStream(getRuntimeConfigurationFile()));
96 return new OutputStreamWriter(stream);
97 } catch (FileNotFoundException e) {}
102 protected void loadRuntimeConfiguration() {
103 installedInterpreters = new ArrayList();
105 File file = getRuntimeConfigurationFile();
107 XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
108 reader.setContentHandler(getRuntimeConfigurationContentHandler());
109 reader.parse(new InputSource(getRuntimeConfigurationReader(file)));
111 } catch(Exception e) {
112 PHPLaunchingPlugin.log(e);
116 protected Reader getRuntimeConfigurationReader(File file) {
118 return new FileReader(file);
119 } catch(FileNotFoundException e) {}
120 return new StringReader("");
123 protected void writeXML(Writer writer) {
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=\"");
130 PHPInterpreter entry = (PHPInterpreter) interpretersIterator.next();
131 // writer.write(entry.getName());
132 writer.write("\" path=\"");
133 writer.write(entry.getInstallLocation().toString());
135 if (entry.equals(selectedInterpreter))
136 writer.write(" selected=\"true\"");
140 writer.write("</runtimeconfig>");
142 } catch(IOException e) {
143 PHPLaunchingPlugin.log(e);
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);
161 installLocation = new File(atts.getValue("path"));
163 PHPInterpreter interpreter = new PHPInterpreter(installLocation);
164 installedInterpreters.add(interpreter);
165 if (atts.getValue("selected") != null)
166 selectedInterpreter = interpreter;
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 {}
177 protected File getRuntimeConfigurationFile() {
178 IPath stateLocation = PHPLaunchingPlugin.getDefault().getStateLocation();
179 IPath fileLocation = stateLocation.append("runtimeConfiguration.xml");
180 return new File(fileLocation.toOSString());