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 net.sourceforge.phpeclipse.LoadPathEntry;
12 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IProjectNature;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.CoreException;
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 PHPProject implements IProjectNature, PHPElement {
29 protected IProject project;
30 protected List loadPathEntries;
31 protected boolean scratched;
33 public PHPProject() {}
35 public void configure() throws CoreException {}
37 public void deconfigure() throws CoreException {}
39 public IProject getProject() {
43 protected IProject getProject(String name) {
44 return PHPeclipsePlugin.getWorkspace().getRoot().getProject(name);
47 public void setProject(IProject aProject) {
51 public void addLoadPathEntry(IProject anotherPHPProject) {
54 LoadPathEntry newEntry = new LoadPathEntry(anotherPHPProject);
55 getLoadPathEntries().add(newEntry);
58 public void removeLoadPathEntry(IProject anotherPHPProject) {
59 Iterator entries = getLoadPathEntries().iterator();
60 while (entries.hasNext()) {
61 LoadPathEntry entry = (LoadPathEntry) entries.next();
62 if (entry.getType() == LoadPathEntry.TYPE_PROJECT && entry.getProject().getName().equals(anotherPHPProject.getName())) {
63 getLoadPathEntries().remove(entry);
70 public List getLoadPathEntries() {
71 if (loadPathEntries == null) {
72 loadLoadPathEntries();
75 return loadPathEntries;
78 public List getReferencedProjects() {
79 List referencedProjects = new ArrayList();
81 Iterator iterator = getLoadPathEntries().iterator();
82 while (iterator.hasNext()) {
83 LoadPathEntry pathEntry = (LoadPathEntry) iterator.next();
84 if (pathEntry.getType() == LoadPathEntry.TYPE_PROJECT)
85 referencedProjects.add(pathEntry.getProject());
88 return referencedProjects;
91 protected void loadLoadPathEntries() {
92 loadPathEntries = new ArrayList();
94 IFile loadPathsFile = getLoadPathEntriesFile();
96 XMLReader reader = null;
98 reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
99 reader.setContentHandler(getLoadPathEntriesContentHandler());
100 reader.parse(new InputSource(loadPathsFile.getContents()));
101 } catch (Exception e) {
102 //the file is nonextant or unreadable
106 protected ContentHandler getLoadPathEntriesContentHandler() {
107 return new ContentHandler() {
108 public void characters(char[] arg0, int arg1, int arg2) throws SAXException {}
110 public void endDocument() throws SAXException {}
112 public void endElement(String arg0, String arg1, String arg2) throws SAXException {}
114 public void endPrefixMapping(String arg0) throws SAXException {}
116 public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {}
118 public void processingInstruction(String arg0, String arg1) throws SAXException {}
120 public void setDocumentLocator(Locator arg0) {}
122 public void skippedEntity(String arg0) throws SAXException {}
124 public void startDocument() throws SAXException {}
126 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
127 if ("pathentry".equals(qName))
128 if ("project".equals(atts.getValue("type"))) {
129 IPath referencedProjectPath = new Path(atts.getValue("path"));
130 IProject referencedProject = getProject(referencedProjectPath.lastSegment());
131 loadPathEntries.add(new LoadPathEntry(referencedProject));
135 public void startPrefixMapping(String arg0, String arg1) throws SAXException {}
139 protected IFile getLoadPathEntriesFile() {
140 return project.getFile(".loadpath");
143 public void save() throws CoreException {
145 InputStream xmlPath = new ByteArrayInputStream(getLoadPathXML().getBytes());
146 IFile loadPathsFile = getLoadPathEntriesFile();
147 if (!loadPathsFile.exists())
148 loadPathsFile.create(xmlPath, true, null);
150 loadPathsFile.setContents(xmlPath, true, false, null);
156 protected String getLoadPathXML() {
157 StringBuffer buffer = new StringBuffer();
158 buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><loadpath>");
160 Iterator pathEntriesIterator = loadPathEntries.iterator();
162 while (pathEntriesIterator.hasNext()) {
163 LoadPathEntry entry = (LoadPathEntry) pathEntriesIterator.next();
164 buffer.append(entry.toXML());
167 buffer.append("</loadpath>");
168 return buffer.toString();
170 public IResource getUnderlyingResource() {