source: java/FramclipsePlugin/src/main/java/com/framsticks/framclipse/internal/parser/FramclipseParser.java @ 13

Last change on this file since 13 was 13, checked in by jbochenski, 15 years ago
  • Property svn:mime-type set to text/plain
File size: 2.8 KB
Line 
1package com.framsticks.framclipse.internal.parser;
2
3import java.io.File;
4import java.io.FileReader;
5import java.io.InputStream;
6import java.lang.reflect.Field;
7import java.net.URL;
8
9import com.framsticks.framclipse.editors.EditorType;
10
11
12public class FramclipseParser {
13
14        public final static void recursiveSetOffset(Node root,int begin,int end){
15                final int modelNumChild = root.jjtGetNumChildren();
16                for (int i = 0; i < modelNumChild; ++i) {
17                        final Node child = root.jjtGetChild(i);
18                        if (child instanceof ElementWithOffset) {
19                                final ElementWithOffset ewo = (ElementWithOffset) child;
20                                ewo.setBeginOffset(begin);
21                                ewo.setEndOffset(end);
22                        }
23                        recursiveSetOffset(child, begin, end);
24                }
25               
26        }
27       
28        public Node parse(String content, EditorType editorType, URL url) {
29                if (editorType.equals(EditorType.FRAMSCRIPT)) {
30
31                } else {
32                        try {
33                                FramclipseNonScriptParser parser = new FramclipseNonScriptParser(
34                                                content);
35                                ElementWithOffset model = parser.FramclipseFile();
36                                model.setBeginOffset(0);
37                                model.setEndOffset(content.length() - 1);
38                                if (url == null) return model;
39                                String path = url.toString();
40                                path = path.substring(0, path.lastIndexOf('/') + 1);
41                                // if (File.separatorChar != '/')
42                                // path = path.replace('/', File.separatorChar);
43
44                                final int modelNumChild = model.jjtGetNumChildren();
45                                for (int i = 0; i < modelNumChild; ++i) {
46                                        final Node child = model.jjtGetChild(i);
47                                        if (child instanceof ASTGlobalInclude) {
48                                                final ASTGlobalInclude inc = (ASTGlobalInclude) child;
49                                                final String fname = inc.getFileName();
50                                                if (!"N/A".equals(fname)) {
51                                                        try {
52                                                                final URL incurl = new URL(path + fname);
53                                                                final InputStream stream = incurl.openStream();
54                                                                final CharStreamWithOffset cs = new CharStreamWithOffset(stream);
55                                                                final FramclipseNonScriptParser parser2 = new FramclipseNonScriptParser(
56                                                                                cs);
57                                                                final Field fld = FramclipseNonScriptParser.class.getDeclaredField("charStream");
58                                                                fld.setAccessible(true);
59                                                                fld.set(parser2, cs);                                                           
60                                                                final ASTFramclipseFile include = parser2
61                                                                                .FramclipseFile();
62                                                                for (int j = 0; j < include.jjtGetNumChildren(); ++j) {
63                                                                        final Node ic = include.jjtGetChild(j);
64                                                                        inc.jjtAddChild(ic, j);
65                                                                        ic.jjtSetParent(inc);
66                                                                }
67                                                                recursiveSetOffset(inc, inc.getBeginOffset(), inc.getEndOffset());
68                                                        } catch (Throwable e) {
69                                                                inc.setValid(false);
70                                                                System.err.println("In "+path+fname);
71                                                                e.printStackTrace();
72                                                        }
73                                                }
74                                        }
75                                }
76                                return model;
77                        } catch (Exception e) {
78                                e.printStackTrace();
79                        } catch (TokenMgrError err) {
80                                err.printStackTrace();
81                        }
82                }
83
84                return null;
85
86        }
87}
Note: See TracBrowser for help on using the repository browser.