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.bridge.portlet.JuzuPortlet;
import juzu.impl.compiler.CompilationError;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
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 writer the writer
* @throws IOException any io exception
*/
public static void renderStyleSheet(Writer writer) throws IOException {
// Get CSS
URL cssURL = Formatting.class.getResource("juzu.css");
String css = Tools.read(cssURL);
css = css.replace("\"", "\\\"");
css = css.replace("'", "\\'");
css = css.replace("\n", "\\n");
//
writer.append("\n");
}
/**
* Renders the throwable in the specified writer.
*
* @param writer the writer
* @param t the throwable
* @throws IOException any io exception
*/
public static void renderThrowable(Class> stop, Writer writer, 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(writer) {
@Override
public void println(Object x) {
if (open.get()) {
super.append("