summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/utils/XmlElementParser.java
blob: f6fef4bebd1ac2065798ef1c52e1cd6cbaea9960 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.c2kernel.utils;

import java.io.StringReader;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class XmlElementParser
{
	public static String[] parse(String data, String xpath)
	{
        try {
            return Dom4JElementParser.parse(data, xpath);
        } catch (NoClassDefFoundError ex) {
            Logger.msg(5, "Using old xpath parser");
            return parseOld(data, xpath);
        }
	}
    
    public static String[] parseOld(String data, String path)
        {
            Vector returnData = new Vector();
            String[] returnArray = new String[0];
            try
            {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                StringReader is = new StringReader(data);
                Document doc = builder.parse(new InputSource(is));
                StringTokenizer pathTokens = new StringTokenizer(path, "/");
                int taille = pathTokens.countTokens();
                String[] pathElements = new String[taille];
                int i=taille;
                while (pathTokens.hasMoreElements())
                    pathElements[--i] = pathTokens.nextToken();
                
                if (Logger.doLog(6)) {
                    Logger.msg(6, "Path elements:");
                    for (int x=0;x<pathElements.length;x++)
                        Logger.debug(6, pathElements[x]);
                }
                
                Logger.msg(6, "Looking for attribute "+pathElements[0]+" in "+pathElements[1]);
                NodeList nl = doc.getElementsByTagName(pathElements[1]);
                for (int j = 0; j < nl.getLength(); j++)
                {
                    Logger.msg(6, "Found one");
                    Element e = (Element)nl.item(j);
                    boolean match=true;
                    Node child=e;
                    for (int k=2;k<taille&&match;k++)
                        {
                            Logger.msg(6, "Checking parent "+pathElements[k]);
                            child = child.getParentNode(); 
                            if (!child.getNodeName().equals(pathElements[k])) 
                                {
                                Logger.msg(6, "No match for "+child.getNodeName());
                                match=false; 
                                }
                            else
                                Logger.msg(6, "Match");
                        }
                    if (match&&e.hasAttribute(pathElements[0])) {
                        Logger.msg(6, "Matching Attribute "+pathElements[0]+"="+e.getAttribute(pathElements[0]));   
                        returnData.add(e.getAttribute(pathElements[0]));
                    }
                }
            
                Logger.msg(6, "Looking for element "+pathElements[0]);
                nl = doc.getElementsByTagName(pathElements[0]);
                for (int j = 0; j < nl.getLength(); j++)
                {
                    Logger.msg(6, "Found one");
                    Element e = (Element)nl.item(j);
                    boolean match=true;
                    Node child=e;
                    for (int k=1;k<taille&&match;k++)
                        {
                            Logger.msg(6, "Checking parent "+pathElements[k]);
                            child = child.getParentNode(); 
                            if (!child.getNodeName().equals(pathElements[k])) 
                                {
                                Logger.msg(6, "No match for "+child.getNodeName());
                                match=false; 
                                }
                            else
                                Logger.msg(6, "Match");
                        }
                    if (match) 
                        {
                        String s =e.getFirstChild().getNodeValue();
                        Logger.msg(6, "Found Element "+pathElements[0]+"="+s);
                        if (s!=null) returnData.add(s);
                        }
                }
            }
            catch (Exception e)
            {
                //Logger.error(e);
            }
            Logger.msg(3, returnData.size()+" values found for "+path);
            returnArray=new String[returnData.size()];
            for (int i=0;i<returnArray.length;i++)
                returnArray[i] = (String)returnData.get(i);
            return returnArray;
        }    
}