Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing simple imports in xml #101

Closed
ValeryDubrava opened this issue Jun 27, 2013 · 3 comments
Closed

Parsing simple imports in xml #101

ValeryDubrava opened this issue Jun 27, 2013 · 3 comments

Comments

@ValeryDubrava
Copy link

I've added parsing imports to ServiceMapParser.

diff --git a/src/fr/adrienbrault/idea/symfony2plugin/dic/ServiceMapParser.java b/src/fr/adrienbrault/idea/symfony2plugin/dic/ServiceMapParser.java
index 4d73b8d..8ab34c6 100644
--- a/src/fr/adrienbrault/idea/symfony2plugin/dic/ServiceMapParser.java
+++ b/src/fr/adrienbrault/idea/symfony2plugin/dic/ServiceMapParser.java
@@ -11,8 +11,9 @@ import javax.xml.parsers.ParserConfigurationException;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;

 /**
  * @author Adrien Brault <adrien.brault@gmail.com>
@@ -26,17 +27,52 @@ public class ServiceMapParser {
         documentBuilder = dbFactory.newDocumentBuilder();
     }

+    public ServiceMap parse(File file) throws IOException, SAXException {
+        ServiceMap serviceMap = new ServiceMap();
+
+        HashSet<String> parsedFile = new HashSet<String>(); // to avoid recursion
+        parserRecursive(file.getParentFile(), file, serviceMap, parsedFile);
+
+        populateMapWithDefaultServices(serviceMap.getMap());
+        populateMapWithDefaultServices(serviceMap.getPublicMap());
+        return serviceMap;
+    }
+
     public ServiceMap parse(InputStream stream) throws IOException, SAXException {
-        return parse(documentBuilder.parse(stream));
+        ServiceMap serviceMap = new ServiceMap();
+
+        parseDocument(documentBuilder.parse(stream), serviceMap);
+
+        populateMapWithDefaultServices(serviceMap.getMap());
+        populateMapWithDefaultServices(serviceMap.getPublicMap());
+        return serviceMap;
     }

-    public ServiceMap parse(File file) throws IOException, SAXException {
-        return parse(documentBuilder.parse(file));
+    private void parserRecursive(File baseDir, File file, ServiceMap serviceMap, Set<String> parsedFiles) throws IOException, SAXException {
+        if (parsedFiles.contains(file.getPath())) {
+            return;
+        }
+        parsedFiles.add(file.getPath());
+
+        Document document = documentBuilder.parse(file);
+
+        parseDocument(document, serviceMap);
+
+        NodeList importNodes = document.getElementsByTagName("import");
+        for (int i = 0; i < importNodes.getLength(); i ++) {
+            Element node = (Element) importNodes.item(i);
+            if (!node.hasAttribute("resource")) {
+                continue;
+            }
+            String resource = node.getAttribute("resource");
+            File newResource = new File(baseDir, resource);
+            parserRecursive(baseDir, newResource, serviceMap, parsedFiles);
+        }
     }

-    public ServiceMap parse(Document document) {
-        Map<String, String> map = new HashMap<String, String>();
-        Map<String, String> publicMap = new HashMap<String, String>();
+    private void parseDocument(Document document, ServiceMap serviceMap) {
+        Map<String, String> map = serviceMap.getMap();
+        Map<String, String> publicMap = serviceMap.getPublicMap();

         NodeList servicesNodes = document.getElementsByTagName("service");
         for (int i = 0; i < servicesNodes.getLength(); i++) {
@@ -52,12 +88,6 @@ public class ServiceMapParser {
                 publicMap.put(node.getAttribute("id"), map.get(node.getAttribute("alias")));
             }
         }
-
-        // Support services whose class isn't specified
-        populateMapWithDefaultServices(map);
-        populateMapWithDefaultServices(publicMap);
-
-        return new ServiceMap(map, publicMap);
     }

     private void populateMapWithDefaultServices(Map<String, String> map) {
@adrienbrault
Copy link
Contributor

Thanks but a pull request would be better than a patch in an issue :). https://help.github.com/articles/creating-a-pull-request

@Haehnchen
Copy link
Owner

@ValeryDubrava
when does the import tag occur in your project? i think the compiled xml we are currently using, already included them. moreover your code is helpful for multi container support, which should be in soon #13. iam think of including it then...

@Haehnchen
Copy link
Owner

we have now index-based service, so every container file is recognize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants