001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library 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 * Contributors: 015 * bstefanescu 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.ecm.webengine.scripting; 021 022import java.io.File; 023import java.io.FileInputStream; 024import java.io.IOException; 025import java.io.InputStream; 026import java.net.MalformedURLException; 027import java.net.URI; 028import java.net.URL; 029 030import org.nuxeo.ecm.webengine.WebEngine; 031import org.nuxeo.runtime.api.Framework; 032 033/** 034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 035 */ 036public final class ScriptFile { 037 038 public static final String ROOT_PATH = Framework.getLocalService(WebEngine.class).getRootDirectory().getAbsolutePath(); 039 040 File file; 041 042 String ext = ""; 043 044 // TODO should remove the typed file name 045 public ScriptFile(File file) throws IOException { 046 String name = file.getName(); 047 int p = name.lastIndexOf('.'); 048 if (p > -1) { 049 ext = name.substring(p + 1); 050 } 051 this.file = file.getCanonicalFile(); 052 } 053 054 public boolean isTemplate() { 055 return "ftl".equals(ext); 056 } 057 058 public File getFile() { 059 return file; 060 } 061 062 public String getExtension() { 063 return ext; 064 } 065 066 public String getAbsolutePath() { 067 return file.getAbsolutePath(); 068 } 069 070 public String getRelativePath() { 071 return file.getAbsolutePath().substring(ROOT_PATH.length()); 072 } 073 074 public String getFileName() { 075 return file.getName(); 076 } 077 078 public String getURL() throws MalformedURLException { 079 return file.toURI().toURL().toExternalForm(); 080 } 081 082 public URL toURL() throws MalformedURLException { 083 return file.toURI().toURL(); 084 } 085 086 public URI toURI() { 087 return file.toURI(); 088 } 089 090 @Override 091 public String toString() { 092 return file.toString(); 093 } 094 095 public long lastModified() { 096 return file.lastModified(); 097 } 098 099 public InputStream getInputStream() throws IOException { 100 return new FileInputStream(file); 101 } 102 103}