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.Document;
22 import org.w3c.dom.DocumentFragment;
23 import org.w3c.dom.Node;
24 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
25
26 /**
27 * Base class for specialized DocumentChanges that when applied will add a component
28 * instance to the component tree.
29 */
30 abstract public class AddComponentDocumentChange implements DocumentChange
31 {
32 protected AddComponentDocumentChange(
33 DocumentFragment fragment)
34 {
35 if (fragment == null)
36 throw new IllegalArgumentException(_LOG.getMessage(
37 "DOCUMENTFRAGMENT_REQUIRED"));
38
39 _fragment = fragment;
40 }
41
42
43 /**
44 * Returns true if adding the DocumentChange should force the JSP Document
45 * to reload
46 */
47 public boolean getForcesDocumentReload()
48 {
49 // adding components should force the document to reload
50 return true;
51 }
52
53 /**
54 * Returns the component that is to be added either as a child or a facet
55 * while applying this Change. Returns null if the component cannot be
56 * successfully re-constructed.
57 */
58 protected final DocumentFragment getComponentFragment()
59 {
60 return _fragment;
61 }
62
63 /**
64 * Given the target Node, return the DocumentFragment, imported into the
65 * target Document
66 * @param targetNode
67 */
68 protected final DocumentFragment getImportedComponentFragment(Node targetNode)
69 {
70 Document targetDocument = targetNode.getOwnerDocument();
71
72 // return a deep import
73 return (DocumentFragment)targetDocument.importNode(_fragment, true);
74 }
75
76 private final DocumentFragment _fragment;
77 private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
78 AddComponentDocumentChange.class);
79 }