1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.myfaces.trinidad.change;
20
21 import org.w3c.dom.Element;
22 import org.w3c.dom.NamedNodeMap;
23 import org.w3c.dom.Node;
24 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
25
26 /**
27 * Change specialization for change in attributes.
28 * While applying this Change, the specified attribute state is restored.
29 */
30 public class AttributeDocumentChange implements DocumentChange
31 {
32 /**
33 * Constructs an AttributeChange with the given attributeName and
34 * attributeValue.
35 * @param attributeName The name of the attribute for which the value needs
36 * to be restored.
37 * @param attributeValueString The value of the attribute that needs to be restored.
38 * @throws IllegalArgumentException if specified attributeName were to be null.
39 * =-= bts TODO Figure out what to do about non String values
40 */
41 public AttributeDocumentChange(
42 String attributeName,
43 String attributeValueString)
44 {
45 if ((attributeName == null) || (attributeName.length() == 0))
46 throw new IllegalArgumentException(_LOG.getMessage(
47 "CANNOT_CONSTRUCT_ATTRIBUTECHANGE_WITH_NULL_NAME"));
48
49 _attributeName = attributeName;
50 _attributeValueString = attributeValueString;
51 }
52
53 /**
54 * Returns the name of the attribute that represents this Change.
55 */
56 public String getAttributeName()
57 {
58 return _attributeName;
59 }
60
61 /**
62 * Returns the value of the attribute corresponding to this AttributeChange.
63 */
64 public Object getAttributeStringValue()
65 {
66 return _attributeValueString;
67 }
68
69 /**
70 * {@inheritDoc}
71 */
72 public void changeDocument(Node componentNode)
73 {
74 NamedNodeMap attributes = componentNode.getAttributes();
75
76 if (attributes != null)
77 {
78 // remove the attribute
79 if (_attributeValueString == null)
80 attributes.removeNamedItem(_attributeName);
81 else
82 {
83 ((Element)componentNode).setAttribute(_attributeName,
84 _attributeValueString);
85 }
86 }
87 }
88
89 /**
90 * Returns true if adding the DocumentChange should force the JSP Document
91 * to reload
92 */
93 public boolean getForcesDocumentReload()
94 {
95 return false;
96 }
97
98
99 private final String _attributeName;
100 private final String _attributeValueString;
101 private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
102 AttributeDocumentChange.class);
103 }