All Downloads are FREE. Search and download functionalities are using the official Maven repository.

testutil.LocalCustomizationFileTransformer Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2018, 2022 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0, which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package testutil;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;

import com.sun.tools.ws.wsdl.parser.Util;
import com.sun.xml.ws.util.xml.XmlUtil;


/**
 * This class is called from ant to transform
 * a regular jaxrpc:bindings customization file so that it looks
 * for the wsdl file in a local location instead
 * of getting it through http.
 */
public class LocalCustomizationFileTransformer {

    /**
     * 
     */
    public LocalCustomizationFileTransformer() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * Must pass in location of orginal file and
     * location to save new file to. 
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args == null || args.length < 1) {
            System.err.println("ERROR: need args: old config file location,\n" +
                "wsdl name");
            return;
        }

        try{
            Map map = null;
            if(args.length > 1){
                 File wsdlLocationFile = new File(args[2]);
                 File newLoc = new File(args[1]+"WEB-INF/wsdl/"+wsdlLocationFile.getName());
                 map = buildMap(new File(args[1]+"WEB-INF/wsdl/").getCanonicalPath(), newLoc.getCanonicalPath());
            }

            int index = args[0].lastIndexOf('/');
            String path = "./";
            String files = args[0];
            if(index != -1){
                path = args[0].substring(0, index+1);
                files = args[0].substring(index+1);
            }

            StringTokenizer tokenizer = new StringTokenizer(files, ",");
            if(!tokenizer.hasMoreTokens())
                tokenizer = new StringTokenizer(files, " ");

            while(tokenizer.hasMoreTokens()){
                String token = tokenizer.nextToken().trim();
                String oldCustom = path+token;
        //        String configLocation = config.substring(0, config.lastIndexOf('/')+1);
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = dbf.newDocumentBuilder();

                // make change in config file
                File file = new File(oldCustom);
                Document doc = builder.parse(file);
                Element wsdlElement = doc.getDocumentElement();
                String localName = wsdlElement.getLocalName();
                String nspace = wsdlElement.getNamespaceURI();
                if((localName != null && localName.equals("bindings")) &&
                        (nspace != null && !wsdlElement.getNamespaceURI().equals("http://java.sun.com/xml/ns/jaxws")))
                    continue;
                Attr wsdlAttr = wsdlElement.getAttributeNode("wsdlLocation");
                String location = (wsdlAttr != null)?wsdlAttr.getValue():null;


                if(location != null) {
                    //send the wsdlLocation value to outputproperty
                    //probably called from with-tomcat target
                    if(args.length == 1){
                        System.out.println(location);
                        return;
                    }
                    token = "temp-config.xml";
                }

                String newCustom = args[1]+token;

                if(location != null){
                    File wsdlLocationFile = new File(args[2]);
                    File newLoc = new File(args[1]+"WEB-INF/wsdl/"+wsdlLocationFile.getName());
                    wsdlAttr.setValue(newLoc.getCanonicalPath());
                    //System.out.println(args[1]+wsdlLocationFile.getName());
                    System.out.println(newLoc.getCanonicalPath());
                }else{
                    transformSchemaLocation(wsdlElement, map);
                }
                // save file
                TransformerFactory factory = TransformerFactory.newInstance();
                Transformer transformer = factory.newTransformer();
                transformer.transform(new DOMSource(doc), new StreamResult(newCustom));
            }
        } catch (Exception e) {
            System.err.println("exception in LocalConfigFileTransformer:");
            e.printStackTrace();
        }
    }
    
    
    private static Attr getWSDLLocation(Element root){
        NamedNodeMap atts = root.getAttributes();
        for( int i=0; i map) throws IOException{
        for (Iterator iter = XmlUtil.getAllChildren(root); iter.hasNext();) {
            Element e = Util.nextElement(iter);
            if(e==null)
                return;
            Attr schemaLocationAttr = e.getAttributeNode("schemaLocation");
            if(schemaLocationAttr == null)
                continue;
            //this location is a URL
            String sl = schemaLocationAttr.getValue();
            int index = sl.lastIndexOf('?');
            if(index != -1){
                schemaLocationAttr.setValue("file:/"+map.get(sl.substring(index+1)));
            }
//
//            int index = sl.lastIndexOf('/');
//            String path = "./";
//            String file = sl;
//            if(index != -1){
//                //File newLoc = new File(parent+sl.substring(index+1));
//                schemaLocationAttr.setValue(sl.substring(index+1));
//            }
        }
    }
    /*
     * Make sure that the element was found. It will be null
     * when there is a problem with the jaxrpc-ri file.
     *
    private static void checkEndpoint(Element endpoint) {
        if (endpoint == null) {
            System.err.println("\nLocalConfigFileTransformer could not " +
                "find \"endpoint\" element in sun-jaxws.xml file.\n" +
                "Please check file and verify that it was generated correctly.\n");
            throw new RuntimeException("Cannot process sun-jaxws.xml file");
        }
    }

    private static Map buildMap(NodeList nodeList) throws Exception {
        Map map = new HashMap(nodeList.getLength());
        Element endpoint = null;
        for (int i=0; i}path e.g: wsdl=sub/a.wsdl {@literal -->} /WEB-INF/wsdl/sub/a.wsdl
     */
    private static Map buildMap(String dirName, String primaryWsdl)
    throws Exception {
        Map map = new HashMap<>();
        List schemaIds = new ArrayList<>();
        List wsdlIds = new ArrayList<>();
        File dir = new File(dirName);
        List list = new ArrayList<>();
        buildDocList(dir, list);
        for(String file : list) {
            // Use this logic for now for identifying wsdl or schema
			// TODO: use the logic from runtime
            if (file.endsWith(".wsdl")) {
                // TODO: For primary wsdl need to put only "wsdl"
                if (!file.equals(primaryWsdl)) {
                    wsdlIds.add(file);
                } else {
                    map.put("wsdl", file);
                }
            } else if (file.endsWith(".xsd")) {
                schemaIds.add(file);
            }
        }
        Collections.sort(wsdlIds);
        Collections.sort(schemaIds);
        int wsdlNum = 1;
        for(String file : wsdlIds) {
            map.put("wsdl="+wsdlNum++, file);
        }
        int schemaNum = 1;
        for(String file : schemaIds) {
            map.put("xsd="+schemaNum++, file);
        }
//System.out.println("Map="+map);
        return map;
    }

    private static  void buildDocList(File dir, List list)
    throws Exception {
        String[] files = dir.list();
        for (int i = 0; i < files.length; i++) {
            File sub = new File(dir, files[i]);
            if (sub.isDirectory()) {
                buildDocList(sub, list);
            } else {
                list.add(sub.getCanonicalPath());
            }
        }
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy