apps/scenebuilder/SceneBuilderKit/src/com/oracle/javafx/scenebuilder/kit/fxom/FXOMLoader.java

Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization


  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package com.oracle.javafx.scenebuilder.kit.fxom;
  33 
  34 import com.oracle.javafx.scenebuilder.kit.util.Deprecation;
  35 import com.oracle.javafx.scenebuilder.kit.metadata.util.PropertyName;
  36 import com.sun.javafx.fxml.LoadListener;
  37 import java.io.ByteArrayInputStream;
  38 import java.io.IOException;
  39 import java.io.InputStream;
  40 import java.nio.charset.Charset;
  41 import javafx.fxml.FXMLLoader;
  42 
  43 
  44 /**
  45  *
  46  * 
  47  */
  48 class FXOMLoader implements LoadListener {
  49     
  50     private final FXOMDocument document;
  51     private TransientNode currentTransientNode;
  52     private GlueCursor glueCursor;
  53     
  54     /*
  55      * FXOMLoader
  56      */


  59         assert document != null;
  60         assert document.getGlue().getRootElement() != null;
  61         this.document = document;
  62     }
  63     
  64     public void load(String fxmlText) throws java.io.IOException {
  65         assert fxmlText != null;
  66         
  67         final ClassLoader classLoader;
  68         if (document.getClassLoader() != null) {
  69             classLoader = document.getClassLoader();
  70         } else {
  71             classLoader = FXMLLoader.getDefaultClassLoader();
  72         }
  73         
  74         FXMLLoader fxmlLoader = new FXMLLoader();
  75         
  76         fxmlLoader.setLocation(document.getLocation());
  77         fxmlLoader.setResources(new ResourceKeyCollector(document.getResources()));
  78         fxmlLoader.setClassLoader(new TransientClassLoader(classLoader));
  79         fxmlLoader.setBuilderFactory(new FXOMBuilderFactory(classLoader));
  80         Deprecation.setStaticLoad(fxmlLoader, true);
  81         Deprecation.setLoadListener(fxmlLoader, this);
  82         
  83         final Charset utf8 = Charset.forName("UTF-8");
  84         try (final InputStream is = new ByteArrayInputStream(fxmlText.getBytes(utf8))) {
  85             glueCursor = new GlueCursor(document.getGlue());
  86             currentTransientNode = null;
  87             assert is.markSupported();
  88             is.reset();
  89             document.setSceneGraphRoot(fxmlLoader.load(is));
  90 //            assert document.isConsistent(); // TODO Eric - Returns true on Preview
  91         } catch(RuntimeException | IOException x) {
  92             throw new IOException(x);
  93         }
  94     }
  95     
  96     public FXOMDocument getDocument() {
  97         return document;
  98     }
  99     
 100     
 101     /*




  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package com.oracle.javafx.scenebuilder.kit.fxom;
  33 
  34 import com.oracle.javafx.scenebuilder.kit.util.Deprecation;
  35 import com.oracle.javafx.scenebuilder.kit.metadata.util.PropertyName;
  36 import javafx.fxml.LoadListener;
  37 import java.io.ByteArrayInputStream;
  38 import java.io.IOException;
  39 import java.io.InputStream;
  40 import java.nio.charset.Charset;
  41 import javafx.fxml.FXMLLoader;
  42 
  43 
  44 /**
  45  *
  46  * 
  47  */
  48 class FXOMLoader implements LoadListener {
  49     
  50     private final FXOMDocument document;
  51     private TransientNode currentTransientNode;
  52     private GlueCursor glueCursor;
  53     
  54     /*
  55      * FXOMLoader
  56      */


  59         assert document != null;
  60         assert document.getGlue().getRootElement() != null;
  61         this.document = document;
  62     }
  63     
  64     public void load(String fxmlText) throws java.io.IOException {
  65         assert fxmlText != null;
  66         
  67         final ClassLoader classLoader;
  68         if (document.getClassLoader() != null) {
  69             classLoader = document.getClassLoader();
  70         } else {
  71             classLoader = FXMLLoader.getDefaultClassLoader();
  72         }
  73         
  74         FXMLLoader fxmlLoader = new FXMLLoader();
  75         
  76         fxmlLoader.setLocation(document.getLocation());
  77         fxmlLoader.setResources(new ResourceKeyCollector(document.getResources()));
  78         fxmlLoader.setClassLoader(new TransientClassLoader(classLoader));
  79         fxmlLoader.setLoadListener(this);
  80         Deprecation.setStaticLoad(fxmlLoader, true);

  81 
  82         final Charset utf8 = Charset.forName("UTF-8");
  83         try (final InputStream is = new ByteArrayInputStream(fxmlText.getBytes(utf8))) {
  84             glueCursor = new GlueCursor(document.getGlue());
  85             currentTransientNode = null;
  86             assert is.markSupported();
  87             is.reset();
  88             document.setSceneGraphRoot(fxmlLoader.load(is));
  89 //            assert document.isConsistent(); // TODO Eric - Returns true on Preview
  90         } catch(RuntimeException | IOException x) {
  91             throw new IOException(x);
  92         }
  93     }
  94     
  95     public FXOMDocument getDocument() {
  96         return document;
  97     }
  98     
  99     
 100     /*