| 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 |
package com.sun.star.script.framework.provider.java; |
| 19 |
|
| 20 |
import java.util.ArrayList; |
| 21 |
import java.util.List; |
| 22 |
import java.util.StringTokenizer; |
| 23 |
|
| 24 |
/** |
| 25 |
* The <code>ScriptDescriptor</code> object is used to store the search |
| 26 |
* criteria that should be used for finding a particular script |
| 27 |
*/ |
| 28 |
public class ScriptDescriptor { |
| 29 |
|
| 30 |
private final String m_name; |
| 31 |
private final String m_methodName; |
| 32 |
private final String m_className; |
| 33 |
private List<String> m_classpath; |
| 34 |
private final ArrayList<Class<?>> m_argumentTypes = new ArrayList<Class<?>>(11); |
| 35 |
|
| 36 |
/** |
| 37 |
* Constructs a ScriptDescriptor for the given name |
| 38 |
* |
| 39 |
* @param name Script Name |
| 40 |
* @exception IllegalArgumentException if the given name does not contain a "." |
| 41 |
*/ |
| 42 |
public ScriptDescriptor(String name) throws IllegalArgumentException { |
| 43 |
int idx = name.lastIndexOf('.'); |
| 44 |
|
| 45 |
if (idx == -1) { |
| 46 |
throw new IllegalArgumentException("Invalid method name"); |
| 47 |
} |
| 48 |
|
| 49 |
this.m_name = name; |
| 50 |
this.m_methodName = name.substring(idx + 1); |
| 51 |
this.m_className = name.substring(0, idx); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Gets the fully qualified name of this <code>ScriptDescriptor</code> |
| 56 |
* |
| 57 |
* @return The Script Name value |
| 58 |
*/ |
| 59 |
public String getName() { |
| 60 |
return m_name; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Gets the fully qualified name of this <code>ScriptDescriptor</code> |
| 65 |
* |
| 66 |
* @return The Script Name value |
| 67 |
*/ |
| 68 |
public String getClassName() { |
| 69 |
return m_className; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Gets the method name of this <code>ScriptDescriptor</code> |
| 74 |
* |
| 75 |
* @return The methodName value |
| 76 |
*/ |
| 77 |
public String getMethodName() { |
| 78 |
return m_methodName; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Sets the classpath value stored by this <code>ScriptDescriptor</code> |
| 83 |
* |
| 84 |
* @param classpath The new classpath value |
| 85 |
*/ |
| 86 |
public void setClasspath(String classpath) { |
| 87 |
StringTokenizer stk = new StringTokenizer(classpath, ":"); |
| 88 |
|
| 89 |
while (stk.hasMoreTokens()) { |
| 90 |
this.m_classpath.add(stk.nextToken()); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Sets the classpath value stored by this <code>ScriptDescriptor</code> |
| 96 |
* |
| 97 |
* @param classpath The new classpath value |
| 98 |
*/ |
| 99 |
public void setClasspath(List<String> classpath) { |
| 100 |
this.m_classpath = classpath; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Gets the classpath value stored by this <code>ScriptDescriptor</code> |
| 105 |
* |
| 106 |
* @return The classpath value |
| 107 |
*/ |
| 108 |
public List<String> getClasspath() { |
| 109 |
return m_classpath; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Adds the given <code>Class</code> to the list of argument types stored in |
| 114 |
* this ScriptDescriptor |
| 115 |
* |
| 116 |
* @param clazz The feature to be added to the ArgumentType attribute |
| 117 |
*/ |
| 118 |
|
| 119 |
public synchronized void addArgumentType(Class<?> clazz) { |
| 120 |
m_argumentTypes.add(clazz); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Gets a list of the types of the arguments stored in this |
| 125 |
* <code>ScriptDescriptor</code> |
| 126 |
* |
| 127 |
* return the argument types as an array of Class |
| 128 |
* |
| 129 |
* @return The argumentTypes value |
| 130 |
*/ |
| 131 |
|
| 132 |
public synchronized Class<?>[] getArgumentTypes() { |
| 133 |
if (m_argumentTypes.size() > 0) |
| 134 |
return m_argumentTypes.toArray(new Class[ m_argumentTypes.size() ]); |
| 135 |
else |
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Returns a <code>String</code> representation of this |
| 141 |
* <code>ScriptDescriptor</code> |
| 142 |
* |
| 143 |
* @return The scriptName including the parameters. |
| 144 |
*/ |
| 145 |
@Override |
| 146 |
public String toString() { |
| 147 |
StringBuilder description = new StringBuilder(m_name); |
| 148 |
Class<?>[] types = getArgumentTypes(); |
| 149 |
|
| 150 |
description.append(" ("); |
| 151 |
|
| 152 |
if (types != null) { |
| 153 |
for (int i = 0; i < types.length - 1; i++) { |
| 154 |
description.append(types[ i ].getName()); |
| 155 |
description.append(", "); |
| 156 |
} |
| 157 |
|
| 158 |
description.append(types[ types.length - 1 ].getName()); |
| 159 |
} |
| 160 |
|
| 161 |
description.append(")"); |
| 162 |
|
| 163 |
return description.toString(); |
| 164 |
} |
| 165 |
} |