1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.css;
  27 
  28 import javafx.css.CssMetaData;
  29 import javafx.css.Styleable;
  30 import javafx.scene.Scene;
  31 
  32 import java.lang.ref.Reference;
  33 import java.lang.ref.WeakReference;
  34 
  35 /**
  36  * Encapsulate information about the source and nature of errors encountered
  37  * while parsing CSS or applying styles to Nodes. 
  38  */
  39 public class CssError {
  40  
  41     // RT-20643 - hold a ref so CssError doesn't leak Scene.
  42     private static Reference<Scene> SCENE_REF;
  43             
  44     /** 
  45      * Set the static scene variable. This scene will be set on all CssErrors
  46      * generated after the call is made. The argument may be null. Null should
  47      * be passed when the code exits a method from which CssErrors may be 
  48      * created. This is intended internal use and should not be called from
  49      * outside the css code.
  50      */
  51     public static void setCurrentScene(Scene scene) {
  52         
  53         // Treat as a no-op if noone cares about CssErrors
  54         if (StyleManager.getErrors() == null) return;
  55         
  56         if (scene != null) {
  57             // don't make new ref for same scene
  58             final Scene oldScene = SCENE_REF != null ? SCENE_REF.get() : null;
  59             if (oldScene != scene) {
  60                 SCENE_REF = new WeakReference<Scene>(scene);
  61             }
  62         } else {
  63             SCENE_REF = null;
  64         }
  65     }
  66     
  67     /** @return The error message from the CSS code. */
  68     public final String getMessage() {
  69         return message;
  70     }
  71 
  72     public CssError(String message) {
  73         this.message = message;
  74         // RT-20643        
  75         this.sceneRef = SCENE_REF;
  76     }
  77     
  78     /** 
  79      * @return The Scene in which this error occurred, if known, or null.
  80      */
  81     public Scene getScene() {
  82         return sceneRef != null ? sceneRef.get() : null;
  83     }
  84     
  85     // RT-20643 - track the scene that this error belongs to       
  86     // Note that CssError has the potential to leak Scene so the Scene
  87     // variable is held as a Reference.
  88     private final Reference<Scene> sceneRef;
  89     protected final String message;
  90 
  91     @Override
  92     public String toString() {
  93         return "CSS Error: " + message;
  94     }
  95 
  96     /** Encapsulate errors arising from parsing of stylesheet files */
  97     public final static class StylesheetParsingError extends CssError { 
  98         
  99         public StylesheetParsingError(String url, String message) {
 100             super(message);
 101             this.url = url;
 102         }
 103         
 104         public String getURL() {
 105             return url;
 106         }
 107         
 108         private final String url;
 109 
 110         @Override
 111         public String toString() {
 112             final String path = url != null ? url : "?";
 113             // TBD: i18n
 114             return "CSS Error parsing " + path + ": " + message;
 115         }
 116         
 117     }
 118     
 119     /** Encapsulate errors arising from parsing of Node's style property */
 120     public final static class InlineStyleParsingError extends CssError { 
 121         
 122         public InlineStyleParsingError(Styleable styleable, String message) {
 123             super(message);
 124             this.styleable = styleable;
 125         }
 126         
 127         public Styleable getStyleable() {
 128             return styleable;
 129         }
 130         
 131         private final Styleable styleable;
 132         
 133         @Override
 134         public String toString() {
 135             final String inlineStyle = styleable.getStyle();
 136             final String source = styleable.toString();
 137             // TBD: i18n
 138             return "CSS Error parsing in-line style \'" + inlineStyle + 
 139                     "\' from " + source + ": " + message;
 140         }
 141     } 
 142     
 143     /** 
 144      * Encapsulate errors arising from parsing when the style is not 
 145      * an in-line style nor is the style from a stylesheet. Primarily to
 146      * support unit testing.
 147      */
 148     public final static class StringParsingError extends CssError { 
 149         
 150         public StringParsingError(String style, String message) {
 151             super(message);
 152             this.style = style;
 153         }
 154         
 155         public String getStyle() {
 156             return style;
 157         }
 158         
 159         private final String style;
 160         
 161         @Override
 162         public String toString() {
 163             // TBD: i18n
 164             return "CSS Error parsing \'" + style + ": " + message;
 165         }
 166     } 
 167     
 168     /** Encapsulates errors arising from applying a style to a Node. */
 169     public final static class PropertySetError extends CssError { 
 170         
 171         public PropertySetError(CssMetaData styleableProperty, 
 172                 Styleable styleable, String message) {
 173             super(message);
 174             this.styleableProperty = styleableProperty;
 175             this.styleable = styleable;
 176         }
 177         
 178         public Styleable getStyleable() {
 179             return styleable;
 180         }
 181         
 182         public CssMetaData getProperty() {
 183             return styleableProperty;
 184         }
 185         
 186         private final CssMetaData styleableProperty;
 187         private final Styleable styleable;
 188         
 189     }
 190 }     
 191