1 package org.apache.torque.generator.configuration.source;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.COMBINE_FILES_ATTRIBUTE;
23 import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.EXCLUDE_TAG;
24 import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.FORMAT_ATTRIBUTE;
25 import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.INCLUDE_TAG;
26 import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.SOURCE_TAG;
27
28 import java.util.HashSet;
29 import java.util.Set;
30
31 import org.apache.torque.generator.configuration.ConfigurationException;
32 import org.apache.torque.generator.configuration.ConfigurationHandlers;
33 import org.apache.torque.generator.configuration.ConfigurationProvider;
34 import org.apache.torque.generator.configuration.SaxHelper;
35 import org.apache.torque.generator.configuration.paths.ProjectPaths;
36 import org.apache.torque.generator.file.Fileset;
37 import org.apache.torque.generator.source.SourceProvider;
38 import org.apache.torque.generator.source.stream.FileSourceProvider;
39 import org.apache.torque.generator.source.stream.StreamSourceFormat;
40 import org.xml.sax.Attributes;
41 import org.xml.sax.SAXException;
42
43
44
45
46 public class FileSourceSaxHandler extends SourceSaxHandler
47 {
48
49 private String format;
50
51
52 private Set<String> includes = new HashSet<String>();
53
54
55 private Set<String> excludes = new HashSet<String>();
56
57
58 private Boolean combineFiles;
59
60
61
62
63
64 private StringBuilder currentInclude = null;
65
66
67
68
69
70 private StringBuilder currentExclude = null;
71
72
73
74
75 private SourceProvider sourceProvider;
76
77
78
79
80
81
82
83
84
85
86
87 public FileSourceSaxHandler(
88 ConfigurationProvider configurationProvider,
89 ProjectPaths projectPaths,
90 ConfigurationHandlers configurationHandlers)
91 {
92 super(configurationProvider, projectPaths, configurationHandlers);
93 }
94
95
96
97
98 @Override
99 public void startElement(String uri, String localName, String rawName,
100 Attributes attributes)
101 throws SAXException
102 {
103 if (rawName.equals(INCLUDE_TAG))
104 {
105 currentInclude = new StringBuilder();
106 }
107 else if (rawName.equals(EXCLUDE_TAG))
108 {
109 currentExclude = new StringBuilder();
110 }
111 else if (rawName.equals(SOURCE_TAG))
112 {
113 super.startElement(uri, localName, rawName, attributes);
114 format = attributes.getValue(FORMAT_ATTRIBUTE);
115 if (attributes.getValue(COMBINE_FILES_ATTRIBUTE) != null)
116 {
117 combineFiles = SaxHelper.getBooleanAttribute(
118 COMBINE_FILES_ATTRIBUTE,
119 attributes,
120 "the element " + SOURCE_TAG);
121 }
122 }
123 else
124 {
125 super.startElement(uri, localName, rawName, attributes);
126 }
127 }
128
129
130
131
132 @Override
133 public void endElement(String uri, String localName, String rawName)
134 throws SAXException
135 {
136 if (rawName.equals(INCLUDE_TAG))
137 {
138 includes.add(currentInclude.toString());
139 currentInclude = null;
140 }
141 else if (rawName.equals(EXCLUDE_TAG))
142 {
143 excludes.add(currentExclude.toString());
144 currentExclude = null;
145 }
146 else if (rawName.equals(SOURCE_TAG))
147 {
148 try
149 {
150 StreamSourceFormat sourceFormat = null;
151 if (format != null)
152 {
153 Set<StreamSourceFormat> sourceFormats
154 = getConfigurationHandlers().getStreamSourceFormats();
155 for (StreamSourceFormat candidate : sourceFormats)
156 {
157 if (format.equals(candidate.getKey()))
158 {
159 sourceFormat = candidate;
160 break;
161 }
162 }
163 if (sourceFormat == null)
164 {
165 throw new SAXException("Unknown source format : "
166 + format
167 + " Known types are: " + sourceFormats);
168 }
169 }
170 Fileset sourceFileset = new Fileset(
171 getProjectPaths().getDefaultSourcePath(),
172 includes,
173 excludes);
174
175 sourceProvider = new FileSourceProvider(
176 sourceFormat,
177 sourceFileset,
178 combineFiles);
179 }
180 catch (ConfigurationException e)
181 {
182 throw new SAXException("Could not create source: "
183 + e.getMessage(),
184 e);
185 }
186 super.endElement(uri, localName, rawName);
187 finished();
188 }
189 else
190 {
191 super.endElement(uri, localName, rawName);
192 }
193 }
194
195
196
197
198 @Override
199 public void characters(char[] ch, int start, int length)
200 throws SAXException
201 {
202 if (currentInclude != null)
203 {
204 for (int i = start; i < start + length; ++i)
205 {
206 currentInclude.append(ch[i]);
207 }
208 }
209 else if (currentExclude != null)
210 {
211 for (int i = start; i < start + length; ++i)
212 {
213 currentExclude.append(ch[i]);
214 }
215 }
216 else
217 {
218 super.characters(ch, start, length);
219 }
220 }
221
222
223
224
225
226
227
228 public SourceProvider getSourceProvider()
229 {
230 return sourceProvider;
231 }
232 }