1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package org.openrdf.elmo.codegen;
30
31 import java.io.File;
32 import java.io.FileWriter;
33 import java.io.IOException;
34 import java.io.Writer;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
39
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43
44
45
46
47
48
49 public class FileSourceCodeHandler implements SourceCodeHandler {
50
51 private static final Pattern PACKAGE = Pattern.compile("package ([^;]*);");
52
53 private static final Pattern INTERFACE = Pattern
54 .compile("public (interface|class|abstract class) (\\S*) ");
55
56 private static final Pattern CONCRETE = Pattern.compile(
57 ".*public class .*", Pattern.DOTALL);
58
59 private static final Pattern ANNOTATED = Pattern.compile(".*"
60 + PACKAGE.pattern() + ".*@.*" + INTERFACE.pattern() + ".*",
61 Pattern.DOTALL);
62
63 private final Logger logger = LoggerFactory
64 .getLogger(OntologyConverter.class);
65
66 private File target;
67
68 private List<String> content = new ArrayList<String>();
69
70 private List<String> annotatedClasses = new ArrayList<String>();
71
72 private List<String> concreteClasses = new ArrayList<String>();
73
74 public FileSourceCodeHandler() throws IOException {
75 String prefix = FileSourceCodeHandler.class.getSimpleName();
76 target = File.createTempFile(prefix, "");
77 target.delete();
78 target.mkdir();
79 }
80
81 public FileSourceCodeHandler(File target) {
82 this.target = target;
83 }
84
85 public File getTarget() {
86 return target;
87 }
88
89 public List<String> getConcreteClasses() {
90 return concreteClasses;
91 }
92
93 public List<String> getAnnotatedClasses() {
94 return annotatedClasses;
95 }
96
97 public List<String> getClasses() {
98 return content;
99 }
100
101 public void handleSource(String code) throws IOException {
102 String pkg = getPackageName(code);
103 String name = getSimpleClassName(code);
104 if (name == null)
105 name = "package-info";
106 String className = pkg + '.' + name;
107 logger.debug("Saving {}", className);
108 saveClass(pkg, name, code);
109 content.add(className);
110 if (ANNOTATED.matcher(code).matches())
111 annotatedClasses.add(className);
112 if (CONCRETE.matcher(code).matches())
113 concreteClasses.add(className);
114 }
115
116 private String getPackageName(String code) {
117 Matcher m = PACKAGE.matcher(code);
118 m.find();
119 String pkg = m.group(1);
120 return pkg;
121 }
122
123 private String getSimpleClassName(String code) {
124 Matcher m;
125 m = INTERFACE.matcher(code);
126 if (m.find())
127 return m.group(2);
128 return null;
129 }
130
131 private File saveClass(String pkg, String name, String code)
132 throws IOException {
133 File folder = new File(target, pkg.replace('.', '/'));
134 folder.mkdirs();
135 File file = new File(folder, name + ".java");
136 Writer writer = new FileWriter(file);
137 try {
138 writer.write(code);
139 } finally {
140 writer.close();
141 }
142 return file;
143 }
144 }