Example usage for org.apache.commons.jxpath JXPathContext registerNamespace

List of usage examples for org.apache.commons.jxpath JXPathContext registerNamespace

Introduction

In this page you can find the example usage for org.apache.commons.jxpath JXPathContext registerNamespace.

Prototype

public void registerNamespace(String prefix, String namespaceURI) 

Source Link

Document

Registers a namespace prefix.

Usage

From source file:jp.go.nict.langrid.commons.jxpath.WSDLUtil.java

/**
 * /*from  w  ww.  j  a  va2s.  co  m*/
 * 
 */
public static URL getServiceAddress(InputStream wsdl) throws IOException, MalformedURLException, SAXException {
    JXPathContext context = newWSDLContext(wsdl, "_");
    context.registerNamespace("wsdlsoap", "http://schemas.xmlsoap.org/wsdl/soap/");
    String url = (String) context.getValue("_:definitions/_:service/_:port/wsdlsoap:address/@location");
    if (url == null)
        return null;
    else
        return new URL(url);
}

From source file:jp.go.nict.langrid.commons.jxpath.BPELUtil.java

/**
 * /*from   w  ww  .  j  ava 2s.co m*/
 * 
 */
public static JXPathContext newBPELContext(InputStream bpel, String bpelNSPrefix)
        throws IOException, SAXException {
    JXPathContext context = JXPathUtil.newXMLContext(bpel);
    context.setLenient(true);
    context.registerNamespace(bpelNSPrefix, Constants.BPEL_URI);
    return context;
}

From source file:jp.go.nict.langrid.commons.jxpath.BPELUtil.java

/**
 * //  w  w w.ja  va 2 s . c o  m
 * 
 */
public static JXPathContext newWSBPEL_2_0_Context(InputStream bpel, String bpelNSPrefix)
        throws IOException, SAXException {
    JXPathContext context = JXPathUtil.newXMLContext(bpel);
    context.setLenient(true);
    context.registerNamespace(bpelNSPrefix, Constants.WSBPEL_2_0_URI);
    return context;
}

From source file:jp.go.nict.langrid.commons.jxpath.WSDLUtil.java

/**
 * /*  w  ww . j a  v  a 2s.c  om*/
 * 
 */
public static JXPathContext newWSDLContext(InputStream wsdl, String wsdlNSPrefix)
        throws IOException, SAXException {
    JXPathContext context = JXPathUtil.newXMLContext(wsdl);
    context.setLenient(true);
    context.registerNamespace(wsdlNSPrefix, Constants.WSDL_URI);
    return context;
}

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

/**
 * /*from   w ww .ja va  2  s.com*/
 * 
 */
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:de.iai.ilcd.xml.read.FlowPropertyReader.java

@Override
public FlowProperty parse(JXPathContext context, PrintWriter out) {

    context.registerNamespace("ilcd", "http://lca.jrc.it/ILCD/FlowProperty");

    FlowProperty flowProperty = new FlowProperty();

    // OK, now read in all fields common to all DataSet types
    readCommonFields(flowProperty, DataSetType.FLOWPROPERTY, context);

    IMultiLangString synonyms = parserHelper.getIMultiLanguageString("//common:synonyms");
    flowProperty.setSynonyms(synonyms);/*from  w  w w .  jav  a 2  s.  c o m*/

    GlobalReference refToUnitGroup = commonReader.getGlobalReference("//ilcd:referenceToReferenceUnitGroup",
            out);
    flowProperty.setReferenceToUnitGroup(refToUnitGroup);

    return flowProperty;
}

From source file:de.iai.ilcd.xml.read.ContactReader.java

@Override
public Contact parse(JXPathContext context, PrintWriter out) {

    context.registerNamespace("ilcd", "http://lca.jrc.it/ILCD/Contact");

    Contact contact = new Contact();

    // OK, now read in all fields common to all DataSet types
    readCommonFields(contact, DataSetType.CONTACT, context);

    IMultiLangString shortName = parserHelper.getIMultiLanguageString("//common:shortName");
    String contactAddress = parserHelper.getStringValue("//ilcd:contactAddress");
    String phone = parserHelper.getStringValue("//ilcd:telephone");
    String fax = parserHelper.getStringValue("//ilcd:telefax");
    String email = parserHelper.getStringValue("//ilcd:email");
    String homePage = parserHelper.getStringValue("//ilcd:WWWAddress");
    String ccPoint = parserHelper.getStringValue("//ilcd:centralContactPoint");
    IMultiLangString contactDescription = parserHelper
            .getIMultiLanguageString("//ilcd:contactDescriptionOrComment");

    if (out != null) {
        if (shortName == null || contact.getName() == null)
            out.println("Warning: One of the fields 'name' or 'shortName' of the contact data set is empty");
    }/*from w w w. j  a v a  2  s  .c  om*/
    contact.setShortName(shortName);
    if (contact.getName() == null)
        contact.setName(shortName);
    contact.setContactAddress(contactAddress);
    contact.setPhone(phone);
    contact.setFax(fax);
    contact.setEmail(email);
    contact.setWww(homePage);
    contact.setCentralContactPoint(ccPoint);
    contact.setDescription(contactDescription);

    return contact;
}

From source file:de.iai.ilcd.xml.read.UnitGroupReader.java

