KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > expressions > ExpressionConverter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.core.expressions;
12
13 import org.w3c.dom.Element JavaDoc;
14 import org.w3c.dom.Node JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21
22 import org.eclipse.core.internal.expressions.CompositeExpression;
23 import org.eclipse.core.internal.expressions.ExpressionMessages;
24 import org.eclipse.core.internal.expressions.ExpressionPlugin;
25 import org.eclipse.core.internal.expressions.Messages;
26
27 /**
28  * An expression converter converts an XML expression represented by an
29  * {@link IConfigurationElement} or {@link Element} (DOM) subtree into a
30  * corresponding expression tree.
31  *
32  * <p>
33  * An expression converter manages a list of {@link ElementHandler}s. Element
34  * handlers are responsible to do the actual conversion. The element handlers
35  * build a chain of responsibility.
36  * </p>
37  *
38  * @since 3.0
39  */

40 public final class ExpressionConverter {
41     
42     private ElementHandler[] fHandlers;
43     private static final ExpressionConverter INSTANCE= new ExpressionConverter(
44         new ElementHandler[] { ElementHandler.getDefault() } );
45     
46     /**
47      * Returns the default expression converter. The default expression converter
48      * can cope with all expression elements defined by the common expression
49      * language.
50      *
51      * @return the default expression converter
52      */

53     public static ExpressionConverter getDefault() {
54         return INSTANCE;
55     }
56     
57     /**
58      * Creates a new expression converter with the given list of element
59      * handlers. The element handlers build a chain of responsibility
60      * meaning that the first handler in the list is first used to
61      * convert the configuration element. If this handler isn't able
62      * to convert the configuration element the next handler in the
63      * array is used.
64      *
65      * @param handlers the array of element handlers
66      */

67     public ExpressionConverter(ElementHandler[] handlers) {
68         Assert.isNotNull(handlers);
69         fHandlers= handlers;
70     }
71     
72     /**
73      * Converts the tree of configuration elements represented by the given
74      * root element and returns a corresponding expression tree.
75      *
76      * @param root the configuration element to be converted
77      *
78      * @return the corresponding expression tree or <code>null</code>
79      * if the configuration element cannot be converted
80      *
81      * @throws CoreException if the configuration element can't be
82      * converted. Reasons include: (a) no handler is available to
83      * cope with a certain configuration element or (b) the XML
84      * expression tree is malformed.
85      */

86     public Expression perform(IConfigurationElement root) throws CoreException {
87         for (int i= 0; i < fHandlers.length; i++) {
88             ElementHandler handler= fHandlers[i];
89             Expression result= handler.create(this, root);
90             if (result != null)
91                 return result;
92         }
93         return null;
94     }
95
96     /**
97      * Converts the tree of DOM elements represented by the given
98      * root element and returns a corresponding expression tree.
99      *
100      * @param root the element to be converted
101      *
102      * @return the corresponding expression tree or <code>null</code>
103      * if the element cannot be converted
104      *
105      * @throws CoreException if the element can't be converted.
106      * Reasons include: (a) no handler is available to cope with
107      * a certain element or (b) the XML expression tree is malformed.
108      *
109      * @since 3.3
110      */

111     public Expression perform(Element JavaDoc root) throws CoreException {
112         for (int i= 0; i < fHandlers.length; i++) {
113             ElementHandler handler= fHandlers[i];
114             Expression result= handler.create(this, root);
115             if (result != null)
116                 return result;
117         }
118         return null;
119     }
120
121     /* package */ void processChildren(IConfigurationElement element, CompositeExpression result) throws CoreException {
122         IConfigurationElement[] children= element.getChildren();
123         if (children != null) {
124             for (int i= 0; i < children.length; i++) {
125                 Expression child= perform(children[i]);
126                 if (child == null)
127                     throw new CoreException(new Status(IStatus.ERROR, ExpressionPlugin.getPluginId(),
128                         IStatus.ERROR,
129                         Messages.format(
130                             ExpressionMessages.Expression_unknown_element,
131                             children[i].getName()),
132                         null));
133                 result.add(child);
134             }
135         }
136     }
137
138     /* package */ void processChildren(Element JavaDoc element, CompositeExpression result) throws CoreException {
139         Node JavaDoc child = element.getFirstChild();
140         while (child != null) {
141             if (child.getNodeType() == Node.ELEMENT_NODE) {
142                 Expression exp= perform((Element JavaDoc)child);
143                 if (exp == null)
144                     throw new CoreException(new Status(IStatus.ERROR, ExpressionPlugin.getPluginId(),
145                         IStatus.ERROR,
146                         Messages.format(
147                             ExpressionMessages.Expression_unknown_element,
148                             child.getNodeName()),
149                         null));
150                 result.add(exp);
151             }
152             child = child.getNextSibling();
153         }
154     }
155 }
Popular Tags