private static final String XML = "xml";
public static CodeExample read(URL sourceFileURL, String sourceFileName, boolean productionMode)
throws IOException {
TextResource textResource = TextResourceUtil.read(sourceFileURL);
String sourceCodeText = textResource.getText();
if (sourceCodeText != null) {
String fileExtension;
if (sourceFileName.endsWith(JAVA_EXTENSION)) {
fileExtension = JAVA;
Matcher matcher = JAVA_MULTILINE_COMMENTS_PATTERN.matcher(sourceCodeText);
sourceCodeText = matcher.replaceAll(StringPool.BLANK);
}
else {
fileExtension = XML;
sourceCodeText = TEMPLATE_ATTRIBUTE_PATTERN.matcher(sourceCodeText).replaceAll(StringPool.BLANK);
sourceCodeText = SHOWCASE_NAMESPACE_PATTERN.matcher(sourceCodeText).replaceAll(StringPool.BLANK);
StringReader stringReader = new StringReader(sourceCodeText);
StringBuffer buf = new StringBuffer();
BufferedReader bufferedReader = new BufferedReader(stringReader);
int trimTab = 0;
String line;
boolean ignoreNextLine = false;
while ((line = bufferedReader.readLine()) != null) {
String trimmedLine = line.trim();
if (ignoreNextLine) {
ignoreNextLine = !trimmedLine.endsWith(StringPool.GREATER_THAN);
}
else {
if (trimmedLine.startsWith("<showcase") || trimmedLine.startsWith("<ui:define")) {
trimTab++;
ignoreNextLine = !trimmedLine.endsWith(StringPool.GREATER_THAN);
}
else if (trimmedLine.startsWith("</showcase") || trimmedLine.startsWith("</ui:define")) {
trimTab--;
}
else {
for (int i = 0; i < trimTab; i++) {
if (line.startsWith(StringPool.TAB)) {
line = line.substring(1);
}
}
int pos = line.indexOf(OUTPUTMODEL_MODELVALUE);
if (pos > 0) {
line = line.substring(0, pos) + ":modelValue" +
line.substring(pos + OUTPUTMODEL_MODELVALUE.length());
}
pos = line.indexOf(RENDER_EXAMPLE_FORM);
if (pos > 0) {
line = line.substring(0, pos) + "render=\"" +
line.substring(pos + RENDER_EXAMPLE_FORM.length());
}
pos = line.indexOf(RENDER_FORM_EXAMPLE);
if (pos > 0) {
line = line.substring(0, pos) + "render=\":exampleForm" +
line.substring(pos + RENDER_FORM_EXAMPLE.length());
}
if (productionMode) {
pos = line.indexOf(RENDERED);
if (pos > 0) {
line = line.substring(0, pos) + line.substring(pos + RENDERED.length());
}
}
// Strip empty lines
Pattern BLANK_LINE_PATTERN = Pattern.compile("\\t+$");
Matcher matcher = BLANK_LINE_PATTERN.matcher(line);
if (!matcher.matches()) {
buf.append(line);
buf.append(StringPool.NEW_LINE);
}
}
}
}
sourceCodeText = buf.toString();
}
sourceCodeText = sourceCodeText.trim();
return new CodeExample(sourceFileName, fileExtension, sourceFileURL, textResource.getLastModified(),
sourceCodeText);
}
else {
throw new IOException("Unable to locate " + sourceFileURL);
}