View Javadoc

1   /*
2    * Copyright (c) 2007, James Leigh All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    * 
7    * - Redistributions of source code must retain the above copyright notice, this
8    *   list of conditions and the following disclaimer.
9    * - Redistributions in binary form must reproduce the above copyright notice,
10   *   this list of conditions and the following disclaimer in the documentation
11   *   and/or other materials provided with the distribution. 
12   * - Neither the name of the openrdf.org nor the names of its contributors may
13   *   be used to endorse or promote products derived from this software without
14   *   specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26   * POSSIBILITY OF SUCH DAMAGE.
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   * Saves the generated source code from CodeGenerator to disk.
45   * 
46   * @author James Leigh
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 }