|
| 1 | +/* |
| 2 | + * Copyright 2009 Scribble.org |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | +package org.scribble.protocol.validation.rules; |
| 18 | + |
| 19 | +import java.util.logging.Logger; |
| 20 | + |
| 21 | +import org.scribble.common.logging.Journal; |
| 22 | +import org.scribble.protocol.model.Activity; |
| 23 | +import org.scribble.protocol.model.Block; |
| 24 | +import org.scribble.protocol.model.ModelObject; |
| 25 | +import org.scribble.protocol.model.Protocol; |
| 26 | +import org.scribble.protocol.validation.ProtocolComponentValidatorRule; |
| 27 | + |
| 28 | +/** |
| 29 | + * This class provides the validation util for the Declaration |
| 30 | + * model component. |
| 31 | + * |
| 32 | + */ |
| 33 | +public class DeclarationValidatorUtil { |
| 34 | + |
| 35 | + private static final Logger _log=Logger.getLogger(DeclarationValidatorUtil.class.getName()); |
| 36 | + |
| 37 | + /** |
| 38 | + * This method validates the supplied model object. |
| 39 | + * |
| 40 | + * @param obj The model object being validated |
| 41 | + * @param logger The logger |
| 42 | + */ |
| 43 | + public static void validate(ModelObject obj, |
| 44 | + Journal logger) { |
| 45 | + |
| 46 | + /* |
| 47 | + * TODO: Change implementation so that the enclosing protocol is |
| 48 | + * processed to build up the declarations within the scope, |
| 49 | + * and when the current declaration is checked, it should see |
| 50 | + * whether the name is already in the declarations. |
| 51 | + * |
| 52 | + |
| 53 | + // Check name has been defined |
| 54 | + if (elem.getName() != null && |
| 55 | + elem.getName().trim().length() >= 0) { |
| 56 | + |
| 57 | + // Check all grouping constructs containing the |
| 58 | + // declaration, to ensure that only one declaration |
| 59 | + // exists with this name, in the scope |
| 60 | + Block b=null; |
| 61 | + Activity cur=elem; |
| 62 | + |
| 63 | + if (cur.getParent() instanceof Block) { |
| 64 | + b = (Block)elem.getParent(); |
| 65 | + } |
| 66 | + |
| 67 | + while (cur != null && b != null) { |
| 68 | + int index=b.indexOf(cur); |
| 69 | + |
| 70 | + if (index == -1) { |
| 71 | + _log.warning("Declaration not part of parent block"); |
| 72 | + cur = null; |
| 73 | + } else { |
| 74 | + for (int i=0; i < index; i++) { |
| 75 | + ModelObject mo=b.get(i); |
| 76 | + |
| 77 | + if (mo instanceof Declaration) { |
| 78 | + Declaration decl=(Declaration)mo; |
| 79 | + |
| 80 | + if (decl.getName() != null && |
| 81 | + elem.getName().equals(decl.getName())) { |
| 82 | + logger.error(org.scribble.common.util.MessageUtil.format( |
| 83 | + java.util.PropertyResourceBundle.getBundle( |
| 84 | + "org.scribble.protocol.validation.rules.Messages"), |
| 85 | + "_EXISTING_DECLARATION", |
| 86 | + new String[]{elem.getName()}), |
| 87 | + obj.getProperties()); |
| 88 | + |
| 89 | + // Quit loop |
| 90 | + cur = null; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (cur != null) { |
| 96 | + |
| 97 | + if (b.getParent() instanceof Activity) { |
| 98 | + cur = (Activity)b.getParent(); |
| 99 | + |
| 100 | + // Only continue if the current object |
| 101 | + // is not a definition, and it is contained |
| 102 | + // inside a block |
| 103 | + if ((cur instanceof Protocol) == false && |
| 104 | + cur.getParent() instanceof Block) { |
| 105 | + b = (Block)cur.getParent(); |
| 106 | + } else { |
| 107 | + b = null; |
| 108 | + } |
| 109 | + } else { |
| 110 | + b = null; |
| 111 | + cur = null; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + */ |
| 118 | + } |
| 119 | +} |
0 commit comments