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;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.Path;
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 name) {
52 Iterator interpreters = getInstalledInterpreters().iterator();
53 while(interpreters.hasNext()) {
54 PHPInterpreter each = (PHPInterpreter) interpreters.next();
55 if (each.getName().equals(name))
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 XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
106 reader.setContentHandler(getRuntimeConfigurationContentHandler());
107 reader.parse(new InputSource(getRuntimeConfigurationReader()));
108 } catch(Exception e) {
109 PHPLaunchingPlugin.getDefault().log(e);
113 protected Reader getRuntimeConfigurationReader() {
115 return new FileReader(getRuntimeConfigurationFile());
116 } catch(FileNotFoundException e) {}
117 return new StringReader("");
120 protected void writeXML(Writer writer) {
122 writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><runtimeconfig>");
123 Iterator interpretersIterator = installedInterpreters.iterator();
124 while (interpretersIterator.hasNext()) {
125 writer.write("<interpreter name=\"");
127 PHPInterpreter entry = (PHPInterpreter) interpretersIterator.next();
128 writer.write(entry.getName());
129 writer.write("\" path=\"");
130 writer.write(entry.getInstallLocation().toString());
132 if (entry.equals(selectedInterpreter))
133 writer.write(" selected=\"true\"");
137 writer.write("</runtimeconfig>");
139 } catch(IOException e) {
140 PHPLaunchingPlugin.log(e);
144 protected ContentHandler getRuntimeConfigurationContentHandler() {
145 return new ContentHandler() {
146 public void setDocumentLocator(Locator locator) {}
147 public void startDocument() throws SAXException {}
148 public void endDocument() throws SAXException {}
149 public void startPrefixMapping(String prefix, String uri) throws SAXException {}
150 public void endPrefixMapping(String prefix) throws SAXException {}
151 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
152 if ("interpreter".equals(qName)) {
153 String interpreterName = atts.getValue("name");
154 IPath installLocation = new Path(atts.getValue("path"));
155 PHPInterpreter interpreter = new PHPInterpreter(interpreterName, installLocation);
156 installedInterpreters.add(interpreter);
157 if (atts.getValue("selected") != null)
158 selectedInterpreter = interpreter;
161 public void endElement(String namespaceURI, String localName, String qName) throws SAXException {}
162 public void characters(char[] ch, int start, int length) throws SAXException {}
163 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {}
164 public void processingInstruction(String target, String data) throws SAXException {}
165 public void skippedEntity(String name) throws SAXException {}
169 protected File getRuntimeConfigurationFile() {
170 IPath stateLocation = PHPLaunchingPlugin.getDefault().getStateLocation();
171 IPath fileLocation = stateLocation.append("runtimeConfiguration.xml");
172 return new File(fileLocation.toOSString());