Organized imports
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.js.core / src / net / sourceforge / phpeclipse / js / core / parser / JSSyntaxModelFactory.java
1 /*
2  * $RCSfile: JSSyntaxModelFactory.java,v $
3  * 
4  * Copyright 2002 CH-1700 Fribourg, Switzerland All rights reserved.
5  * 
6  * ========================================================================
7  * Modifications history
8  * ========================================================================
9  * $Log: not supported by cvs2svn $
10  * Revision 1.1  2004/09/02 18:14:38  jsurfer
11  * intial source from ttp://www.sf.net/projects/wdte
12  *
13  * Revision 1.2  2004/02/27 17:25:25  cell
14  * Fix NPE for files without an extension
15  *
16  * Revision 1.1  2004/02/26 02:25:42  agfitzp
17  * renamed packages to match xml & css
18  *
19  * Revision 1.1  2004/02/05 03:10:08  agfitzp
20  * Initial Submission
21  *
22  * Revision 1.1.2.1  2003/12/12 21:37:24  agfitzp
23  * Experimental work for Classes view
24  * Revision 1.3 2003/05/30 20:53:08 agfitzp
25  * 0.0.2 : Outlining is now done as the user types. Some other bug fixes.
26  * 
27  * Revision 1.2 2003/05/28 20:47:56 agfitzp Outline the document, not the file.
28  * 
29  * Revision 1.1 2003/05/28 15:17:11 agfitzp net.sourceforge.phpeclipse.js.core 0.0.1 code
30  * base
31  * 
32  * ========================================================================
33  */
34
35 package net.sourceforge.phpeclipse.js.core.parser;
36
37 import java.util.LinkedList;
38 import java.util.List;
39
40 import net.sourceforge.phpeclipse.js.core.model.JSElementList;
41
42 import org.eclipse.core.resources.IContainer;
43 import org.eclipse.core.resources.IFile;
44 import org.eclipse.core.resources.IFolder;
45 import org.eclipse.core.resources.IProject;
46 import org.eclipse.core.resources.IResource;
47 import org.eclipse.core.runtime.CoreException;
48 import org.eclipse.jface.text.IDocument;
49
50 /**
51  * @author Addi
52  */
53 public class JSSyntaxModelFactory {
54         private static JSSyntaxModelFactory instance = new JSSyntaxModelFactory();
55
56         /**
57          * Creates a new JSSyntaxModelFactory.
58          */
59         private JSSyntaxModelFactory() {
60         }
61         /**
62          * @param adaptable
63          * 
64          * @return
65          */
66         public JSElementList getContentOutline(IProject aProject) {
67                 return new JSElementList(getSyntacticElements(aProject));
68         }
69
70         /**
71          * @param adaptable
72          * 
73          * @return
74          */
75         public JSElementList getContentOutline(IFile adaptable) {
76                 return new JSElementList(getSyntacticElements(adaptable));
77         }
78
79         /**
80          * @param document
81          * 
82          * @return
83          */
84         public JSElementList getContentOutline(IDocument document) {
85                 return new JSElementList(getSyntacticElements(document));
86         }
87
88         /**
89          * Returns the singleton.
90          * 
91          * @return
92          */
93         public static JSSyntaxModelFactory getInstance() {
94                 return instance;
95         }
96
97         /**
98          * @param file
99          * 
100          * @return
101          */
102         private List getSyntacticElements(IProject aProject) {
103                 int i;
104                 JSParser aParser = new JSParser();
105                 Object[] jsFiles = getJSFilesFor(aProject);
106
107                 for (i = 0; i < jsFiles.length; i++) {
108                         aParser.parse((IFile) jsFiles[i]);
109                 }
110                 return aParser.getElementList();
111         }
112
113         /**
114          * @param project
115          * @return
116          */
117         private Object[] getJSFilesFor(IProject project) {
118                 LinkedList files = new LinkedList();
119                 collectJSFiles(project, files);
120                 return files.toArray();
121         }
122
123         private void collectJSFiles(IContainer aContainer, LinkedList files) {
124                 try {
125                         int i;
126                         IResource[] members = aContainer.members();
127                         for (i = 0; i < members.length; i++) {
128                                 IResource aResource = members[i];
129                                 if (aResource.getType() == IResource.FILE) {
130                                         IFile aFile = (IFile) aResource;
131                                         String ext = aFile.getFileExtension();
132                                         if ((ext != null) && ext.equals("js")) {
133                                                 files.add(aFile);
134                                         }
135                                 } else if (aResource.getType() == IResource.FOLDER) {
136                                         collectJSFiles((IFolder) aResource, files);
137                                 }
138                         }
139                 } catch (CoreException e) {
140                 }
141         }
142         /**
143          * @param file
144          * 
145          * @return
146          */
147
148         private List getSyntacticElements(IFile file) {
149                 return (new JSParser()).parse(file);
150         }
151
152         /**
153          * @param file
154          * 
155          * @return
156          */
157         private List getSyntacticElements(IDocument document) {
158                 return (new JSParser()).parse(document);
159         }
160 }