1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
|
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
package com.sun.star.script.framework.provider;
import com.sun.star.beans.Property;
import com.sun.star.beans.XIntrospectionAccess;
import com.sun.star.beans.XPropertyChangeListener;
import com.sun.star.beans.XPropertySet;
import com.sun.star.beans.XPropertySetInfo;
import com.sun.star.beans.XVetoableChangeListener;
import com.sun.star.container.XNameContainer;
import com.sun.star.deployment.XPackage;
import com.sun.star.document.XScriptInvocationContext;
import com.sun.star.frame.XModel;
import com.sun.star.frame.XTransientDocumentsDocumentContentFactory;
import com.sun.star.lang.XInitialization;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.lang.XTypeProvider;
import com.sun.star.script.XInvocation;
import com.sun.star.script.browse.BrowseNodeTypes;
import com.sun.star.script.browse.XBrowseNode;
import com.sun.star.script.framework.browse.DialogFactory;
import com.sun.star.script.framework.browse.ProviderBrowseNode;
import com.sun.star.script.framework.container.ParcelContainer;
import com.sun.star.script.framework.container.ParsedScriptUri;
import com.sun.star.script.framework.container.ScriptMetaData;
import com.sun.star.script.framework.container.UnoPkgContainer;
import com.sun.star.script.framework.container.XMLParserFactory;
import com.sun.star.script.framework.log.LogUtils;
import com.sun.star.script.provider.ScriptFrameworkErrorException;
import com.sun.star.script.provider.ScriptFrameworkErrorType;
import com.sun.star.script.provider.XScript;
import com.sun.star.script.provider.XScriptContext;
import com.sun.star.script.provider.XScriptProvider;
import com.sun.star.sdbc.XRow;
import com.sun.star.ucb.Command;
import com.sun.star.ucb.UniversalContentBroker;
import com.sun.star.ucb.XCommandProcessor;
import com.sun.star.ucb.XContent;
import com.sun.star.ucb.XContentIdentifier;
import com.sun.star.ucb.XUniversalContentBroker;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.Exception;
import com.sun.star.uno.Type;
import com.sun.star.uno.TypeClass;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XMacroExpander;
public abstract class ScriptProvider implements
XScriptProvider, XBrowseNode, XPropertySet, XInvocation, XInitialization,
XTypeProvider, XServiceInfo, XNameContainer {
private final String[] __serviceNames = {
"com.sun.star.script.provider.ScriptProviderFor",
"com.sun.star.script.provider.LanguageScriptProvider",
"com.sun.star.script.browse.BrowseNode",
"com.sun.star.script.provider.ScriptProvider"
};
protected String language;
protected XComponentContext m_xContext;
private XMultiComponentFactory m_xMultiComponentFactory;
protected XModel m_xModel;
protected XScriptInvocationContext m_xInvocContext;
private ParcelContainer m_container;
// proxies to helper objects which implement interfaces
private XPropertySet m_xPropertySetProxy;
private XInvocation m_xInvocationProxy;
// TODO should this be implemented in this class
private XBrowseNode m_xBrowseNodeProxy;
private XScriptContext m_xScriptContext;
public ScriptProvider(XComponentContext ctx, String language) {
this.language = language;
__serviceNames[0] += language;
LogUtils.DEBUG("ScriptProvider: constructor - start. " + language);
m_xContext = ctx;
// Initialize DialogFactory class in case dialogs are required
DialogFactory.createDialogFactory(m_xContext);
try {
m_xMultiComponentFactory = m_xContext.getServiceManager();
if (m_xMultiComponentFactory == null) {
throw new Exception("Error could not obtain a " +
"multicomponent factory - rethrowing Exception.");
}
Object serviceObj =
m_xContext.getValueByName(
"/singletons/com.sun.star.util.theMacroExpander");
XMacroExpander me =
(XMacroExpander) AnyConverter.toObject(
new Type(XMacroExpander.class), serviceObj);
XMLParserFactory.setOfficeDTDURL(
me.expandMacros(
"$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/dtd/officedocument/1_0/"));
} catch (Exception e) {
LogUtils.DEBUG(LogUtils.getTrace(e));
throw new com.sun.star.uno.RuntimeException(
e, "Error constructing ScriptProvider: " + e);
}
LogUtils.DEBUG("ScriptProvider: constructor - finished.");
}
synchronized public XScriptContext getScriptingContext() {
if (m_xScriptContext == null) {
m_xScriptContext = ScriptContext.createContext(m_xModel, m_xInvocContext,
m_xContext, m_xMultiComponentFactory);
}
return m_xScriptContext;
}
public void initialize(Object[] aArguments)
throws com.sun.star.uno.Exception {
LogUtils.DEBUG("entering XInit for language " + language);
boolean isPkgProvider = false;
if (aArguments.length == 1) {
String contextUrl = null;
if (AnyConverter.getType(aArguments[0]).getTypeClass().equals(
TypeClass.INTERFACE)) {
// try whether it denotes a XScriptInvocationContext
m_xInvocContext =
UnoRuntime.queryInterface(XScriptInvocationContext.class, aArguments[0]);
if (m_xInvocContext != null) {
// if so, obtain the document - by definition, this must be
// the ScriptContainer
m_xModel =
UnoRuntime.queryInterface(XModel.class, m_xInvocContext.getScriptContainer());
} else {
// otherwise, check whether it's an XModel
m_xModel =
UnoRuntime.queryInterface(XModel.class, aArguments[0]);
}
if (m_xModel == null) {
throw new com.sun.star.uno.Exception(
"ScriptProvider argument must be either a string, a valid XScriptInvocationContext, "
+ "or an XModel", this);
}
contextUrl = getDocUrlFromModel(m_xModel);
m_container = new ParcelContainer(m_xContext, contextUrl, language);
} else if (AnyConverter.isString(aArguments[0])) {
String originalContextURL = AnyConverter.toString(aArguments[0]);
LogUtils.DEBUG("creating Application, path: " + originalContextURL);
contextUrl = originalContextURL;
// TODO no support for packages in documents yet
if (originalContextURL.startsWith("vnd.sun.star.tdoc")) {
m_container = new ParcelContainer(m_xContext, contextUrl, language);
m_xModel = getModelFromDocUrl(originalContextURL);
} else {
String extensionDb = "vnd.sun.star.expand:${$BRAND_INI_DIR/"
+ PathUtils.BOOTSTRAP_NAME + "::UserInstallation}/user";
String extensionRepository = null;
if (originalContextURL.startsWith("bundled")) {
contextUrl = "vnd.sun.star.expand:$BUNDLED_EXTENSIONS";
extensionRepository = "bundled";
} else if (originalContextURL.startsWith("share")) {
contextUrl = "vnd.sun.star.expand:$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR";
extensionRepository = "shared";
} else if (originalContextURL.startsWith("user")) {
contextUrl = "vnd.sun.star.expand:${$BRAND_INI_DIR/"
+ PathUtils.BOOTSTRAP_NAME + "::UserInstallation}/user";
extensionRepository = "user";
}
if (originalContextURL.endsWith("uno_packages")) {
isPkgProvider = true;
if (!originalContextURL.equals(contextUrl)
&& extensionRepository != null
&& !extensionRepository.equals("bundled")) {
contextUrl = PathUtils.make_url(contextUrl, "uno_packages");
}
}
if (isPkgProvider) {
m_container =
new UnoPkgContainer(m_xContext, contextUrl, extensionDb, extensionRepository,
language);
} else {
m_container =
new ParcelContainer(m_xContext, contextUrl, language);
}
}
} else {
throw new com.sun.star.uno.RuntimeException(
"ScriptProvider created with invalid argument");
}
LogUtils.DEBUG("Modified Application path is: " + contextUrl);
LogUtils.DEBUG("isPkgProvider is: " + isPkgProvider);
// TODO should all be done in this class instead of
// delegation????
m_xBrowseNodeProxy =
new ProviderBrowseNode(this, m_container, m_xContext);
m_xInvocationProxy =
UnoRuntime.queryInterface(XInvocation.class, m_xBrowseNodeProxy);
m_xPropertySetProxy =
UnoRuntime.queryInterface(XPropertySet.class, m_xBrowseNodeProxy);
} else {
// this is ok, for example when executing a script from the
// command line
LogUtils.DEBUG("ScriptProviderFor" + language +
" initialized without a context");
}
LogUtils.DEBUG("leaving XInit");
}
/**
* Gets the types attribute of the ScriptProvider object
*
* @return The types value
*/
public com.sun.star.uno.Type[] getTypes() {
Type[] retValue = new Type[ 8 ];
retValue[ 0 ] = new Type(XScriptProvider.class);
retValue[ 1 ] = new Type(XBrowseNode.class);
retValue[ 2 ] = new Type(XInitialization.class);
retValue[ 3 ] = new Type(XTypeProvider.class);
retValue[ 4 ] = new Type(XServiceInfo.class);
retValue[ 5 ] = new Type(XPropertySet.class);
retValue[ 6 ] = new Type(XInvocation.class);
retValue[ 7 ] = new Type(com.sun.star.container.XNameContainer.class);
return retValue;
}
/**
* Gets the implementationId attribute of the ScriptProvider object
*
* @return The implementationId value
*/
public byte[] getImplementationId() {
return new byte[0];
}
/**
* Gets the implementationName attribute of the ScriptProvider object
*
* @return The implementationName value
*/
public String getImplementationName() {
return getClass().getName();
}
/**
* Description of the Method
*
* @param serviceName Description of the Parameter
* @return Description of the Return Value
*/
public boolean supportsService(String serviceName) {
for (int index = __serviceNames.length; index-- > 0;) {
if (serviceName.equals(__serviceNames[ index ])) {
return true;
}
}
return false;
}
/**
* Gets the supportedServiceNames attribute of the ScriptProvider object
*
* @return The supportedServiceNames value
*/
public String[] getSupportedServiceNames() {
return __serviceNames;
}
public abstract XScript getScript(/*IN*/String scriptURI) throws
com.sun.star.uno.RuntimeException, ScriptFrameworkErrorException;
// TODO need to encapsulate this better,
// Some factory concept for creating/accessing Editor
// How this is passed down or how it is accessible by BrowseNode
// implementations needs thinking about
// This method is used to determine whether the ScriptProvider
// has a ScriptEditor
public abstract boolean hasScriptEditor();
// TODO see above
// This method is used to get the ScriptEditor for this ScriptProvider
public abstract ScriptEditor getScriptEditor();
public ScriptMetaData getScriptData(/*IN*/String scriptURI) throws
ScriptFrameworkErrorException {
try {
ParsedScriptUri details = m_container.parseScriptUri(scriptURI);
try {
ScriptMetaData scriptData = m_container.findScript(details);
if (scriptData == null) {
throw new ScriptFrameworkErrorException(
details.function + " does not exist", null, details.function, language,
ScriptFrameworkErrorType.NO_SUCH_SCRIPT);
}
return scriptData;
} catch (com.sun.star.container.NoSuchElementException nse) {
ScriptFrameworkErrorException e2 =
new ScriptFrameworkErrorException(nse.getMessage(), null, details.function,
language, ScriptFrameworkErrorType.NO_SUCH_SCRIPT);
e2.initCause(nse);
throw e2;
} catch (com.sun.star.lang.WrappedTargetException wta) {
// TODO specify the correct error Type
java.lang.Exception wrapped = (java.lang.Exception) wta.TargetException;
String message = wta.getMessage();
if (wrapped != null) {
message = wrapped.getMessage();
}
ScriptFrameworkErrorException e2 =
new ScriptFrameworkErrorException(message, null, details.function, language,
ScriptFrameworkErrorType.UNKNOWN);
e2.initCause(wta);
throw e2;
}
} catch (com.sun.star.lang.IllegalArgumentException ila) {
// TODO specify the correct error Type
ScriptFrameworkErrorException e2
= new ScriptFrameworkErrorException(
ila.getMessage(), null, scriptURI, language,
ScriptFrameworkErrorType.UNKNOWN);
e2.initCause(ila);
throw e2;
}
}
// Implementation of XBrowseNode interface
public String getName() {
return language;
}
public XBrowseNode[] getChildNodes() {
if (m_xBrowseNodeProxy == null) {
LogUtils.DEBUG("No Nodes available ");
return new XBrowseNode[0];
}
return m_xBrowseNodeProxy .getChildNodes();
}
public boolean hasChildNodes() {
if (m_xBrowseNodeProxy == null) {
LogUtils.DEBUG("No Nodes available ");
return false;
}
return m_xBrowseNodeProxy.hasChildNodes();
}
public short getType() {
return BrowseNodeTypes.CONTAINER;
}
@Override
public String toString() {
return getName();
}
// implementation of XInvocation interface
public XIntrospectionAccess getIntrospection() {
return m_xInvocationProxy.getIntrospection();
}
public Object invoke(String aFunctionName, Object[] aParams,
short[][] aOutParamIndex, Object[][] aOutParam) throws
com.sun.star.lang.IllegalArgumentException,
com.sun.star.script.CannotConvertException,
com.sun.star.reflection.InvocationTargetException {
return m_xInvocationProxy.invoke(
aFunctionName, aParams, aOutParamIndex, aOutParam);
}
public void setValue(String aPropertyName, Object aValue) throws
com.sun.star.beans.UnknownPropertyException,
com.sun.star.script.CannotConvertException,
com.sun.star.reflection.InvocationTargetException {
m_xInvocationProxy.setValue(aPropertyName, aValue);
}
public Object getValue(String aPropertyName) throws
com.sun.star.beans.UnknownPropertyException {
return m_xInvocationProxy.getValue(aPropertyName);
}
public boolean hasMethod(String aName) {
return m_xInvocationProxy.hasMethod(aName);
}
public boolean hasProperty(String aName) {
return m_xInvocationProxy.hasProperty(aName);
}
public XPropertySetInfo getPropertySetInfo() {
return m_xPropertySetProxy.getPropertySetInfo();
}
public void setPropertyValue(String aPropertyName, Object aValue) throws
com.sun.star.beans.UnknownPropertyException,
com.sun.star.beans.PropertyVetoException,
com.sun.star.lang.IllegalArgumentException,
com.sun.star.lang.WrappedTargetException {
m_xPropertySetProxy.setPropertyValue(aPropertyName, aValue);
}
public Object getPropertyValue(String PropertyName) throws
com.sun.star.beans.UnknownPropertyException,
com.sun.star.lang.WrappedTargetException {
return m_xPropertySetProxy.getPropertyValue(PropertyName);
}
public void addPropertyChangeListener(String aPropertyName,
XPropertyChangeListener xListener) throws
com.sun.star.beans.UnknownPropertyException,
com.sun.star.lang.WrappedTargetException {
m_xPropertySetProxy.addPropertyChangeListener(aPropertyName, xListener);
}
public void removePropertyChangeListener(
String aPropertyName, XPropertyChangeListener aListener) throws
com.sun.star.beans.UnknownPropertyException,
com.sun.star.lang.WrappedTargetException {
m_xPropertySetProxy.removePropertyChangeListener(
aPropertyName, aListener);
}
public void addVetoableChangeListener(
String PropertyName, XVetoableChangeListener aListener) throws
com.sun.star.beans.UnknownPropertyException,
com.sun.star.lang.WrappedTargetException {
m_xPropertySetProxy.addVetoableChangeListener(PropertyName, aListener);
}
public void removeVetoableChangeListener(String PropertyName,
XVetoableChangeListener aListener) throws
com.sun.star.beans.UnknownPropertyException,
com.sun.star.lang.WrappedTargetException {
m_xPropertySetProxy.removeVetoableChangeListener(
PropertyName, aListener);
}
public java.lang.Object getByName(String aName) throws
com.sun.star.container.NoSuchElementException,
com.sun.star.lang.WrappedTargetException {
// TODO needs implementing?
throw new com.sun.star.uno.RuntimeException("getByName not implemented");
}
public String[] getElementNames() {
// TODO needs implementing?
throw new com.sun.star.uno.RuntimeException("getElementNames not implemented");
}
// Performs the getRegStatus functionality for the PkgMgr
public boolean hasByName(String aName) {
return ((UnoPkgContainer)m_container).hasRegisteredUnoPkgContainer(aName);
}
public com.sun.star.uno.Type getElementType() {
// TODO at the moment this returns void indicating
// type is unknown should indicate XPackage ? do we implement XPackage
return new Type();
}
public boolean hasElements() {
// TODO needs implementing?
throw new com.sun.star.uno.RuntimeException("hasElements not implemented");
}
public void replaceByName(String aName, java.lang.Object aElement) throws
com.sun.star.lang.IllegalArgumentException,
com.sun.star.container.NoSuchElementException,
com.sun.star.lang.WrappedTargetException {
// TODO needs implementing
throw new com.sun.star.uno.RuntimeException("replaceByName not implemented");
}
public void insertByName(String aName, java.lang.Object aElement) throws
com.sun.star.lang.IllegalArgumentException,
com.sun.star.container.ElementExistException,
com.sun.star.lang.WrappedTargetException {
LogUtils.DEBUG("Provider for " + language + " received register for package "
+ aName);
XPackage newPackage = UnoRuntime.queryInterface(XPackage.class, aElement);
if (aName == null || aName.length() == 0) {
throw new com.sun.star.lang.IllegalArgumentException("Empty name");
}
if (newPackage == null) {
throw new com.sun.star.lang.IllegalArgumentException("No package supplied");
}
((UnoPkgContainer)m_container).processUnoPackage(newPackage, language);
}
// de-register for library only !!
public void removeByName(String Name) throws
com.sun.star.container.NoSuchElementException,
com.sun.star.lang.WrappedTargetException {
LogUtils.DEBUG("In ScriptProvider.removeByName() for " + Name
+ " this provider = " + language);
ParcelContainer c =
((UnoPkgContainer)m_container).getRegisteredUnoPkgContainer(
Name);
if (c != null) {
String libName;
if (Name.endsWith("/")) {
String tmp = Name.substring(0, Name.lastIndexOf('/'));
libName = tmp.substring(tmp.lastIndexOf('/') + 1);
} else {
libName = Name.substring(Name.lastIndexOf('/') + 1);
}
LogUtils.DEBUG("Deregistering library " + libName);
if (c.removeParcel(libName)) {
((UnoPkgContainer)m_container).deRegisterPackageContainer(Name);
} else {
throw new com.sun.star.container.NoSuchElementException(
libName + " cannot be removed from container.");
}
} else {
throw new com.sun.star.container.NoSuchElementException(
Name + " doesn't exist for " + language);
}
// TODO see if we want to remove the ParcelContainer is no Parcels/Libraries left
}
private String getDocUrlFromModel(XModel document) {
XTransientDocumentsDocumentContentFactory factory = null;
try {
factory = UnoRuntime.queryInterface(
XTransientDocumentsDocumentContentFactory.class,
m_xMultiComponentFactory.createInstanceWithContext(
"com.sun.star.frame.TransientDocumentsDocumentContentFactory",
m_xContext));
} catch (Exception ex) {
}
if (factory == null)
throw new com.sun.star.uno.RuntimeException(
"ScriptProvider: unable to create a TDOC context factory.", this);
try {
XContent content = factory.createDocumentContent(document);
return content.getIdentifier().getContentIdentifier();
} catch (com.sun.star.lang.IllegalArgumentException ex) {
}
LogUtils.DEBUG("unable to determine the model's TDOC URL");
return "";
}
private XModel getModelFromDocUrl(String docUrl) {
LogUtils.DEBUG("getModelFromDocUrl - searching for match for ->"
+ docUrl + "<-");
XModel xModel = null;
try {
XUniversalContentBroker ucb = UniversalContentBroker.create(m_xContext);
XContentIdentifier xCntId = ucb.createContentIdentifier(docUrl);
XContent xCnt = ucb.queryContent(xCntId);
XCommandProcessor xCmd =
UnoRuntime.queryInterface(XCommandProcessor.class, xCnt);
Property[] pArgs = new Property[ ] { new Property() };
pArgs[ 0 ].Name = "DocumentModel";
pArgs[ 0 ].Handle = -1;
Command command = new Command();
command.Handle = -1;
command.Name = "getPropertyValues";
command.Argument = pArgs;
com.sun.star.ucb.XCommandEnvironment env = null ;
Object result = xCmd.execute(command, 0, env) ;
XRow values = UnoRuntime.queryInterface(XRow.class, result);
xModel = UnoRuntime.queryInterface(XModel.class,
values.getObject(1, null));
} catch (Exception ignore) {
LogUtils.DEBUG("Failed to get model exception " + ignore);
}
return xModel;
}
}
|