Package org.apache.ws.resource.tool

Source Code of org.apache.ws.resource.tool.GenerationUtils

/*=============================================================================*
*  Copyright 2004 The Apache Software Foundation
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*=============================================================================*/
package org.apache.ws.resource.tool;

import org.apache.ws.resource.ResourceDefinition;
import org.apache.ws.util.OperationInfo;
import org.apache.xmlbeans.impl.common.NameUtil;
import javax.wsdl.Operation;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
* A set of utility methods that are utilized by Wsdl2Java.
*
* @author Ian Springer (ian DOT springer AT hp DOT com)
*/
public abstract class GenerationUtils
{
   private static Map s_nsToPrefixMap = new Hashtable(  );

   /**
    * DOCUMENT_ME
    *
    * @param nsURI DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public static String getJavaPackageName( String nsURI )
   {
      return NameUtil.getPackageFromNamespace( nsURI );
   }

   /**
    * DOCUMENT_ME
    *
    * @param resourceDef DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public static Map getOperationInfoMap( ResourceDefinition resourceDef )
   {
      Map      opInfoMap = new HashMap(  );
      String[] opNames = resourceDef.getCustomOperationNames(  );
      for ( int i = 0; i < opNames.length; i++ )
      {
         Operation     op     = resourceDef.getPortType(  ).getOperation( opNames[i], null, null );
         OperationInfo opInfo = new OperationInfo( op,
                                                   resourceDef.getDefinition(  ).getTargetNamespace(  ) );

         // TODO: handle operations that have no input element (e.g. wsx:Get) - we can't have a null key
         opInfoMap.put( opInfo.getRequestElementName(  ),
                        opInfo );
      }

      return opInfoMap;
   }

   /**
    * Generates a valid concise prefix for the specified namespace URI. Guaranteed to never return the same prefix for
    * two different namespaces.
    *
    * @param nsURI the namespace URI
    *
    * @return the prefix
    */
   public static String getPrefix( String nsURI )
   {
      if ( nsURI == null )
      {
         return ( "" );
      }

      synchronized ( s_nsToPrefixMap )
      {
         if ( s_nsToPrefixMap.containsKey( nsURI ) )
         {
            return (String) s_nsToPrefixMap.get( nsURI );
         }
      }

      String chompedNsURI = nsURI;

      while ( ( chompedNsURI.length(  ) > 0 )
              && !Character.isLetterOrDigit( chompedNsURI.charAt( chompedNsURI.length(  ) - 1 ) ) )
      {
         chompedNsURI = chompedNsURI.substring( 0, chompedNsURI.length(  ) - 1 );
      }

      int prefixStartIndex = chompedNsURI.lastIndexOf( '/' );

      if ( prefixStartIndex == -1 )
      {
         prefixStartIndex = chompedNsURI.lastIndexOf( '.' );
      }

      if ( prefixStartIndex == -1 )
      {
         prefixStartIndex = chompedNsURI.lastIndexOf( ':' );
      }

      String prefix = chompedNsURI.substring( prefixStartIndex + 1 );

      for ( int i = 0; i < prefix.length(  ); i++ )
      {
         char currentChar = prefix.charAt( i );
         if ( !isValidPrefixChar( currentChar ) )
         {
            prefix = prefix.replace( currentChar, '-' );
         }
      }

      synchronized ( s_nsToPrefixMap )
      {
         if ( s_nsToPrefixMap.containsValue( prefix ) )
         {
            prefix = manglePrefix( prefix );
         }

         s_nsToPrefixMap.put( nsURI, prefix );
      }

      return ( prefix );
   }

   private static boolean isValidPrefixChar( char c )
   {
      return ( Character.isLetterOrDigit( c ) ) || ( c == '-' ) || ( c == '_' ) || ( c == '.' );
   }

   private static String manglePrefix( String prefix )
   {
      String uniquePrefix;
      synchronized ( s_nsToPrefixMap )
      {
         int i = 1;
         do
         {
            uniquePrefix = prefix + ++i;
         }
         while ( s_nsToPrefixMap.containsValue( uniquePrefix ) );
      }

      return uniquePrefix;
   }
}
TOP

Related Classes of org.apache.ws.resource.tool.GenerationUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.