Coverage Report - com.sun.tools.javafx.util.JavafxFileManager
 
Classes in this File Line Coverage Branch Coverage Complexity
JavafxFileManager
52%
15/29
50%
6/12
0
JavafxFileManager$1
100%
2/2
N/A
0
JavafxFileManager$DelegateJavaFileObject
57%
17/30
43%
6/14
0
 
 1  
 /*
 2  
  * Copyright 2005-2007 Sun Microsystems, Inc.  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.  Sun designates this
 8  
  * particular file as subject to the "Classpath" exception as provided
 9  
  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 22  
  * CA 95054 USA or visit www.sun.com if you need additional information or
 23  
  * have any questions.
 24  
  */
 25  
 
 26  
 package com.sun.tools.javafx.util;
 27  
 
 28  
 import com.sun.tools.javac.util.BaseFileObject;
 29  
 import com.sun.tools.javac.util.Context;
 30  
 import com.sun.tools.javac.util.JavacFileManager;
 31  
 import java.io.File;
 32  
 import java.io.IOException;
 33  
 import java.io.InputStream;
 34  
 import java.io.OutputStream;
 35  
 import java.io.Reader;
 36  
 import java.io.Writer;
 37  
 import java.net.URI;
 38  
 import java.nio.charset.Charset;
 39  
 import java.util.ArrayList;
 40  
 import java.util.EnumSet;
 41  
 import java.util.Set;
 42  
 import javax.lang.model.element.Modifier;
 43  
 import javax.lang.model.element.NestingKind;
 44  
 import javax.tools.JavaFileManager;
 45  
 import javax.tools.JavaFileObject;
 46  
 
 47  
 public class JavafxFileManager extends JavacFileManager {
 48  
     
 49  
     /**
 50  
      * The JavaFX Script source file extension.
 51  
      * @see javax.tools.JavaFileObject.Kind.SOURCE
 52  
      */
 53  
     public static final String FX_SOURCE_SUFFIX = ".fx";
 54  
 
 55  
     /**
 56  
      * Register a Context.Factory to create a JavafxFileManager.
 57  
      */
 58  
     public static void preRegister(final Context context) {
 59  702
         context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() {
 60  
             public JavaFileManager make() {
 61  351
                 return new JavafxFileManager(context, true, null);
 62  
             }
 63  
         });
 64  351
     }
 65  
 
 66  
     public JavafxFileManager(Context context, boolean register, Charset charset) {
 67  395
         super(context, register, charset);      
 68  395
     }
 69  
 
 70  
     @Override
 71  
     protected JavaFileObject.Kind getKind(String extension) {
 72  37167
         if (extension.equals(JavaFileObject.Kind.CLASS.extension))
 73  36867
             return JavaFileObject.Kind.CLASS;
 74  300
         else if (extension.equals(FX_SOURCE_SUFFIX))
 75  0
             return JavaFileObject.Kind.SOURCE;
 76  300
         else if (extension.equals(JavaFileObject.Kind.HTML.extension))
 77  0
             return JavaFileObject.Kind.HTML;
 78  
         else
 79  300
             return JavaFileObject.Kind.OTHER;
 80  
     }
 81  
 
 82  
     @Override
 83  
     public JavaFileObject getRegularFile(File file) {
 84  0
         return new DelegateJavaFileObject(super.getRegularFile(file));
 85  
     }
 86  
     
 87  
     @Override
 88  
     public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(
 89  
         Iterable<? extends File> files)
 90  
     {
 91  358
         Iterable<? extends JavaFileObject> objs = super.getJavaFileObjectsFromFiles(files);
 92  358
         ArrayList<DelegateJavaFileObject> result = new ArrayList<DelegateJavaFileObject>();
 93  358
         for (JavaFileObject jfo : objs)
 94  370
             result.add(new DelegateJavaFileObject(jfo));
 95  358
         return result;
 96  
     }
 97  
 
 98  
     @Override
 99  
     public JavaFileObject getJavaFileForInput(Location location,
 100  
                                               String className,
 101  
                                               JavaFileObject.Kind kind)
 102  
         throws IOException
 103  
     {
 104  0
         nullCheck(location);
 105  
         // validateClassName(className);
 106  0
         nullCheck(className);
 107  0
         nullCheck(kind);
 108  0
         if (!sourceOrClass.contains(kind))
 109  0
             throw new IllegalArgumentException("Invalid kind " + kind);
 110  0
         return (JavaFileObject)getFileForInput(location, "", externalizeFileName(className, kind));
 111  
     }
 112  
 
 113  
     private static <T> T nullCheck(T o) {
 114  0
         o.getClass(); // null check
 115  0
         return o;
 116  
     }
 117  
 
 118  
     private static String externalizeFileName(CharSequence name, JavaFileObject.Kind kind) {
 119  0
         String basename = name.toString().replace('.', File.separatorChar);
 120  0
         String suffix = kind == JavaFileObject.Kind.SOURCE ? 
 121  
             FX_SOURCE_SUFFIX : kind.extension;
 122  0
         return basename + suffix;
 123  
     }
 124  
 
 125  395
     private final Set<JavaFileObject.Kind> sourceOrClass =
 126  
         EnumSet.of(JavaFileObject.Kind.SOURCE, JavaFileObject.Kind.CLASS);
 127  
     
 128  
     private static class DelegateJavaFileObject extends BaseFileObject {
 129  
         JavaFileObject delegate;
 130  
         boolean isFXSourceFile;
 131  
         
 132  370
         DelegateJavaFileObject(JavaFileObject jfo) {
 133  370
             delegate = jfo;
 134  370
             isFXSourceFile = jfo.toString().endsWith(FX_SOURCE_SUFFIX);
 135  370
         }
 136  
 
 137  
         @Override
 138  
         public Kind getKind() {
 139  8
             return isFXSourceFile ? JavaFileObject.Kind.SOURCE : delegate.getKind();
 140  
         }
 141  
 
 142  
         @Override
 143  
         public boolean isNameCompatible(String cn, Kind kind) {
 144  678
             cn.getClass(); // null check
 145  678
             if (kind == Kind.OTHER && getKind() != kind)
 146  0
                 return false;
 147  678
             String suffix = (kind == JavaFileObject.Kind.SOURCE) ? ".fx" : kind.extension;
 148  678
             String n = cn + suffix;
 149  678
             if (delegate.getName().equals(n))
 150  0
                 return true;
 151  678
             if (getName().equalsIgnoreCase(n)) {
 152  
                 try {
 153  
                     // allow for Windows
 154  0
                     File f = new File(getPath());
 155  0
                     return (f.getCanonicalFile().getName().equals(n));
 156  0
                 } catch (IOException e) {
 157  
                 }
 158  
             }
 159  678
             return false;
 160  
         }
 161  
 
 162  
         @Override
 163  
         public NestingKind getNestingKind() {
 164  0
             return delegate.getNestingKind();
 165  
         }
 166  
 
 167  
         @Override
 168  
         public Modifier getAccessLevel() {
 169  0
             return delegate.getAccessLevel();
 170  
         }
 171  
 
 172  
         public URI toUri() {
 173  356
             return delegate.toUri();
 174  
         }
 175  
 
 176  
         public String getName() {
 177  1000
             return delegate.getName();
 178  
         }
 179  
 
 180  
         /** @deprecated see bug 6410637 */
 181  
         @Deprecated @Override
 182  
         public String getPath() {
 183  71
             return delegate instanceof BaseFileObject ? 
 184  
                 ((BaseFileObject)delegate).getPath() : getName();
 185  
         }
 186  
 
 187  
         public InputStream openInputStream() throws IOException {
 188  0
             return delegate.openInputStream();
 189  
         }
 190  
 
 191  
         public OutputStream openOutputStream() throws IOException {
 192  0
             return delegate.openOutputStream();
 193  
         }
 194  
 
 195  
         @Override
 196  
         public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
 197  0
             return delegate.openReader(ignoreEncodingErrors);
 198  
         }
 199  
 
 200  
         public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
 201  424
             return delegate.getCharContent(ignoreEncodingErrors);
 202  
         }
 203  
 
 204  
         public Writer openWriter() throws IOException {
 205  0
             return delegate.openWriter();
 206  
         }
 207  
 
 208  
         public long getLastModified() {
 209  0
             return delegate.getLastModified();
 210  
         }
 211  
 
 212  
         public boolean delete() {
 213  0
             return delegate.delete();
 214  
         }
 215  
         
 216  
         @Override
 217  
         public String toString() {
 218  1724
             return delegate.toString();
 219  
         }
 220  
     }
 221  
 }