Example #1
0
  /**
   * Loads an SVGT document from the given URL.
   *
   * @param urlStr The SVGT document URL or path.
   * @return An SVGT document.
   */
  protected SVGDocument loadSVG(URL url) {
    InputStream is = null;
    try {
      String completeUrl = url.toExternalForm();
      int p = completeUrl.lastIndexOf('/');
      if (p != -1) {
        baseURL = new URL(completeUrl.substring(0, p + 1));
      }
      is = url.openStream();
      if (url.toString().endsWith("svgz")) {
        is = new GZIPInputStream(is);
      }
    } catch (Exception ex) {
      logger.error(ex);
      // ex.printStackTrace();
      Enumeration en = errorListeners.elements();
      while (en.hasMoreElements()) {
        SVGViewerErrorListener l = (SVGViewerErrorListener) en.nextElement();
        l.error(new SVGViewerErrorEvent(ex));
      }
    }

    return loadSVG(is);
  }
Example #2
0
 /**
  * Loads an SVGT document from the given InputStream.
  *
  * @param is The InputStream.
  * @return An SVGT document.
  */
 private SVGDocument loadSVG(InputStream is) {
   loaded = false;
   String errorMessage = null;
   Throwable throwable = null;
   SVGDocument doc = raster.createSVGDocument();
   try {
     // Read and parse the SVGT stream
     TinyPixbuf pixbuf = raster.getPixelBuffer();
     // Create the SVGT attributes parser
     SVGAttr attrParser = new SVGAttr(pixbuf.width, pixbuf.height);
     // Create the SVGT stream parser
     SVGParser parser = new SVGParser(attrParser);
     // Parse the input SVGT stream parser into the document
     int errorCode = parser.load(doc, is);
     errorCode = errorCode >> 10;
     if (errorCode != 0) {
       logger.error("Error al parsear SVG. Código:" + errorCode);
       errorMessage = "Error de sintaxis XML";
     } else {
       if (doc.root.children == null || doc.root.children.data[0] instanceof SVGUnknownElem) {
         errorMessage = "El fichero no es un SVG válido";
       } else {
         // Fichero correcto
         logger.debug("Fichero cargado correctamente");
         loaded = true;
       }
     }
   } catch (OutOfMemoryError memerror) {
     doc = null;
     Runtime.getRuntime().gc();
     logger.error("Not enought memory", memerror);
     errorMessage = "Memoria insuficiente";
     throwable = memerror;
     // memerror.printStackTrace();
   } catch (SecurityException secex) {
     doc = null;
     logger.error("Security violation", secex);
     errorMessage = "Violación de seguridad";
     throwable = secex;
     // secex.printStackTrace();
   } catch (Exception ex) {
     doc = null;
     logger.error("Not in SVGT format", ex);
     errorMessage = "El fichero no está en formato SVG Tiny";
     throwable = ex;
     // ex.printStackTrace();
   } catch (Throwable t) {
     doc = null;
     logger.error("Not in SVGT format", t);
     errorMessage = "El fichero no está en formato SVG Tiny";
     throwable = t;
     // thr.printStackTrace();
   } finally {
     if (errorMessage != null) {
       Enumeration en = errorListeners.elements();
       while (en.hasMoreElements()) {
         SVGViewerErrorListener l = (SVGViewerErrorListener) en.nextElement();
         l.error(new SVGViewerErrorEvent(errorMessage, throwable));
       }
     }
     try {
       if (is != null) is.close();
     } catch (IOException ioe) {
       logger.error(ioe);
       // ioe.printStackTrace();
     }
   }
   return doc;
 }