1 11 package org.eclipse.help.internal; 12 13 import java.lang.reflect.Constructor ; 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.Map ; 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 ; 36 37 41 public class UAElementFactory { 42 43 private static final Class [][] interfaceTable = new Class [][] { 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 classByElementName; 57 58 static { 59 classByElementName = Collections.synchronizedMap(new HashMap ()); 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 name = element.getNodeName(); 77 Class clazz = (Class )classByElementName.get(name); 78 if (clazz != null) { 79 try { 80 Constructor constructor = clazz.getConstructor(new Class [] { Element.class }); 81 return (UAElement)constructor.newInstance(new Object [] { element }); 82 } 83 catch (Exception e) { 84 String msg = "Error creating document model element"; 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 interfaze = interfaceTable[i][0]; 94 Class clazz = interfaceTable[i][1]; 95 if (interfaze.isAssignableFrom(src.getClass())) { 96 try { 97 Constructor constructor = clazz.getConstructor(new Class [] { interfaze }); 98 return (UAElement)constructor.newInstance(new Object [] { src }); 99 } 100 catch (Exception e) { 101 String msg = "Error creating document model element"; HelpPlugin.logError(msg, e); 103 } 104 } 105 } 106 return null; 107 } 108 } 109 | Popular Tags |