Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
CustomAction |
|
| 3.0;3 |
1 | /* |
|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
3 | * contributor license agreements. See the NOTICE file distributed with |
|
4 | * this work for additional information regarding copyright ownership. |
|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
6 | * (the "License"); you may not use this file except in compliance with |
|
7 | * the License. You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
16 | */ |
|
17 | package org.apache.commons.scxml.model; |
|
18 | ||
19 | import org.apache.commons.logging.LogFactory; |
|
20 | import org.apache.commons.scxml.SCXMLHelper; |
|
21 | ||
22 | /** |
|
23 | * A custom action is simply a tuple consisting of a namespace URI, |
|
24 | * the local name for the custom action and the corresponding |
|
25 | * {@link Action} class. |
|
26 | * |
|
27 | */ |
|
28 | public class CustomAction { |
|
29 | ||
30 | /** |
|
31 | * Error logged while attempting to define a custom action |
|
32 | * in a null or empty namespace. |
|
33 | */ |
|
34 | private static final String ERR_NO_NAMESPACE = |
|
35 | "Cannot define a custom SCXML action with a null or empty namespace"; |
|
36 | ||
37 | /** |
|
38 | * The SCXML namespace, to which custom actions may not be added. |
|
39 | */ |
|
40 | private static final String NAMESPACE_SCXML = |
|
41 | "http://www.w3.org/2005/07/scxml"; |
|
42 | ||
43 | /** |
|
44 | * Error logged while attempting to define a custom action |
|
45 | * with the SCXML namespace. |
|
46 | */ |
|
47 | private static final String ERR_RESERVED_NAMESPACE = |
|
48 | "Cannot define a custom SCXML action within the SCXML namespace '" |
|
49 | + NAMESPACE_SCXML + "'"; |
|
50 | ||
51 | /** |
|
52 | * Error logged while attempting to define a custom action |
|
53 | * in a null or empty local name. |
|
54 | */ |
|
55 | private static final String ERR_NO_LOCAL_NAME = |
|
56 | "Cannot define a custom SCXML action with a null or empty local name"; |
|
57 | ||
58 | /** |
|
59 | * Error logged while attempting to define a custom action |
|
60 | * which does not extend {@link Action}. |
|
61 | */ |
|
62 | private static final String ERR_NOT_AN_ACTION = |
|
63 | "Custom SCXML action does not extend Action superclass"; |
|
64 | ||
65 | /** |
|
66 | * The namespace this custom action belongs to. |
|
67 | */ |
|
68 | private String namespaceURI; |
|
69 | ||
70 | /** |
|
71 | * The local name of the custom action. |
|
72 | */ |
|
73 | private String localName; |
|
74 | ||
75 | /** |
|
76 | * The implementation of this custom action. |
|
77 | */ |
|
78 | private Class actionClass; |
|
79 | ||
80 | /** |
|
81 | * The log for this custom action. |
|
82 | */ |
|
83 | private org.apache.commons.logging.Log log; |
|
84 | ||
85 | /** |
|
86 | * Constructor, if the namespace or local name is null or empty, |
|
87 | * or if the implementation is not an {@link Action}, an |
|
88 | * {@link IllegalArgumentException} is thrown. |
|
89 | * |
|
90 | * @param namespaceURI The namespace URI for this custom action. |
|
91 | * @param localName The local name for this custom action. |
|
92 | * @param actionClass The {@link Action} subclass implementing this |
|
93 | * custom action. |
|
94 | */ |
|
95 | public CustomAction(final String namespaceURI, final String localName, |
|
96 | 11 | final Class actionClass) { |
97 | 13 | this.log = LogFactory.getLog(CustomAction.class); |
98 | 11 | if (SCXMLHelper.isStringEmpty(namespaceURI)) { |
99 | 2 | log.error(ERR_NO_NAMESPACE); |
100 | 2 | throw new IllegalArgumentException(ERR_NO_NAMESPACE); |
101 | } |
|
102 | 9 | if (namespaceURI.trim().equalsIgnoreCase(NAMESPACE_SCXML)) { |
103 | 1 | log.error(ERR_RESERVED_NAMESPACE); |
104 | 1 | throw new IllegalArgumentException(ERR_RESERVED_NAMESPACE); |
105 | } |
|
106 | 8 | if (SCXMLHelper.isStringEmpty(localName)) { |
107 | 2 | log.error(ERR_NO_LOCAL_NAME); |
108 | 2 | throw new IllegalArgumentException(ERR_NO_LOCAL_NAME); |
109 | } |
|
110 | 6 | if (actionClass == null |
111 | || !Action.class.isAssignableFrom(actionClass)) { |
|
112 | 1 | log.error(ERR_NOT_AN_ACTION); |
113 | 1 | throw new IllegalArgumentException(ERR_NOT_AN_ACTION); |
114 | } |
|
115 | 5 | this.namespaceURI = namespaceURI; |
116 | 5 | this.localName = localName; |
117 | 5 | this.actionClass = actionClass; |
118 | 5 | } |
119 | ||
120 | /** |
|
121 | * Get this custom action's implementation. |
|
122 | * |
|
123 | * @return Returns the action class. |
|
124 | */ |
|
125 | public Class getActionClass() { |
|
126 | 25 | return actionClass; |
127 | } |
|
128 | ||
129 | /** |
|
130 | * Get the local name for this custom action. |
|
131 | * |
|
132 | * @return Returns the local name. |
|
133 | */ |
|
134 | public String getLocalName() { |
|
135 | 25 | return localName; |
136 | } |
|
137 | ||
138 | /** |
|
139 | * Get the namespace URI for this custom action. |
|
140 | * |
|
141 | * @return Returns the namespace URI. |
|
142 | */ |
|
143 | public String getNamespaceURI() { |
|
144 | 25 | return namespaceURI; |
145 | } |
|
146 | ||
147 | } |
|
148 |