KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2007 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.lang.reflect.Constructor JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.help.IAnchor;
19 import org.eclipse.help.ICommandLink;
20 import org.eclipse.help.IContentExtension;
21 import org.eclipse.help.IContext;
22 import org.eclipse.help.IInclude;
23 import org.eclipse.help.IIndex;
24 import org.eclipse.help.IIndexEntry;
25 import org.eclipse.help.ILink;
26 import org.eclipse.help.IToc;
27 import org.eclipse.help.ITopic;
28 import org.eclipse.help.IUAElement;
29 import org.eclipse.help.internal.context.Context;
30 import org.eclipse.help.internal.extension.ContentExtension;
31 import org.eclipse.help.internal.index.Index;
32 import org.eclipse.help.internal.index.IndexEntry;
33 import org.eclipse.help.internal.toc.Link;
34 import org.eclipse.help.internal.toc.Toc;
35 import org.w3c.dom.Element JavaDoc;
36
37 /*
38  * Constructs typed model elements from DOM elements or interface
39  * implementations.
40  */

41 public class UAElementFactory {
42
43     private static final Class JavaDoc[][] interfaceTable = new Class JavaDoc[][] {
44         { ITopic.class, Topic.class },
45         { IIndexEntry.class, IndexEntry.class },
46         { IContext.class, Context.class },
47         { IAnchor.class, Anchor.class },
48         { IInclude.class, Include.class },
49         { ILink.class, Link.class },
50         { IToc.class, Toc.class },
51         { ICommandLink.class, CommandLink.class },
52         { IIndex.class, Index.class },
53         { IContentExtension.class, ContentExtension.class },
54     };
55
56     private static final Map JavaDoc classByElementName;
57
58     static {
59         classByElementName = Collections.synchronizedMap(new HashMap JavaDoc());
60         classByElementName.put(Anchor.NAME, Anchor.class);
61         classByElementName.put(Include.NAME, Include.class);
62         classByElementName.put(Toc.NAME, Toc.class);
63         classByElementName.put(Topic.NAME, Topic.class);
64         classByElementName.put(Index.NAME, Index.class);
65         classByElementName.put(IndexEntry.NAME, IndexEntry.class);
66         classByElementName.put(Context.NAME, Context.class);
67         classByElementName.put(CommandLink.NAME, CommandLink.class);
68         classByElementName.put(Link.NAME, Link.class);
69         classByElementName.put(ContentExtension.NAME_CONTRIBUTION, ContentExtension.class);
70         classByElementName.put(ContentExtension.NAME_CONTRIBUTION_LEGACY, ContentExtension.class);
71         classByElementName.put(ContentExtension.NAME_REPLACEMENT, ContentExtension.class);
72         classByElementName.put(ContentExtension.NAME_REPLACEMENT_LEGACY, ContentExtension.class);
73     }
74     
75     public static UAElement newElement(Element element) {
76         String JavaDoc name = element.getNodeName();
77         Class JavaDoc clazz = (Class JavaDoc)classByElementName.get(name);
78         if (clazz != null) {
79             try {
80                 Constructor JavaDoc constructor = clazz.getConstructor(new Class JavaDoc[] { Element.class });
81                 return (UAElement)constructor.newInstance(new Object JavaDoc[] { element });
82             }
83             catch (Exception JavaDoc e) {
84                 String JavaDoc msg = "Error creating document model element"; //$NON-NLS-1$
85
HelpPlugin.logError(msg, e);
86             }
87         }
88         return new UAElement(element);
89     }
90
91     public static UAElement newElement(IUAElement src) {
92         for (int i=0;i<interfaceTable.length;++i) {
93             Class JavaDoc interfaze = interfaceTable[i][0];
94             Class JavaDoc clazz = interfaceTable[i][1];
95             if (interfaze.isAssignableFrom(src.getClass())) {
96                 try {
97                     Constructor JavaDoc constructor = clazz.getConstructor(new Class JavaDoc[] { interfaze });
98                     return (UAElement)constructor.newInstance(new Object JavaDoc[] { src });
99                 }
100                 catch (Exception JavaDoc e) {
101                     String JavaDoc msg = "Error creating document model element"; //$NON-NLS-1$
102
HelpPlugin.logError(msg, e);
103                 }
104             }
105         }
106         return null;
107     }
108 }
109
Popular Tags