| 1 | package org.axdt.axdoc.model.util; |
|---|
| 2 | |
|---|
| 3 | import java.io.ByteArrayInputStream; |
|---|
| 4 | import java.io.IOException; |
|---|
| 5 | import java.util.ArrayList; |
|---|
| 6 | import java.util.List; |
|---|
| 7 | |
|---|
| 8 | import org.axdt.axdoc.model.AXDocFactory; |
|---|
| 9 | import org.axdt.axdoc.model.AXEntry; |
|---|
| 10 | import org.axdt.axdoc.model.AXIndex; |
|---|
| 11 | import org.axdt.axdoc.model.AXMember; |
|---|
| 12 | import org.axdt.axdoc.model.AXRoot; |
|---|
| 13 | import org.axdt.axdoc.model.AXType; |
|---|
| 14 | import org.eclipse.emf.common.util.URI; |
|---|
| 15 | import org.eclipse.emf.ecore.EObject; |
|---|
| 16 | import org.eclipse.emf.ecore.resource.Resource; |
|---|
| 17 | import org.eclipse.emf.ecore.resource.ResourceSet; |
|---|
| 18 | |
|---|
| 19 | public class AXUtil { |
|---|
| 20 | private static AXDocXMLProcessor xmlProcessor = new AXDocXMLProcessor(); |
|---|
| 21 | private static AXDocFactory factory = AXDocFactory.eINSTANCE; |
|---|
| 22 | public static Resource createResource(String path) { |
|---|
| 23 | return xmlProcessor.createResourceSet().createResource(URI.createURI(path)); |
|---|
| 24 | } |
|---|
| 25 | public static Resource getOrCreateResource(ResourceSet set, EObject node, String path) { |
|---|
| 26 | Resource resource = node.eResource(); |
|---|
| 27 | if (resource == null) { |
|---|
| 28 | if (set == null) |
|---|
| 29 | set = xmlProcessor.createResourceSet(); |
|---|
| 30 | resource = set.createResource(URI.createURI(path)); |
|---|
| 31 | resource.getContents().add(node); |
|---|
| 32 | } else if (path != null) { |
|---|
| 33 | resource.setURI(URI.createURI(path)); |
|---|
| 34 | } |
|---|
| 35 | return resource; |
|---|
| 36 | } |
|---|
| 37 | public static String writeToString(EObject node) throws IOException { |
|---|
| 38 | ResourceSet set = (node.eResource() != null) ? node.eResource().getResourceSet() : null; |
|---|
| 39 | return writeToString(getOrCreateResource(set, node, null)); |
|---|
| 40 | } |
|---|
| 41 | public static String writeToString(Resource resource) throws IOException { |
|---|
| 42 | if (resource.getContents().isEmpty()) |
|---|
| 43 | return null; |
|---|
| 44 | return xmlProcessor.saveToString(resource, null); |
|---|
| 45 | } |
|---|
| 46 | public static Resource readFromString(String text, String path) throws IOException { |
|---|
| 47 | Resource resource = createResource(path); |
|---|
| 48 | ByteArrayInputStream inputStream = new ByteArrayInputStream(text.getBytes()); |
|---|
| 49 | resource.load(inputStream, null); |
|---|
| 50 | return resource; |
|---|
| 51 | } |
|---|
| 52 | public static AXRoot createRoot(String url, String basePath) { |
|---|
| 53 | return createRoot(null, url, basePath); |
|---|
| 54 | } |
|---|
| 55 | public static AXRoot createRoot(String name, String url, String resourceUrl) { |
|---|
| 56 | AXRoot root = factory.createAXRoot(); |
|---|
| 57 | root.setName(name); |
|---|
| 58 | root.setUrl(url); |
|---|
| 59 | ResourceSet set = xmlProcessor.createResourceSet(); |
|---|
| 60 | getOrCreateResource(set, root, resourceUrl); |
|---|
| 61 | return root; |
|---|
| 62 | } |
|---|
| 63 | public static AXIndex createPackage(AXRoot root, String name) { |
|---|
| 64 | if (name == null || name.equals("")) return root; |
|---|
| 65 | AXIndex current = root; |
|---|
| 66 | main : for (String part : name.split("\\.")) { |
|---|
| 67 | for (AXIndex pack : current.getIndexes()) { |
|---|
| 68 | if (part.equals(pack.getName())) { |
|---|
| 69 | current = pack; |
|---|
| 70 | continue main; |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | AXIndex child = factory.createAXIndex(); |
|---|
| 74 | child.setName(part); |
|---|
| 75 | current.getIndexes().add(child); |
|---|
| 76 | //getResource(child, child.fullName()+".axdoc.xml"); |
|---|
| 77 | current = child; |
|---|
| 78 | } |
|---|
| 79 | return current; |
|---|
| 80 | } |
|---|
| 81 | public static AXMember createMember(String name) { |
|---|
| 82 | AXMember member = factory.createAXMember(); |
|---|
| 83 | if (name.contains("()")) { |
|---|
| 84 | name = name.replace("()", ""); |
|---|
| 85 | member.setFunction(true); |
|---|
| 86 | } |
|---|
| 87 | member.setName(name); |
|---|
| 88 | return member; |
|---|
| 89 | } |
|---|
| 90 | public static AXType createType(String typeName) { |
|---|
| 91 | AXType type = factory.createAXType(); |
|---|
| 92 | type.setName(typeName); |
|---|
| 93 | return type; |
|---|
| 94 | } |
|---|
| 95 | public static List<AXEntry> getAllTypes(AXIndex index) { |
|---|
| 96 | ArrayList<AXEntry> allTypes = new ArrayList<AXEntry>(); |
|---|
| 97 | collectAllTypes(index, allTypes); |
|---|
| 98 | return allTypes; |
|---|
| 99 | } |
|---|
| 100 | public static void collectAllTypes(AXIndex index, List<AXEntry> allTypes) { |
|---|
| 101 | for (AXEntry e:index.getEntries()) { |
|---|
| 102 | if (e.getType().isType()) allTypes.add(e); |
|---|
| 103 | } |
|---|
| 104 | for (AXIndex p:index.getIndexes()) { |
|---|
| 105 | collectAllTypes(p, allTypes); |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | } |
|---|