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