| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 4 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 10 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
* contributor license agreements. See the NOTICE file distributed |
| 12 |
* with this work for additional information regarding copyright |
| 13 |
* ownership. The ASF licenses this file to you under the Apache |
| 14 |
* License, Version 2.0 (the "License"); you may not use this file |
| 15 |
* except in compliance with the License. You may obtain a copy of |
| 16 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
*/ |
| 18 |
|
| 19 |
package com.sun.star.script.framework.container; |
| 20 |
|
| 21 |
import com.sun.star.script.framework.io.UCBStreamHandler; |
| 22 |
import com.sun.star.script.framework.io.XInputStreamImpl; |
| 23 |
import com.sun.star.script.framework.log.LogUtils; |
| 24 |
import com.sun.star.script.framework.provider.PathUtils; |
| 25 |
|
| 26 |
import com.sun.star.ucb.XSimpleFileAccess2; |
| 27 |
|
| 28 |
import com.sun.star.uno.UnoRuntime; |
| 29 |
|
| 30 |
import java.io.ByteArrayInputStream; |
| 31 |
import java.io.InputStream; |
| 32 |
|
| 33 |
import java.net.URL; |
| 34 |
|
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.StringTokenizer; |
| 37 |
|
| 38 |
public class ScriptMetaData extends ScriptEntry { |
| 39 |
|
| 40 |
private boolean hasSource = false; |
| 41 |
private static final String locationPlaceHolder = ""; |
| 42 |
private String source; |
| 43 |
private final Parcel parent; |
| 44 |
|
| 45 |
|
| 46 |
public ScriptMetaData(Parcel parent, ScriptEntry entry, |
| 47 |
String source) { |
| 48 |
super(entry); |
| 49 |
this.parent = parent; |
| 50 |
|
| 51 |
if (source != null) { |
| 52 |
this.hasSource = true; |
| 53 |
this.source = source; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public boolean hasSource() { |
| 58 |
return hasSource; |
| 59 |
} |
| 60 |
|
| 61 |
public String getSource() { |
| 62 |
return (source != null && hasSource) ? source : null; |
| 63 |
} |
| 64 |
|
| 65 |
public byte[] getSourceBytes() { |
| 66 |
return (source != null && hasSource) ? source.getBytes() : null; |
| 67 |
} |
| 68 |
|
| 69 |
public String getScriptFullURL() { |
| 70 |
String url = "vnd.sun.star.script:" + parent.getName() + "." |
| 71 |
+ getLanguageName() + "?" + "language=" + getLanguage() |
| 72 |
+ "&location=" + getParcelLocation(); |
| 73 |
|
| 74 |
return url; |
| 75 |
} |
| 76 |
|
| 77 |
public String getShortFormScriptURL() { |
| 78 |
String url = "vnd.sun.star.script:" + parent.getName() + "." |
| 79 |
+ getLanguageName() + "?" + "language=" + getLanguage() |
| 80 |
+ "&location=" + getLocationPlaceHolder(); |
| 81 |
|
| 82 |
return url; |
| 83 |
} |
| 84 |
|
| 85 |
// TODO probably should be private should not be necessary |
| 86 |
// to be exposed at all |
| 87 |
|
| 88 |
private static final String SHARE = |
| 89 |
"vnd.sun.star.expand:$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR"; |
| 90 |
|
| 91 |
private static final String USER = |
| 92 |
"vnd.sun.star.expand:${$BRAND_INI_DIR/" + PathUtils.BOOTSTRAP_NAME |
| 93 |
+ "::UserInstallation}/user"; |
| 94 |
|
| 95 |
private static final String UNO_USER_PACKAGES1 = |
| 96 |
"vnd.sun.star.expand:$UNO_USER_PACKAGES_CACHE"; |
| 97 |
|
| 98 |
private static final String UNO_USER_PACKAGES2 = USER + "/uno_packages"; |
| 99 |
|
| 100 |
private static final String UNO_SHARED_PACKAGES1 = |
| 101 |
"$UNO_SHARED_PACKAGES_CACHE"; |
| 102 |
|
| 103 |
private static final String UNO_SHARED_PACKAGES2 = SHARE + "/uno_packages"; |
| 104 |
|
| 105 |
public static String getFileName(URL url) { |
| 106 |
String fileName = url.toExternalForm(); |
| 107 |
if (fileName.lastIndexOf(UCBStreamHandler.separator) != -1) { |
| 108 |
fileName = fileName.substring(0, fileName.lastIndexOf(UCBStreamHandler.separator)); |
| 109 |
fileName = fileName.substring(fileName.lastIndexOf("/") + 1); |
| 110 |
} |
| 111 |
return fileName; |
| 112 |
} |
| 113 |
|
| 114 |
public static String getLocationPlaceHolder(String url, String pkgname) { |
| 115 |
String result = "Unknown"; |
| 116 |
|
| 117 |
if (url.contains(UNO_USER_PACKAGES1) || |
| 118 |
url.contains(UNO_USER_PACKAGES2)) { |
| 119 |
result = PathUtils.make_url("user:uno_packages", pkgname); |
| 120 |
} else if (url.contains(UNO_SHARED_PACKAGES1) || |
| 121 |
url.contains(UNO_SHARED_PACKAGES2)) { |
| 122 |
result = PathUtils.make_url("share:uno_packages", pkgname); |
| 123 |
} else if (url.indexOf(SHARE) == 0) { |
| 124 |
result = "share"; |
| 125 |
} else if (url.indexOf(USER) == 0) { |
| 126 |
result = "user"; |
| 127 |
} else if (url.indexOf("vnd.sun.star.tdoc:") == 0) { |
| 128 |
result = "document"; |
| 129 |
} |
| 130 |
|
| 131 |
return result; |
| 132 |
} |
| 133 |
|
| 134 |
public String getLocationPlaceHolder() { |
| 135 |
String placeHolder = "Unknown"; |
| 136 |
String pathToParcel = parent.getPathToParcel(); |
| 137 |
|
| 138 |
if (pathToParcel.contains(UNO_USER_PACKAGES1) || |
| 139 |
pathToParcel.contains(UNO_USER_PACKAGES2)) { |
| 140 |
// it's a package |
| 141 |
placeHolder = "user:uno_packages"; |
| 142 |
String unoPkg = parent.parent.getName(); |
| 143 |
|
| 144 |
if (unoPkg != null) { |
| 145 |
placeHolder = PathUtils.make_url(placeHolder, unoPkg); |
| 146 |
} |
| 147 |
} else if (pathToParcel.contains(UNO_SHARED_PACKAGES1) || |
| 148 |
pathToParcel.contains(UNO_SHARED_PACKAGES2)) { |
| 149 |
//it's a package |
| 150 |
placeHolder = "share:uno_packages"; |
| 151 |
String unoPkg = parent.parent.getName(); |
| 152 |
|
| 153 |
if (unoPkg != null) { |
| 154 |
placeHolder = PathUtils.make_url(placeHolder, unoPkg); |
| 155 |
} |
| 156 |
} else if (pathToParcel.indexOf(SHARE) == 0) { |
| 157 |
placeHolder = "share"; |
| 158 |
} else if (pathToParcel.indexOf(USER) == 0) { |
| 159 |
placeHolder = "user"; |
| 160 |
} else if (pathToParcel.indexOf("vnd.sun.star.tdoc:") == 0) { |
| 161 |
placeHolder = "document"; |
| 162 |
} |
| 163 |
|
| 164 |
// TODO handling document packages ??? not really sure of package url |
| 165 |
/* else |
| 166 |
{ |
| 167 |
} */ |
| 168 |
return placeHolder; |
| 169 |
} |
| 170 |
|
| 171 |
// TODO probably should be private should not be necessary |
| 172 |
// to be exposed at all only used in lang providers at the moment |
| 173 |
// to generate URL for script, editors should use a model of script |
| 174 |
// source and not interact with the URL |
| 175 |
// Also if it is to remain needs to be renamed to getParcelLocationURL |
| 176 |
|
| 177 |
// return URL string to parcel |
| 178 |
public String getParcelLocation() { |
| 179 |
return parent.getPathToParcel(); |
| 180 |
} |
| 181 |
|
| 182 |
@Override |
| 183 |
public String toString() { |
| 184 |
return "\nParcelLocation = " + getParcelLocation() + "\nLocationPlaceHolder = " |
| 185 |
+ locationPlaceHolder + super.toString(); |
| 186 |
} |
| 187 |
|
| 188 |
public URL[] getClassPath() { |
| 189 |
try { |
| 190 |
String classpath = getLanguageProperties().get("classpath"); |
| 191 |
|
| 192 |
if (classpath == null) { |
| 193 |
classpath = ""; |
| 194 |
} |
| 195 |
|
| 196 |
String parcelPath = getParcelLocation(); |
| 197 |
|
| 198 |
// make sure path ends with / |
| 199 |
if (!parcelPath.endsWith("/")) { |
| 200 |
parcelPath += "/"; |
| 201 |
} |
| 202 |
|
| 203 |
// replace \ with / |
| 204 |
parcelPath = parcelPath.replace('\\', '/'); |
| 205 |
|
| 206 |
ArrayList<URL> classPathVec = new ArrayList<URL>(); |
| 207 |
StringTokenizer stk = new StringTokenizer(classpath, ":"); |
| 208 |
|
| 209 |
while (stk.hasMoreElements()) { |
| 210 |
String relativeClasspath = stk.nextToken(); |
| 211 |
String pathToProcess = PathUtils.make_url(parcelPath, relativeClasspath); |
| 212 |
URL url = createURL(pathToProcess); |
| 213 |
|
| 214 |
if (url != null) { |
| 215 |
classPathVec.add(url); |
| 216 |
} |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
if (classPathVec.isEmpty()) { |
| 221 |
URL url = createURL(parcelPath); |
| 222 |
|
| 223 |
if (url != null) { |
| 224 |
classPathVec.add(url); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
return classPathVec.toArray(new URL[classPathVec.size()]); |
| 229 |
} catch (Exception e) { |
| 230 |
LogUtils.DEBUG("Failed to build class path " + e.toString()); |
| 231 |
LogUtils.DEBUG(LogUtils.getTrace(e)); |
| 232 |
return new URL[0]; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
private URL createURL(String path) throws java.net.MalformedURLException { |
| 237 |
int indexOfColon = path.indexOf(':'); |
| 238 |
String scheme = path.substring(0, indexOfColon); |
| 239 |
UCBStreamHandler handler = new UCBStreamHandler(scheme, parent.m_xSFA); |
| 240 |
|
| 241 |
path += UCBStreamHandler.separator; |
| 242 |
return new URL(null, path, handler); |
| 243 |
} |
| 244 |
|
| 245 |
// TODO should decide whether this should throw or not |
| 246 |
// decide whether it should be public or protected ( final ? ) |
| 247 |
public void loadSource() { |
| 248 |
try { |
| 249 |
URL sourceUrl = getSourceURL(); |
| 250 |
|
| 251 |
LogUtils.DEBUG("** In load source BUT not loading yet for " |
| 252 |
+ sourceUrl); |
| 253 |
|
| 254 |
if (sourceUrl != null) { |
| 255 |
StringBuilder buf = new StringBuilder(); |
| 256 |
InputStream in = sourceUrl.openStream(); |
| 257 |
|
| 258 |
byte[] contents = new byte[1024]; |
| 259 |
int len; |
| 260 |
|
| 261 |
while ((len = in.read(contents, 0, 1024)) != -1) { |
| 262 |
buf.append(new String(contents, 0, len)); |
| 263 |
} |
| 264 |
|
| 265 |
try { |
| 266 |
in.close(); |
| 267 |
} catch (java.io.IOException ignore) { |
| 268 |
LogUtils.DEBUG("** Failed to read scriot from url " |
| 269 |
+ ignore.toString()); |
| 270 |
} |
| 271 |
|
| 272 |
source = buf.toString(); |
| 273 |
hasSource = true; |
| 274 |
} |
| 275 |
} catch (java.io.IOException e) { |
| 276 |
LogUtils.DEBUG("** Failed to read scriot from url " + e.toString()); |
| 277 |
} |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
protected boolean writeSourceFile() { |
| 282 |
String sourceFilePath = parent.getPathToParcel() + "/" + getLanguageName(); |
| 283 |
boolean result = false; |
| 284 |
|
| 285 |
try { |
| 286 |
|
| 287 |
XSimpleFileAccess2 xSFA2 = |
| 288 |
UnoRuntime.queryInterface(XSimpleFileAccess2.class, parent.m_xSFA); |
| 289 |
|
| 290 |
if (xSFA2 != null) { |
| 291 |
ByteArrayInputStream bis = new ByteArrayInputStream(getSourceBytes()); |
| 292 |
XInputStreamImpl xis = new XInputStreamImpl(bis); |
| 293 |
xSFA2.writeFile(sourceFilePath, xis); |
| 294 |
xis.closeInput(); |
| 295 |
result = true; |
| 296 |
} |
| 297 |
} |
| 298 |
// TODO re-examine exception processing should probably throw |
| 299 |
// exceptions back to caller |
| 300 |
catch (Exception ignore) { |
| 301 |
} |
| 302 |
|
| 303 |
return result; |
| 304 |
} |
| 305 |
|
| 306 |
protected boolean removeSourceFile() { |
| 307 |
String parcelLocation = parent.getPathToParcel(); |
| 308 |
String sourceFilePath = parcelLocation + "/" + getLanguageName(); |
| 309 |
boolean result = false; |
| 310 |
|
| 311 |
try { |
| 312 |
parent.m_xSFA.kill(sourceFilePath); |
| 313 |
result = true; |
| 314 |
} |
| 315 |
// TODO reexamine exception handling |
| 316 |
catch (Exception e) { |
| 317 |
} |
| 318 |
|
| 319 |
return result; |
| 320 |
} |
| 321 |
|
| 322 |
public URL getSourceURL() throws java.net.MalformedURLException { |
| 323 |
String sUrl = getParcelLocation(); |
| 324 |
sUrl = PathUtils.make_url(sUrl, getLanguageName()); |
| 325 |
LogUtils.DEBUG("Creating script url for " + sUrl); |
| 326 |
return createURL(sUrl); |
| 327 |
} |
| 328 |
} |