📄 utils.java
字号:
if ((nodeKind != null) && nodeKind.getLocalPart().equals("simpleType") && Constants.isSchemaXSD(nodeKind.getNamespaceURI())) { // Under the simpleType there should be a restriction. // (There may be other #text nodes, which we will ignore). NodeList children = node.getChildNodes(); Node restrictionNode = null; for (int j = 0; (j < children.getLength()) && (restrictionNode == null); j++) { QName restrictionKind = Utils.getNodeQName(children.item(j)); if ((restrictionKind != null) && restrictionKind.getLocalPart().equals("restriction") && Constants.isSchemaXSD( restrictionKind.getNamespaceURI())) { restrictionNode = children.item(j); } } // The restriction node indicates the type being restricted // (the base attribute contains this type). // The base type must be a simple type, and not boolean TypeEntry baseEType = null; if (restrictionNode != null) { QName baseType = Utils.getTypeQName(restrictionNode, new BooleanHolder(), false); baseEType = symbolTable.getType(baseType); if (baseEType != null) { String javaName = baseEType.getName(); if (javaName.equals("boolean") || !SchemaUtils.isSimpleSchemaType( baseEType.getQName())) { baseEType = null; } } } // Process the enumeration elements underneath the restriction node if ((baseEType != null) && (restrictionNode != null)) { Vector v = new Vector(); NodeList enums = restrictionNode.getChildNodes(); for (int i = 0; i < enums.getLength(); i++) { QName enumKind = Utils.getNodeQName(enums.item(i)); if ((enumKind != null) && enumKind.getLocalPart().equals("enumeration") && Constants.isSchemaXSD( enumKind.getNamespaceURI())) { // Put the enum value in the vector. Node enumNode = enums.item(i); String value = Utils.getAttribute(enumNode, "value"); if (value != null) { v.add(value); } } } // is this really an enumeration? if (v.isEmpty()) { return null; } // The first element in the vector is the base type (an TypeEntry). v.add(0, baseEType); return v; } } return null; } /** * Capitalize the first character of the name. * * @param name * @return */ public static String capitalizeFirstChar(String name) { if ((name == null) || name.equals("")) { return name; } char start = name.charAt(0); if (Character.isLowerCase(start)) { start = Character.toUpperCase(start); return start + name.substring(1); } return name; } // capitalizeFirstChar /** * Prepend an underscore to the name * * @param name * @return */ public static String addUnderscore(String name) { if ((name == null) || name.equals("")) { return name; } return "_" + name; } /** * Map an XML name to a valid Java identifier * * @param name * @return */ public static String xmlNameToJava(String name) { // NOTE: This method should really go away and all callers should call // JavaUtils.xmlNameToJava directly. But there are a lot of them and I wanted // to keep the changes to a minimum. Besides, these are static methods so the should // be inlined. return JavaUtils.xmlNameToJava(name); } /** * Map an XML name to a valid Java identifier w/ capitolized first letter * * @param name * @return */ public static String xmlNameToJavaClass(String name) { return capitalizeFirstChar(xmlNameToJava(name)); } /** * Method makePackageName * * @param namespace * @return */ public static String makePackageName(String namespace) { String hostname = null; String path = ""; // get the target namespace of the document try { URL u = new URL(namespace); hostname = u.getHost(); path = u.getPath(); } catch (MalformedURLException e) { if (namespace.indexOf(":") > -1) { hostname = namespace.substring(namespace.indexOf(":") + 1); if (hostname.indexOf("/") > -1) { hostname = hostname.substring(0, hostname.indexOf("/")); } } else { hostname = namespace; } } // if we didn't file a hostname, bail if (hostname == null) { return null; } // convert illegal java identifier hostname = hostname.replace('-', '_'); path = path.replace('-', '_'); // chomp off last forward slash in path, if necessary if ((path.length() > 0) && (path.charAt(path.length() - 1) == '/')) { path = path.substring(0, path.length() - 1); } // tokenize the hostname and reverse it StringTokenizer st = new StringTokenizer(hostname, ".:"); String[] words = new String[st.countTokens()]; for (int i = 0; i < words.length; ++i) { words[i] = st.nextToken(); } StringBuffer sb = new StringBuffer(namespace.length()); for (int i = words.length - 1; i >= 0; --i) { addWordToPackageBuffer(sb, words[i], (i == words.length - 1)); } // tokenize the path StringTokenizer st2 = new StringTokenizer(path, "/"); while (st2.hasMoreTokens()) { addWordToPackageBuffer(sb, st2.nextToken(), false); } return sb.toString(); } /** * Massage <tt>word</tt> into a form suitable for use in a Java package name. * Append it to the target string buffer with a <tt>.</tt> delimiter iff * <tt>word</tt> is not the first word in the package name. * * @param sb the buffer to append to * @param word the word to append * @param firstWord a flag indicating whether this is the first word */ private static void addWordToPackageBuffer(StringBuffer sb, String word, boolean firstWord) { if (JavaUtils.isJavaKeyword(word)) { word = JavaUtils.makeNonJavaKeyword(word); } // separate with dot after the first word if (!firstWord) { sb.append('.'); } // prefix digits with underscores if (Character.isDigit(word.charAt(0))) { sb.append('_'); } // replace periods with underscores if (word.indexOf('.') != -1) { char[] buf = word.toCharArray(); for (int i = 0; i < word.length(); i++) { if (buf[i] == '.') { buf[i] = '_'; } } word = new String(buf); } sb.append(word); } /** * Query Java Local Name * * @param fullName * @return */ public static String getJavaLocalName(String fullName) { return fullName.substring(fullName.lastIndexOf('.') + 1); } // getJavaLocalName /** * Query Java Package Name * * @param fullName * @return */ public static String getJavaPackageName(String fullName) { if (fullName.lastIndexOf('.') > 0) { return fullName.substring(0, fullName.lastIndexOf('.')); } else { return ""; } } // getJavaPackageName /** * Does the given file already exist in the given namespace? * * @param name * @param namespace * @param namespaces * @return * @throws IOException */ public static boolean fileExists( String name, String namespace, Namespaces namespaces) throws IOException { String packageName = namespaces.getAsDir(namespace); String fullName = packageName + name; return new File(fullName).exists(); } // fileExists /** A simple map of the primitive types and their holder objects */ private static HashMap TYPES = new HashMap(7); static { TYPES.put("int", "java.lang.Integer"); TYPES.put("float", "java.lang.Float"); TYPES.put("boolean", "java.lang.Boolean"); TYPES.put("double", "java.lang.Double"); TYPES.put("byte", "java.lang.Byte"); TYPES.put("short", "java.lang.Short"); TYPES.put("long", "java.lang.Long"); } /** * Return a string with "var" wrapped as an Object type if needed * * @param type * @param var * @return */ public static String wrapPrimitiveType(TypeEntry type, String var) { String objType = (type == null) ? null : (String) TYPES.get(type.getName()); if (objType != null) { return "new " + objType + "(" + var + ")"; } else if ((type != null) && type.getName().equals("byte[]") && type.getQName().getLocalPart().equals("hexBinary")) { // Need to wrap byte[] in special HexBinary object to get the correct serialization return "new org.apache.axis.types.HexBinary(" + var + ")"; } else { return var; } } // wrapPrimitiveType /** * Return the Object variable 'var' cast to the appropriate type * doing the right thing for the primitive types. * * @param var * @return */ public static String getResponseString(Parameter param, String var) { if (param.getType() == null) { return ";";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -