Example usage for org.apache.commons.jxpath.xml DOMParser DOMParser

List of usage examples for org.apache.commons.jxpath.xml DOMParser DOMParser

Introduction

In this page you can find the example usage for org.apache.commons.jxpath.xml DOMParser DOMParser.

Prototype

DOMParser

Source Link

Usage

From source file:net.sbbi.upnp.JXPathParser.java

public Object parseXML(InputStream in) {
    StringBuffer xml = new StringBuffer();
    try {//from w  ww  .j a  va  2s.  co m
        byte[] buffer = new byte[512];
        int readen = 0;
        while ((readen = in.read(buffer)) != -1) {
            xml.append(new String(buffer, 0, readen));
        }
    } catch (IOException ex) {
        log.error("IOException occured during XML reception", ex);
        return null;
    }
    String doc = xml.toString();
    log.debug("Readen raw xml doc:\n" + doc);
    if (doc.indexOf(buggyChar) != -1) {
        doc = doc.replace(buggyChar, ' ');
    }

    ByteArrayInputStream in2 = new ByteArrayInputStream(doc.getBytes());
    DOMParser parser = new DOMParser();
    return parser.parseXML(in2);
}

From source file:jp.go.nict.langrid.bpel.ProcessAnalyzer.java

/**
 * /* w  w  w  . j av  a2  s .  c  om*/
 * 
 */
public static WSDL analyzeWsdl(byte[] body) throws MalformedURLException, SAXException, URISyntaxException {
    JXPathContext context = JXPathContext.newContext(new DOMParser().parseXML(new ByteArrayInputStream(body)));
    context.registerNamespace("_", Constants.WSDL_URI);
    context.registerNamespace("wsdlsoap", Constants.WSDLSOAP_URI);
    context.registerNamespace("plnk1", Constants.PLNK_URI);
    context.registerNamespace("plnk2", Constants.PLNK_URI_2);

    WSDL wsdl = new WSDL();
    wsdl.setBody(body);
    wsdl.setTargetNamespace(new URI(context.getValue("/_:definitions/@targetNamespace").toString()));

    // 
    // 
    HashMap<String, PartnerLinkType> links = new HashMap<String, PartnerLinkType>();
    {
        Iterator<?> pli = context.iteratePointers("/_:definitions/plnk1:partnerLinkType");
        while (pli.hasNext()) {
            JXPathContext pltc = JXPathContext.newContext(context, ((Pointer) pli.next()).getNode());
            pltc.registerNamespace("plnk1", Constants.PLNK_URI);
            PartnerLinkType plt = new PartnerLinkType();
            plt.setName(pltc.getValue("/@name").toString());

            HashMap<String, Role> roles = new HashMap<String, Role>();
            Iterator<?> ri = pltc.iteratePointers("/plnk1:role");
            while (ri.hasNext()) {
                JXPathContext rc = JXPathContext.newContext(pltc, ((Pointer) ri.next()).getNode());
                rc.registerNamespace("plnk1", Constants.PLNK_URI);
                Role role = new Role();
                role.setName(rc.getValue("/@name").toString());
                role.setPortType(toQName(rc, rc.getValue("/plnk1:portType/@name").toString()));
                roles.put(role.getName(), role);
            }
            plt.setRoles(roles);
            links.put(plt.getName(), plt);
        }
    }
    {
        Iterator<?> pli = context.iteratePointers("/_:definitions/plnk2:partnerLinkType");
        while (pli.hasNext()) {
            JXPathContext pltc = JXPathContext.newContext(context, ((Pointer) pli.next()).getNode());
            pltc.registerNamespace("plnk2", Constants.PLNK_URI_2);
            PartnerLinkType plt = new PartnerLinkType();
            plt.setName(pltc.getValue("/@name").toString());

            HashMap<String, Role> roles = new HashMap<String, Role>();
            Iterator<?> ri = pltc.iteratePointers("/plnk2:role");
            while (ri.hasNext()) {
                JXPathContext rc = JXPathContext.newContext(pltc, ((Pointer) ri.next()).getNode());
                Role role = new Role();
                role.setName(rc.getValue("/@name").toString());
                role.setPortType(toQName(rc, rc.getValue("/@portType").toString()));
                roles.put(role.getName(), role);
            }
            plt.setRoles(roles);
            links.put(plt.getName(), plt);
        }
    }
    wsdl.setPlinks(links);

    { // definitions/portType??
        HashMap<String, EndpointReference> portTypeToEndpointReference = new HashMap<String, EndpointReference>();
        Iterator<?> pti = context.iteratePointers("/_:definitions/_:portType");
        while (pti.hasNext()) {
            JXPathContext ptc = JXPathContext.newContext(context, ((Pointer) pti.next()).getNode());
            portTypeToEndpointReference.put(ptc.getValue("/@name").toString(), null);
        }
        wsdl.setPortTypeToEndpointReference(portTypeToEndpointReference);
    }

    { // definitions/binding??
        HashMap<String, QName> bindingTypes = new HashMap<String, QName>();
        Iterator<?> bi = context.iteratePointers("/_:definitions/_:binding");
        while (bi.hasNext()) {
            JXPathContext bc = JXPathContext.newContext(context, ((Pointer) bi.next()).getNode());
            bindingTypes.put(bc.getValue("/@name").toString(), toQName(bc, bc.getValue("/@type").toString()));
        }
        wsdl.setBindingTypes(bindingTypes);
    }

    String firstServiceName = null;
    { // definitions/service??
        HashMap<String, Service> services = new HashMap<String, Service>();
        Iterator<?> si = context.iteratePointers("/_:definitions/_:service");
        while (si.hasNext()) {
            JXPathContext sc = JXPathContext.newContext(context, ((Pointer) si.next()).getNode());
            sc.registerNamespace("_", Constants.WSDL_URI);
            String serviceName = sc.getValue("/@name").toString();

            Service s = new Service();
            s.setName(serviceName);
            if (firstServiceName == null) {
                firstServiceName = serviceName;
            }

            // port??
            HashMap<String, Port> ports = new HashMap<String, Port>();
            Iterator<?> pi = sc.iteratePointers("/_:port");
            while (pi.hasNext()) {
                JXPathContext pc = JXPathContext.newContext(sc, ((Pointer) pi.next()).getNode());
                pc.registerNamespace("wsdlsoap", Constants.WSDLSOAP_URI);
                Port p = new Port();
                p.setName(pc.getValue("/@name").toString());
                p.setBinding(toQName(pc, pc.getValue("/@binding").toString()));
                p.setAddress(new URL(pc.getValue("/wsdlsoap:address/@location").toString()));
                ports.put(p.getName(), p);
            }
            s.setPorts(ports);

            services.put(s.getName(), s);
        }
        wsdl.setServices(services);
    }

    if (firstServiceName == null) {
        wsdl.setFilename("unknown" + WSDL_EXTENSION);
    } else {
        wsdl.setFilename(firstServiceName + WSDL_EXTENSION);
    }
    return wsdl;
}

From source file:org.onecmdb.utils.internal.nmap.TransformNmap.java

public List<CiBean> transform() throws SAXException, IOException {

    // Parse discovery result
    DOMParser parser = new DOMParser();
    Document doc = (Document) parser.parseXML(null);
    //parser.parse(inputFile);
    //Document doc = parser.

    NodeList hostList = doc.getElementsByTagName("host");
    if (hostList == null) {
        return (Collections.EMPTY_LIST);
    }//from w ww  .j av  a2  s.co  m

    List<CiBean> allBeans = new ArrayList<CiBean>();

    for (int i = 0, j = 0; i < hostList.getLength(); i++) {
        System.out.println("${progress} " + i);
        Element host = (Element) hostList.item(i);
        List<CiBean> currentBeans = new ArrayList<CiBean>();
        if (host != null) {
            Element status = (Element) host.getElementsByTagName("status").item(0);
            String state = status.getAttribute("state");

            ValueBean stateValue = new ValueBean();
            stateValue.setAlias("state");
            stateValue.setValue(state);

            CiBean ipBean = new CiBean();
            ipBean.setDerivedFrom(ipTemplate);
            ipBean.setTemplate(false);

            CiBean nicBean = new CiBean();
            nicBean.setDerivedFrom(nicTemplate);
            nicBean.setTemplate(false);

            CiBean netIfBean = new CiBean();
            netIfBean.setDerivedFrom(netIfTemplate);
            netIfBean.setTemplate(false);

            // Set state on NetIf.
            netIfBean.addAttributeValue(stateValue);

            String ipAddress = null;
            j++;
            NodeList addrList = host.getElementsByTagName("address");
            for (int a = 0; a < addrList.getLength(); a++) {
                Element el = (Element) addrList.item(a);
                String addr = el.getAttribute("addr");
                String type = el.getAttribute("addrtype");

                if (type.equals("mac")) {
                    nicBean.setAlias("mac-" + addr.replace(":", "."));
                    // Set mac address.
                    nicBean.addAttributeValue(new ValueBean("mac", addr, false));

                    // Set vendor.
                    String vendor = el.getAttribute("vendor");
                    nicBean.addAttributeValue(new ValueBean("vendor", vendor, false));

                    // Connect nicBean to netif.
                    netIfBean.addAttributeValue(new ValueBean("nic", nicBean.getAlias(), true));

                } else {
                    ipBean.setDerivedFrom(ipTemplate);
                    ipBean.setAlias("ip-" + addr);

                    // Set ipAddress
                    ipAddress = addr;
                    ipBean.addAttributeValue(new ValueBean("ipAddress", addr, false));

                    // Set addr type
                    ipBean.addAttributeValue(new ValueBean("addrType", type, false));

                    // Connect nicBean to netif.
                    netIfBean.addAttributeValue(new ValueBean("ipAddress", ipBean.getAlias(), true));
                }
            }
            netIfBean.setAlias("netif-" + ipBean.getAlias());

            // Validate that the state is ok, since we retrive all ip's..
            if (state.equals("down")) {
                if (beanProvider != null) {
                    CiBean remote = beanProvider.getBean(netIfBean.getAlias());
                    if (remote == null) {
                        continue;
                    }
                }
            }
            currentBeans.add(ipBean);
            // Can be empty (loocal host)
            if (nicBean.getAlias() != null) {
                currentBeans.add(nicBean);
            }
            currentBeans.add(netIfBean);

            NodeList hostsNameList = host.getElementsByTagName("hostnames");
            for (int hs = 0; hs < hostsNameList.getLength(); hs++) {
                Element hostnames = (Element) hostsNameList.item(hs);
                NodeList hostnameList = hostnames.getElementsByTagName("hostname");
                for (int h = 0; h < hostnameList.getLength(); h++) {
                    Element hostname = (Element) hostnameList.item(h);
                    String name = hostname.getAttribute("name");
                    String type = hostname.getAttribute("type");

                    CiBean hostnameBean = new CiBean();
                    hostnameBean.setAlias("c-" + name);
                    hostnameBean.setTemplate(false);
                    hostnameBean.setDerivedFrom(hostnameTemplate);

                    // Set hostname
                    hostnameBean.addAttributeValue(new ValueBean("hostname", name, false));

                    // Connect this to a dnsEntry.
                    CiBean dnsEntry = new CiBean();
                    dnsEntry.setTemplate(false);
                    dnsEntry.setDerivedFrom(dnsEntryTemplate);
                    dnsEntry.setAlias(name + "." + ipAddress);
                    dnsEntry.addAttributeValue(new ValueBean("ip", ipBean.getAlias(), true));
                    dnsEntry.addAttributeValue(new ValueBean("hostname", hostnameBean.getAlias(), true));
                    dnsEntry.addAttributeValue(new ValueBean("type", type, false));

                    // Add it to the bean list.
                    currentBeans.add(hostnameBean);
                    currentBeans.add(dnsEntry);

                }
            }
        }
        allBeans.addAll(currentBeans);
    }
    return (allBeans);
}