@Override
public UnitGroup parse(JXPathContext context, PrintWriter out) {

    context.registerNamespace("ilcd", "http://lca.jrc.it/ILCD/UnitGroup");

    UnitGroup ugroup = new UnitGroup();

    // OK, now read in all fields common to all DataSet types
    readCommonFields(ugroup, DataSetType.UNITGROUP, context);

    // get all the units and set the reference unit
    String referenceUnitString = parserHelper.getStringValue("//ilcd:referenceToReferenceUnit");
    Integer referenceIndex = (referenceUnitString != null ? Integer.parseInt(referenceUnitString) : null);

    List<Unit> units = readUnits(context);
    for (Unit unit : units) {
        ugroup.addUnit(unit);/*from w ww.j a  va  2s. co m*/
        if (referenceIndex != null && referenceIndex == unit.getInternalId())
            ugroup.setReferenceUnit(unit);
    }

    return ugroup;
}

From source file:de.iai.ilcd.xml.read.SourceReader.java

@Override
public Source parse(JXPathContext context, PrintWriter out) {

    context.registerNamespace("ilcd", "http://lca.jrc.it/ILCD/Source");

    Source source = new Source();

    // OK, now read in all fields common to all DataSet types
    readCommonFields(source, DataSetType.SOURCE, context);

    IMultiLangString shortName = parserHelper.getIMultiLanguageString("//common:shortName");
    IMultiLangString citation = parserHelper.getIMultiLanguageString("//ilcd:sourceCitation");
    String publicationType = parserHelper.getStringValue("//ilcd:publicationType");
    PublicationTypeValue publicationTypeValue = PublicationTypeValue.UNDEFINED;
    if (publicationType != null) {
        try {/*from w w w. j  a va 2 s. c  o  m*/
            publicationTypeValue = PublicationTypeValue.fromValue(publicationType);
        } catch (Exception e) {
            if (out != null) {
                out.println("Warning: the field publicationType has an illegal value " + publicationType);
            }
        }
    }

    IMultiLangString description = parserHelper.getIMultiLanguageString("//ilcd:sourceDescriptionOrComment");

    source.setShortName(shortName);
    source.setName(shortName);
    source.setCitation(citation);
    source.setPublicationType(publicationTypeValue);
    source.setDescription(description);

    List<String> digitalFileReferences = parserHelper.getStringValues("//ilcd:referenceToDigitalFile", "uri");
    for (String digitalFileReference : digitalFileReferences) {
        logger.info("raw source data set file name is {}", getDataSetFileName());
        String baseDirectory = FilenameUtils.getFullPath(getDataSetFileName());
        // get rid of any leading/trailing white space
        digitalFileReference = digitalFileReference.trim();
        String absoluteFileName = FilenameUtils.concat(baseDirectory, digitalFileReference);
        absoluteFileName = FilenameUtils.normalize(absoluteFileName);
        logger.info("normalized form of digital file name is {}", absoluteFileName);
        logger.debug("reference to digital file: " + absoluteFileName);
        File file = new File(absoluteFileName);
        DigitalFile digitalFile = new DigitalFile();
        logger.debug("canread:" + (file.canRead()));
        String fileName = null;
        if (file.canRead()) {
            fileName = absoluteFileName; // file will be imported and Name
            // stripped after importing
        } else if (absoluteFileName.toLowerCase().endsWith(".jpg")
                || absoluteFileName.toLowerCase().endsWith(".jpeg")
                || absoluteFileName.toLowerCase().endsWith(".gif")
                || absoluteFileName.toLowerCase().endsWith(".pdf")) {
            // in case we're on a case sensitive filesystem and the case of the extension is not correct, we'll try
            // to fix this
            // TO DO this could be a little more elegant

            java.io.File parentDir = file.getParentFile();
            String fileNotFound = file.getName();
            String[] matches = parentDir.list(new CaseInsensitiveFilenameFilter(fileNotFound));
            if (matches != null && matches.length == 1) {
                fileName = parentDir + File.separator + matches[0];
                logger.debug(fileName);
            } else {
                fileName = digitalFileReference;
            }
        } else {
            logger.debug("file could not be read from " + file.getAbsolutePath());
            fileName = digitalFileReference; // Not a local filename, likely
            // be an URL to external
            // resource
        } // logger.debug("found reference to digital file: " + fileName);
        logger.info("set filename of digital file to {}", fileName);
        digitalFile.setFileName(fileName);
        source.addFile(digitalFile);
    }

    List<GlobalReference> contacts = commonReader.getGlobalReferences("//ilcd:referenceToContact", out);
    for (GlobalReference contact : contacts) {
        source.addContact(contact);
    }

    return source;
}

From source file:de.iai.ilcd.xml.read.DataSetReader.java

private DataSet parseStream(InputStream inputStream) {

    JDOMParser parser = new JDOMParser();
    parser.setValidating(false);//from   w w w. j a v  a 2  s.c  o m
    Object doc = parser.parseXML(inputStream);
    JXPathContext context = JXPathContext.newContext(doc);
    context.setLenient(true);
    context.registerNamespace("common", "http://lca.jrc.it/ILCD/Common");

    parserHelper = new DataSetParsingHelper(context);
    commonReader = new CommonConstructsReader(parserHelper);

    return parse(context, out);
}