All Downloads are FREE. Search and download functionalities are using the official Maven repository.

juzu.impl.common.Formatting Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
/*
 * 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("
"); } super.append("

"); super.append(String.valueOf(x)); super.append("

"); open.set(false); } @Override public void println(String x) { if (!open.get()) { super.append("
"); } // writer.append(""); } /** * Renders an iterable of {@link CompilationError} in the specified appendable. * * @param writer the writer * @param errors the errors * @throws IOException any io exception */ public static void renderErrors(Writer writer, Iterable errors) throws IOException { renderStyleSheet(writer); // for (CompilationError error : errors) { writer.append("
"); writer.append("

").append(error.getMessage()).append("

"); // Display the source code File source = error.getSourceFile(); if (source != null) { int line = error.getLocation().getLine(); int from = line - 2; int to = line + 3; BufferedReader reader = new BufferedReader(new FileReader(source)); int count = 1; writer.append("
    "); for (String s = reader.readLine();s != null;s = reader.readLine()) { if (count >= from && count < to) { if (count == line) { writer.append("
  1. ").append(s).append("

  2. "); } else { writer.append("
  3. ").append(s).append("

  4. "); } } count++; } writer.append("
"); } writer.append("
"); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy