KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > FilterableUAElement


1 /*******************************************************************************
2  * Copyright (c) 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.help.internal;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.help.internal.util.StringUtil;
17 import org.w3c.dom.Element JavaDoc;
18 import org.w3c.dom.Node JavaDoc;
19 import org.w3c.dom.NodeList JavaDoc;
20 import org.xml.sax.Attributes JavaDoc;
21
22 /**
23  * Any model element or node in any user assistance component that is filterable.
24  * Filters are generally specified in the XML as a filter attribute or child element,
25  * where the filter has a name and value, for example:
26  *
27  * <myUAElement filter="os=win32" myattribute="..." />
28  * <myUAElement filter="os!=linux" myattribute="..." />
29  *
30  * or
31  *
32  * <myUAElement myattribute="...">
33  * <filter name="os" value="linux" />
34  * <filter name="ws" value="!gtk" />
35  * </myUAElement>
36  *
37  * Filters must be stored in the model because they should only be processed when
38  * the content is about to be shown, because some of the filtering properties can change
39  * during a session (e.g. activities).
40  */

41 public abstract class FilterableUAElement {
42     
43     private Map JavaDoc filters;
44     
45     /**
46      * Adds the filters specified in the given attributes. This looks for
47      * the "filter" attribute and parses it.
48      *
49      * @param attrs the XML attributes for the element
50      */

51     public void addFilters(Attributes JavaDoc attrs) {
52         // add filter attribute if it exists
53
if (attrs != null) {
54             String JavaDoc filterAttribute = attrs.getValue("filter"); //$NON-NLS-1$
55
if (filterAttribute != null) {
56                 addFilter(filterAttribute);
57             }
58         }
59     }
60     
61     /**
62      * Adds all filters associated with the given element in the DOM. These can be defined
63      * as either a filter attribute on the element, or filter elements as children of this
64      * element.
65      *
66      * @param element the element whose filters to find
67      * @return a filter name to value mapping for all the filters on this element
68      */

69     public void addFilters(Element JavaDoc element) {
70         // check for filter attribute
71
String JavaDoc filterAttribute = element.getAttribute("filter"); //$NON-NLS-1$
72
if (filterAttribute.length() > 0) {
73             addFilter(filterAttribute);
74         }
75         // check for child filter elements
76
NodeList JavaDoc list = element.getChildNodes();
77         for (int i=0;i<list.getLength();++i) {
78             Node JavaDoc node = list.item(i);
79             if (node.getNodeType() == Node.ELEMENT_NODE && "filter".equals(node.getNodeName())) { //$NON-NLS-1$
80
Element JavaDoc elementNode = (Element JavaDoc)node;
81                 addFilter(elementNode.getAttribute("name"), elementNode.getAttribute("value")); //$NON-NLS-1$ //$NON-NLS-2$
82
}
83         }
84     }
85
86     /**
87      * Adds the filter specified by the given string containing both the
88      * filter name and value (e.g. "os=win32").
89      *
90      * @param nameAndValue the filter name and value
91      */

92     public void addFilter(String JavaDoc nameAndValue) {
93         boolean isPositive = (nameAndValue.indexOf("!=") == -1); //$NON-NLS-1$
94
// split at "=" or "!="
95
String JavaDoc[] tokens = StringUtil.split(nameAndValue, "!?="); //$NON-NLS-1$
96
String JavaDoc name = tokens[0];
97         String JavaDoc value = tokens[1];
98         if (!isPositive) {
99             value = '!' + value;
100         }
101         addFilter(name, value);
102     }
103
104     /**
105      * Adds the specified filter to this element, e.g. name="os", value="win32".
106      *
107      * @param name the filter name, e.g. "os"
108      * @param name the filter value, e.g. "win32"
109      */

110     public void addFilter(String JavaDoc name, String JavaDoc value) {
111         if (filters == null) {
112             filters = new HashMap JavaDoc();
113         }
114         filters.put(name, value);
115     }
116     
117     /**
118      * Returns all the filters on this element. This is a mapping of
119      * filter names (e.g. "os") to filter values (e.g. "win32").
120      *
121      * @return this element's filters
122      */

123     public Map JavaDoc getFilters() {
124         return filters;
125     }
126 }
127
Popular Tags