| 1 | package org.axdt.axdoc.util; |
|---|
| 2 | |
|---|
| 3 | import java.io.IOException; |
|---|
| 4 | import java.util.ArrayList; |
|---|
| 5 | import java.util.HashMap; |
|---|
| 6 | import java.util.Map; |
|---|
| 7 | |
|---|
| 8 | import org.axdt.axdoc.AXDocPlugin; |
|---|
| 9 | import org.axdt.axdoc.model.AXEntry; |
|---|
| 10 | import org.axdt.axdoc.model.AXIndex; |
|---|
| 11 | import org.axdt.axdoc.model.AXIndexNode; |
|---|
| 12 | import org.axdt.axdoc.model.AXLevel; |
|---|
| 13 | import org.axdt.axdoc.model.AXRoot; |
|---|
| 14 | import org.axdt.axdoc.model.AXRootType; |
|---|
| 15 | import org.axdt.axdoc.model.util.AXUtil; |
|---|
| 16 | import org.axdt.axdoc.preferences.AXDocPreferences; |
|---|
| 17 | import org.eclipse.core.runtime.Path; |
|---|
| 18 | import org.eclipse.emf.common.CommonPlugin; |
|---|
| 19 | import org.eclipse.emf.common.util.URI; |
|---|
| 20 | import org.eclipse.emf.ecore.EObject; |
|---|
| 21 | import org.eclipse.emf.ecore.resource.Resource; |
|---|
| 22 | import org.eclipse.emf.ecore.resource.ResourceSet; |
|---|
| 23 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; |
|---|
| 24 | |
|---|
| 25 | public class Index0r { |
|---|
| 26 | |
|---|
| 27 | private static Index0r instance = null; |
|---|
| 28 | |
|---|
| 29 | public static Index0r getInstance() { |
|---|
| 30 | if (instance == null) { |
|---|
| 31 | instance = new Index0r(); |
|---|
| 32 | AXDocPreferences.checkAXDocPaths(instance); |
|---|
| 33 | } |
|---|
| 34 | return instance; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | protected Map<String, AXRoot> roots; |
|---|
| 38 | protected AXDocParser docParser; |
|---|
| 39 | protected URI baseURI; |
|---|
| 40 | |
|---|
| 41 | public Index0r() { |
|---|
| 42 | roots = new HashMap<String, AXRoot>(); |
|---|
| 43 | docParser = new AXDocParser(); |
|---|
| 44 | baseURI = createBaseUri(); |
|---|
| 45 | } |
|---|
| 46 | protected URI createBaseUri() { |
|---|
| 47 | return CommonPlugin.resolve(URI.createURI("platform:/meta/"+AXDocPlugin.PLUGIN_ID)); |
|---|
| 48 | } |
|---|
| 49 | public AXIndexNode[] find(String qname) { |
|---|
| 50 | if (qname == null || qname.length() == 0) { |
|---|
| 51 | return null; |
|---|
| 52 | } |
|---|
| 53 | String[] splitQname = qname.split("::"); |
|---|
| 54 | if (splitQname.length == 0) { |
|---|
| 55 | return null; |
|---|
| 56 | } |
|---|
| 57 | if (splitQname.length == 1) { |
|---|
| 58 | return findPackage(splitQname[0]); |
|---|
| 59 | } |
|---|
| 60 | if (splitQname.length == 2) { |
|---|
| 61 | return findMember(splitQname[0], splitQname[1]); |
|---|
| 62 | } |
|---|
| 63 | return null; |
|---|
| 64 | } |
|---|
| 65 | public AXIndexNode[] findMember(String qualifier, String name) { |
|---|
| 66 | ArrayList<AXIndexNode> result = new ArrayList<AXIndexNode>(); |
|---|
| 67 | for (AXRoot root:roots.values()) { |
|---|
| 68 | AXIndexNode index = root.getPackageIndex(qualifier); |
|---|
| 69 | String[] qsplit = name.split("\\."); |
|---|
| 70 | if (qsplit.length > 1) { |
|---|
| 71 | for (int i = 0; index != null && i < qsplit.length-1;i++) { |
|---|
| 72 | index = index.localEntry(qsplit[0], false); |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | if (index != null) { |
|---|
| 76 | for (AXEntry entry : index.localEntry(qsplit[qsplit.length-1])) { |
|---|
| 77 | result.add(entry); |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | return result.toArray(new AXIndexNode[result.size()]); |
|---|
| 82 | } |
|---|
| 83 | public AXIndex[] findPackage(String name) { |
|---|
| 84 | ArrayList<AXIndex> result = new ArrayList<AXIndex>(); |
|---|
| 85 | for (AXRoot root:roots.values()) { |
|---|
| 86 | AXIndex index = root.getPackageIndex(name); |
|---|
| 87 | if (index != null) { |
|---|
| 88 | result.add(index); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | return result.toArray(new AXIndex[result.size()]); |
|---|
| 92 | } |
|---|
| 93 | public AXRoot addRoot(String name, String url, AXRootType type) { |
|---|
| 94 | AXRoot root = roots.get(url); |
|---|
| 95 | if (root == null) { |
|---|
| 96 | URI resourcePath = baseURI.appendSegment(Integer.toHexString(url.hashCode())).appendSegment("root-index.axdoc"); |
|---|
| 97 | if (new Path(resourcePath.toString()).toFile().exists()) { |
|---|
| 98 | ResourceSet resset = new ResourceSetImpl(); |
|---|
| 99 | Resource resource = resset.createResource(resourcePath); |
|---|
| 100 | try { |
|---|
| 101 | resource.load(null); |
|---|
| 102 | if (resource.getContents().size()>0) { |
|---|
| 103 | EObject object = resource.getContents().get(0); |
|---|
| 104 | if (object instanceof AXRoot) |
|---|
| 105 | return (AXRoot) object; |
|---|
| 106 | } |
|---|
| 107 | } catch (IOException e) { |
|---|
| 108 | AXDocPlugin.getDefault().log("error loading axdoc file",e); |
|---|
| 109 | } |
|---|
| 110 | } else { |
|---|
| 111 | root = AXUtil.createRoot(name, url, resourcePath.toString()); |
|---|
| 112 | root.setRootType(type); |
|---|
| 113 | } |
|---|
| 114 | if (root != null) { |
|---|
| 115 | roots.put(root.getUrl(), root); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | return root; |
|---|
| 119 | } |
|---|
| 120 | public AXRoot[] getRoots() { |
|---|
| 121 | return roots.values().toArray(new AXRoot[roots.size()]); |
|---|
| 122 | } |
|---|
| 123 | public void initialize(AXRoot root, AXLevel level) { |
|---|
| 124 | if (root.getRootType() == AXRootType.ASDOC) { |
|---|
| 125 | try { |
|---|
| 126 | docParser.parseDoc(root, level); |
|---|
| 127 | root.eResource().save(null); |
|---|
| 128 | } catch (Exception e) { |
|---|
| 129 | if (AXDocPlugin.getDefault() != null) |
|---|
| 130 | AXDocPlugin.getDefault().log("error parsing asdoc at "+root.getUrl(), e); |
|---|
| 131 | else e.printStackTrace(); |
|---|
| 132 | } |
|---|
| 133 | } else { |
|---|
| 134 | if (AXDocPlugin.getDefault() != null) |
|---|
| 135 | AXDocPlugin.getDefault().log("only asoc index is available"); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | } |
|---|