juzu.impl.common.Formatting Maven / Gradle / Ivy
/*
* Copyright 2013 eXo Platform SAS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package juzu.impl.common;
import juzu.impl.compiler.CompilationError;
import juzu.io.UndeclaredIOException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Various utilities for formatting stuff.
*
* @author Julien Viet
*/
public class Formatting {
/**
* Append a script section that will add a stylesheet
element in the head section of
* the document.
*
* @param buffer the buffer
*/
public static void renderStyleSheet(StringBuilder buffer) {
try {
renderStyleSheet((Appendable)buffer);
}
catch (IOException e) {
throw new UndeclaredIOException(e);
}
}
/**
* Append a script section that will add a stylesheet
element in the head section of
* the document.
*
* @param appendable the appendable
* @throws IOException any io exception
*/
public static void renderStyleSheet(Appendable appendable) throws IOException {
URL cssURL = Formatting.class.getResource("juzu.css");
String css = Tools.read(cssURL);
css = css.replace("\"", "\\\"");
css = css.replace("'", "\\'");
css = css.replace("\n", "\\n");
appendable.append("\n");
}
/**
* Renders the throwable in the specified writer.
*
* @param buffer the writer
* @param t the throwable
*/
public static void renderThrowable(Class> stop, StringBuilder buffer, Throwable t) {
try {
renderThrowable(stop, (Appendable)buffer, t);
}
catch (IOException e) {
throw new UndeclaredIOException(e);
}
}
/**
* Renders the throwable in the specified writer.
*
* @param appendable the writer
* @param t the throwable
* @throws IOException any io exception
*/
public static void renderThrowable(Class> stop, Appendable appendable, Throwable t) throws IOException {
// Trim the stack trace to remove stuff we don't want to see
int size = 0;
StackTraceElement[] trace = t.getStackTrace();
for (StackTraceElement element : trace) {
if (stop != null && element.getClassName().equals(stop.getName())) {
break;
}
else {
size++;
}
}
StackTraceElement[] ourTrace = new StackTraceElement[size];
System.arraycopy(trace, 0, ourTrace, 0, ourTrace.length);
t.setStackTrace(ourTrace);
// We hack a bit
final AtomicBoolean open = new AtomicBoolean(false);
PrintWriter formatter = new PrintWriter(new AppendableWriter(appendable)) {
@Override
public void println(Object x) {
if (open.get()) {
super.append("