001 /*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020 package org.crsh.lang.java;
021
022 import org.crsh.util.InputStreamFactory;
023
024 import javax.lang.model.element.Modifier;
025 import javax.lang.model.element.NestingKind;
026 import javax.tools.JavaFileObject;
027 import java.io.IOException;
028 import java.io.InputStream;
029 import java.io.OutputStream;
030 import java.io.Reader;
031 import java.io.Writer;
032 import java.net.URI;
033
034 /** @author Julien Viet */
035 class NodeJavaFileObject implements JavaFileObject {
036
037 /** . */
038 final String binaryName;
039
040 /** . */
041 private final URI uri;
042
043 /** . */
044 private final String name;
045
046 /** . */
047 private final long lastModified;
048
049 /** . */
050 private final InputStreamFactory stream;
051
052 public NodeJavaFileObject(String binaryName, URI uri, InputStreamFactory stream, long lastModified) {
053 this.uri = uri;
054 this.binaryName = binaryName;
055 this.name = uri.getPath() == null ? uri.getSchemeSpecificPart() : uri.getPath();
056 this.lastModified = lastModified;
057 this.stream = stream;
058 }
059
060 public URI toUri() {
061 return uri;
062 }
063
064 public String getName() {
065 return name;
066 }
067
068 public long getLastModified() {
069 return lastModified;
070 }
071
072 public InputStream openInputStream() throws IOException {
073 return stream.open();
074 }
075
076 public OutputStream openOutputStream() throws IOException {
077 throw new UnsupportedOperationException();
078 }
079
080 public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
081 throw new UnsupportedOperationException();
082 }
083
084 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
085 throw new UnsupportedOperationException();
086 }
087
088 public Writer openWriter() throws IOException {
089 throw new UnsupportedOperationException();
090 }
091
092 public boolean delete() {
093 return false;
094 }
095
096 public Kind getKind() {
097 return Kind.CLASS;
098 }
099
100 public boolean isNameCompatible(String simpleName, Kind kind) {
101 String baseName = simpleName + kind.extension;
102 return kind.equals(getKind())
103 && (baseName.equals(getName())
104 || getName().endsWith("/" + baseName));
105 }
106
107 public NestingKind getNestingKind() {
108 return null;
109 }
110
111 public Modifier getAccessLevel() {
112 return null;
113 }
114
115 @Override
116 public String toString() {
117 return getClass().getSimpleName() + "[uri=" + uri + "]";
118 }
119 }