TestCase and Mockobject for PHPPartitionScanner Junit testing.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / resourcesview / PHPProject.java
1 package net.sourceforge.phpeclipse.resourcesview;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.InputStream;
5 import java.util.ArrayList;
6 import java.util.Iterator;
7 import java.util.List;
8
9 import javax.xml.parsers.SAXParserFactory;
10
11 import net.sourceforge.phpeclipse.LoadPathEntry;
12 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
13
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;
27
28 public class PHPProject implements IProjectNature, PHPElement {
29         protected IProject project;
30         protected List loadPathEntries;
31         protected boolean scratched;
32
33         public PHPProject() {}
34
35         public void configure() throws CoreException {}
36
37         public void deconfigure() throws CoreException {}
38
39         public IProject getProject() {
40                 return project;
41         }
42
43         protected IProject getProject(String name) {
44                 return PHPeclipsePlugin.getWorkspace().getRoot().getProject(name);
45         }
46
47         public void setProject(IProject aProject) {
48                 project = aProject;
49         }
50
51         public void addLoadPathEntry(IProject anotherPHPProject) {
52                 scratched = true;
53
54                 LoadPathEntry newEntry = new LoadPathEntry(anotherPHPProject);
55                 getLoadPathEntries().add(newEntry);
56         }
57
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);
64                                 scratched = true;
65                                 break;
66                         }
67                 }
68         }
69
70         public List getLoadPathEntries() {
71                 if (loadPathEntries == null) {
72                         loadLoadPathEntries();
73                 }
74
75                 return loadPathEntries;
76         }
77
78         public List getReferencedProjects() {
79                 List referencedProjects = new ArrayList();
80
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());
86                 }
87
88                 return referencedProjects;
89         }
90
91         protected void loadLoadPathEntries() {
92                 loadPathEntries = new ArrayList();
93
94                 IFile loadPathsFile = getLoadPathEntriesFile();
95
96                 XMLReader reader = null;
97                 try {
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
103                 }
104         }
105
106         protected ContentHandler getLoadPathEntriesContentHandler() {
107                 return new ContentHandler() {
108                         public void characters(char[] arg0, int arg1, int arg2) throws SAXException {}
109
110                         public void endDocument() throws SAXException {}
111
112                         public void endElement(String arg0, String arg1, String arg2) throws SAXException {}
113
114                         public void endPrefixMapping(String arg0) throws SAXException {}
115
116                         public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {}
117
118                         public void processingInstruction(String arg0, String arg1) throws SAXException {}
119
120                         public void setDocumentLocator(Locator arg0) {}
121
122                         public void skippedEntity(String arg0) throws SAXException {}
123
124                         public void startDocument() throws SAXException {}
125
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));
132                                         }
133                         }
134
135                         public void startPrefixMapping(String arg0, String arg1) throws SAXException {}
136                 };
137         }
138
139         protected IFile getLoadPathEntriesFile() {
140                 return project.getFile(".loadpath");
141         }
142
143         public void save() throws CoreException {
144                 if (scratched) {
145                         InputStream xmlPath = new ByteArrayInputStream(getLoadPathXML().getBytes());
146                         IFile loadPathsFile = getLoadPathEntriesFile();
147                         if (!loadPathsFile.exists())
148                                 loadPathsFile.create(xmlPath, true, null);
149                         else
150                                 loadPathsFile.setContents(xmlPath, true, false, null);
151
152                         scratched = false;
153                 }
154         }
155
156         protected String getLoadPathXML() {
157                 StringBuffer buffer = new StringBuffer();
158                 buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><loadpath>");
159
160                 Iterator pathEntriesIterator = loadPathEntries.iterator();
161
162                 while (pathEntriesIterator.hasNext()) {
163                         LoadPathEntry entry = (LoadPathEntry) pathEntriesIterator.next();
164                         buffer.append(entry.toXML());
165                 }
166
167                 buffer.append("</loadpath>");
168                 return buffer.toString();
169         }
170         public IResource getUnderlyingResource() {
171                 return project;
172         }
173
174 }