1 package net.sourceforge.phpeclipse.resourcesview;
3 import java.io.ByteArrayInputStream;
4 import java.io.InputStream;
5 import java.util.ArrayList;
6 import java.util.Iterator;
9 import javax.xml.parsers.SAXParserFactory;
11 import org.eclipse.core.resources.IFile;
12 import org.eclipse.core.resources.IProject;
13 import org.eclipse.core.resources.IProjectNature;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.core.runtime.Status;
20 import net.sourceforge.phpeclipse.LoadPathEntry;
21 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23 import org.xml.sax.Attributes;
24 import org.xml.sax.ContentHandler;
25 import org.xml.sax.InputSource;
26 import org.xml.sax.Locator;
27 import org.xml.sax.SAXException;
28 import org.xml.sax.XMLReader;
30 public class PHPProject implements IProjectNature, PHPElement {
31 protected IProject project;
32 protected List loadPathEntries;
33 protected boolean scratched;
35 public PHPProject() {}
37 public void configure() throws CoreException {}
39 public void deconfigure() throws CoreException {}
41 public IProject getProject() {
45 protected IProject getProject(String name) {
46 return PHPeclipsePlugin.getWorkspace().getRoot().getProject(name);
49 public void setProject(IProject aProject) {
53 public void addLoadPathEntry(IProject anotherPHPProject) {
56 LoadPathEntry newEntry = new LoadPathEntry(anotherPHPProject);
57 getLoadPathEntries().add(newEntry);
60 public void removeLoadPathEntry(IProject anotherPHPProject) {
61 Iterator entries = getLoadPathEntries().iterator();
62 while (entries.hasNext()) {
63 LoadPathEntry entry = (LoadPathEntry) entries.next();
64 if (entry.getType() == LoadPathEntry.TYPE_PROJECT && entry.getProject().getName().equals(anotherPHPProject.getName())) {
65 getLoadPathEntries().remove(entry);
72 public List getLoadPathEntries() {
73 if (loadPathEntries == null) {
74 loadLoadPathEntries();
77 return loadPathEntries;
80 public List getReferencedProjects() {
81 List referencedProjects = new ArrayList();
83 Iterator iterator = getLoadPathEntries().iterator();
84 while (iterator.hasNext()) {
85 LoadPathEntry pathEntry = (LoadPathEntry) iterator.next();
86 if (pathEntry.getType() == LoadPathEntry.TYPE_PROJECT)
87 referencedProjects.add(pathEntry.getProject());
90 return referencedProjects;
93 protected void loadLoadPathEntries() {
94 loadPathEntries = new ArrayList();
96 IFile loadPathsFile = getLoadPathEntriesFile();
98 XMLReader reader = null;
100 reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
101 reader.setContentHandler(getLoadPathEntriesContentHandler());
102 reader.parse(new InputSource(loadPathsFile.getContents()));
103 } catch (Exception e) {
104 //the file is nonextant or unreadable
108 protected ContentHandler getLoadPathEntriesContentHandler() {
109 return new ContentHandler() {
110 public void characters(char[] arg0, int arg1, int arg2) throws SAXException {}
112 public void endDocument() throws SAXException {}
114 public void endElement(String arg0, String arg1, String arg2) throws SAXException {}
116 public void endPrefixMapping(String arg0) throws SAXException {}
118 public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {}
120 public void processingInstruction(String arg0, String arg1) throws SAXException {}
122 public void setDocumentLocator(Locator arg0) {}
124 public void skippedEntity(String arg0) throws SAXException {}
126 public void startDocument() throws SAXException {}
128 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
129 if ("pathentry".equals(qName))
130 if ("project".equals(atts.getValue("type"))) {
131 IPath referencedProjectPath = new Path(atts.getValue("path"));
132 IProject referencedProject = getProject(referencedProjectPath.lastSegment());
133 loadPathEntries.add(new LoadPathEntry(referencedProject));
137 public void startPrefixMapping(String arg0, String arg1) throws SAXException {}
141 protected IFile getLoadPathEntriesFile() {
142 return project.getFile(".loadpath");
145 public void save() throws CoreException {
147 InputStream xmlPath = new ByteArrayInputStream(getLoadPathXML().getBytes());
148 IFile loadPathsFile = getLoadPathEntriesFile();
149 if (!loadPathsFile.exists())
150 loadPathsFile.create(xmlPath, true, null);
152 loadPathsFile.setContents(xmlPath, true, false, null);
158 protected String getLoadPathXML() {
159 StringBuffer buffer = new StringBuffer();
160 buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><loadpath>");
162 Iterator pathEntriesIterator = loadPathEntries.iterator();
164 while (pathEntriesIterator.hasNext()) {
165 LoadPathEntry entry = (LoadPathEntry) pathEntriesIterator.next();
166 buffer.append(entry.toXML());
169 buffer.append("</loadpath>");
170 return buffer.toString();
172 public IResource getUnderlyingResource() {