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