dew/src/main/java/org/apidesign/bck2brwsr/dew/BaseFileObject.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 03 Oct 2013 10:02:13 +0200
changeset 1326 8ae6a6c42b5f
parent 1324 263482b074e9
permissions -rw-r--r--
Fixing license
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 
    19 package org.apidesign.bck2brwsr.dew;
    20 
    21 import java.net.URI;
    22 import javax.lang.model.element.Modifier;
    23 import javax.lang.model.element.NestingKind;
    24 
    25 /**
    26  *
    27  * @author Tomas Zezula
    28  */
    29 public abstract class BaseFileObject implements InferableJavaFileObject {
    30 
    31     protected final String path;
    32     protected final Kind kind;
    33 
    34     BaseFileObject(
    35         String path,
    36         Kind kind) {
    37         if (!path.startsWith("/")) {    //NOI18N
    38             throw new IllegalArgumentException();
    39         }
    40         this.path = path;
    41         this.kind = kind;
    42     }
    43 
    44 
    45     @Override
    46     public String infer() {
    47         return ClassLoaderFileManager.convertResourceToFQN(path);
    48     }
    49 
    50     @Override
    51     public Kind getKind() {
    52         return kind;
    53     }
    54 
    55     @Override
    56     public boolean isNameCompatible(String simpleName, Kind kind) {
    57         return this.kind == kind &&
    58         getSimpleName(path).equals(simpleName);
    59     }
    60 
    61     @Override
    62     public NestingKind getNestingKind() {
    63         return null;
    64     }
    65 
    66     @Override
    67     public Modifier getAccessLevel() {
    68         return null;
    69     }
    70 
    71     @Override
    72     public URI toUri() {
    73         return URI.create(escape(path));
    74     }
    75 
    76     @Override
    77     public String getName() {
    78         return path;
    79     }
    80 
    81 
    82 
    83     protected static String getSimpleName(String path) {
    84         int slashIndex = path.lastIndexOf('/');
    85         assert slashIndex >= 0;
    86         return (slashIndex + 1 < path.length()) ?
    87             path.substring(slashIndex + 1) :
    88             ""; //NOI18N
    89     }
    90 
    91     protected static Kind getKind(final String path) {
    92         final String simpleName = getSimpleName(path);
    93         final int dotIndex = simpleName.lastIndexOf('.'); //NOI18N
    94         final String ext = dotIndex > 0 ?
    95             simpleName.substring(dotIndex) :
    96             "";
    97         for (Kind k : Kind.values()) {
    98             if (k.extension.equals(ext)) {
    99                 return k;
   100             }
   101         }
   102         return Kind.OTHER;
   103     }
   104 
   105     private String escape(String path) {
   106         return path;
   107     }
   108 
   109 
   110 }