KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > dynamic > ValidationHandler


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.dynamic;
12
13 import java.util.Map JavaDoc;
14
15 import org.eclipse.help.ITocContribution;
16 import org.eclipse.help.internal.HelpPlugin;
17 import org.eclipse.help.internal.UAElement;
18
19 /*
20  * A handler that ensures that specified attributes are there for certain nodes,
21  * or removes the node and logs an error. Also logs warnings for deprecated elements.
22  */

23 public class ValidationHandler extends ProcessorHandler {
24
25     private Map JavaDoc requiredAttributes;
26     private Map JavaDoc deprecatedElements;
27     
28     /*
29      * Creates a new validator that looks for the given mapping of
30      * element names to required attribute names.
31      */

32     public ValidationHandler(Map JavaDoc requiredAttributes) {
33         this(requiredAttributes, null);
34     }
35
36     /*
37      * Creates a new validator that looks for the given mapping of
38      * element names to required attribute names, as well as a mapping
39      * of deprecated element names to suggested new element names.
40      */

41     public ValidationHandler(Map JavaDoc requiredAttributes, Map JavaDoc deprecatedElements) {
42         this.requiredAttributes = requiredAttributes;
43         this.deprecatedElements = deprecatedElements;
44     }
45
46     public short handle(UAElement element, String JavaDoc id) {
47         if (deprecatedElements != null) {
48             String JavaDoc suggestion = (String JavaDoc)deprecatedElements.get(element.element.getNodeName());
49             if (suggestion != null) {
50                 String JavaDoc msg = "The \"" + element.element.getNodeName() + "\" element is deprecated in \"" + id + "\"; use \"" + suggestion + "\" instead."; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
51
HelpPlugin.logWarning(msg);
52             }
53         }
54         String JavaDoc[] attributes = (String JavaDoc[])requiredAttributes.get(element.element.getNodeName());
55         if (attributes != null) {
56             for (int i=0;i<attributes.length;++i) {
57                 if (element.getAttribute(attributes[i]) == null) {
58                     String JavaDoc msg = "Required attribute \"" + attributes[i] + "\" missing from \"" + element.element.getNodeName() + "\" element"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
59
if (id != null) {
60                         msg += " in \"" + id + '"'; //$NON-NLS-1$
61
}
62                     UAElement parent = element.getParentElement();
63                     if (parent != null && !(parent instanceof ITocContribution)) {
64                         msg += " (skipping element)"; //$NON-NLS-1$
65
parent.removeChild(element);
66                         HelpPlugin.logError(msg);
67                         return HANDLED_SKIP;
68                     }
69                     else {
70                         throw new IllegalArgumentException JavaDoc(msg);
71                     }
72                 }
73             }
74         }
75         return UNHANDLED;
76     }
77 }
78
Popular Tags