blob: f20707e5e46f97f8d018fccf0ac4bc77c8283d16 [file] [log] [blame]
nitindb0f7b262004-11-23 19:12:23 +00001/*******************************************************************************
2 * Copyright (c) 2004 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 *******************************************************************************/
nitind49418452005-02-15 06:42:31 +000011package org.eclipse.jst.jsp.core.internal.document;
nitindb0f7b262004-11-23 19:12:23 +000012
david_williamsbcf91262005-11-14 03:54:42 +000013import org.eclipse.jst.jsp.core.internal.Assert;
david_williams4ad020f2005-04-18 08:00:30 +000014import org.eclipse.wst.sse.core.internal.provisional.AbstractAdapterFactory;
15import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter;
16import org.eclipse.wst.sse.core.internal.provisional.INodeAdapterFactory;
17import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
david_williams4ad020f2005-04-18 08:00:30 +000018import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
nitindb0f7b262004-11-23 19:12:23 +000019import org.w3c.dom.Node;
20
21/**
22 * This class adapts document
23 * with the an instance of PageDirectiveAdapter
24 */
david_williams285ee2d2005-04-10 18:21:21 +000025public class PageDirectiveAdapterFactory extends AbstractAdapterFactory implements INodeAdapterFactory {
nitindb0f7b262004-11-23 19:12:23 +000026
27
28 private PageDirectiveAdapter pageDirectiveAdapterInstance = null;
29
30 /**
31 * Constructor for PageDirectiveAdapterFactory.
32 * Note: its important not to be a singleton, since
33 * this factory needs to track its adapter(s) and release
34 * them when they are released.
35 *
36 * @param adapterKey
37 * @param registerAdapters
38 */
39 protected PageDirectiveAdapterFactory(Object adapterKey, boolean registerAdapters) {
40 super(adapterKey, registerAdapters);
41 }
42
43 /**
44 * The no argument constructor assumes its a
45 * Factory for PageDirectiveAdapter
46 */
47 public PageDirectiveAdapterFactory() {
48 this(PageDirectiveAdapter.class, true);
49 }
50
51 protected INodeAdapter createAdapter(INodeNotifier target) {
52 PageDirectiveAdapter result = null;
david_williamsc39caaf2005-04-05 06:07:16 +000053 if (target instanceof IDOMNode) {
54 IDOMNode node = (IDOMNode) target;
nitindb0f7b262004-11-23 19:12:23 +000055 if (node.getNodeType() == Node.DOCUMENT_NODE) {
56 result = getAdapterInstance(target);
nitindb0f7b262004-11-23 19:12:23 +000057 }
58
59 }
60 return result;
61 }
62
63 public void release() {
64 if (pageDirectiveAdapterInstance != null) {
pavery2a24f372005-06-16 03:10:09 +000065 pageDirectiveAdapterInstance.release();
nitindb0f7b262004-11-23 19:12:23 +000066 }
67 }
68
69 /**
70 * We assume this is only called for 'document' target
71 */
72 protected PageDirectiveAdapter getAdapterInstance(INodeNotifier target) {
73 // if our instance already exists with a different
74 // target, then, somehow, the document node must
75 // have changed for a model, so we should release
76 // old adapter and create new one for new document
77 // node. This is probably a programming error.
78 if (pageDirectiveAdapterInstance != null) {
79 if (target != pageDirectiveAdapterInstance.getTarget()) {
80 release();
81 pageDirectiveAdapterInstance = new PageDirectiveAdapterImpl(target);
82 }
83 // else return the one we have
84 }
85 else {
86 // if is equal to null, create a new one
87 pageDirectiveAdapterInstance = new PageDirectiveAdapterImpl(target);
88 }
nitind73e76412005-11-10 01:50:19 +000089 Assert.isNotNull(pageDirectiveAdapterInstance, "pageDipageDirectiveAdapterInstance was null"); //$NON-NLS-1$
nitindb0f7b262004-11-23 19:12:23 +000090 return pageDirectiveAdapterInstance;
91 }
92
david_williams285ee2d2005-04-10 18:21:21 +000093 public INodeAdapterFactory copy() {
nitindb0f7b262004-11-23 19:12:23 +000094
david_williams6f9dcdd2005-10-09 05:14:55 +000095 return new PageDirectiveAdapterFactory(getAdapterKey(), isShouldRegisterAdapter());
nitindb0f7b262004-11-23 19:12:23 +000096 }
97}