001/* 002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.webengine.scripting; 023 024import java.io.File; 025import java.io.FileInputStream; 026import java.io.IOException; 027import java.io.InputStream; 028import java.net.MalformedURLException; 029import java.net.URI; 030import java.net.URL; 031 032import org.nuxeo.ecm.webengine.WebEngine; 033import org.nuxeo.runtime.api.Framework; 034 035/** 036 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 037 */ 038public final class ScriptFile { 039 040 public static final String ROOT_PATH = Framework.getService(WebEngine.class).getRootDirectory().getAbsolutePath(); 041 042 File file; 043 044 String ext = ""; 045 046 // TODO should remove the typed file name 047 public ScriptFile(File file) throws IOException { 048 String name = file.getName(); 049 int p = name.lastIndexOf('.'); 050 if (p > -1) { 051 ext = name.substring(p + 1); 052 } 053 this.file = file.getCanonicalFile(); 054 } 055 056 public boolean isTemplate() { 057 return "ftl".equals(ext); 058 } 059 060 public File getFile() { 061 return file; 062 } 063 064 public String getExtension() { 065 return ext; 066 } 067 068 public String getAbsolutePath() { 069 return file.getAbsolutePath(); 070 } 071 072 public String getRelativePath() { 073 return file.getAbsolutePath().substring(ROOT_PATH.length()); 074 } 075 076 public String getFileName() { 077 return file.getName(); 078 } 079 080 public String getURL() throws MalformedURLException { 081 return file.toURI().toURL().toExternalForm(); 082 } 083 084 public URL toURL() throws MalformedURLException { 085 return file.toURI().toURL(); 086 } 087 088 public URI toURI() { 089 return file.toURI(); 090 } 091 092 @Override 093 public String toString() { 094 return file.toString(); 095 } 096 097 public long lastModified() { 098 return file.lastModified(); 099 } 100 101 public InputStream getInputStream() throws IOException { 102 return new FileInputStream(file); 103 } 104 105}