001 package com.javaworld.JavaBeans.XMLBeans;
002
003
004 import org.w3c.dom.*;
005 import java.lang.reflect.*;
006
007 /**
008 * Static utility functions to ease dealing with DOM classes.
009 */
010 public class Util {
011 /**
012 * Util constructor comment.
013 */
014 public Util() {
015 super();
016 }
017 /**
018 * Capitalize the first letter of a word.
019 * @param s java.lang.String
020 */
021 public static String capitalize(String s) {
022 char chars[] = s.toCharArray();
023 chars[0] = Character.toUpperCase(chars[0]);
024 return new String(chars);
025 }
026 /**
027 * Return the first "interesting" child node. An "interesting" node is
028 * either a text node that contains something besides white space, or
029 * anything else other than a comment. Return null if children are all
030 * boring (that is, not interesting.)
031 * @return org.w3c.dom.Node
032 * @param node org.w3c.dom.Node
033 */
034 public static Node getFirstInterestingChild(Node node) {
035 NodeList nl = node.getChildNodes();
036 int i;
037 for (i = 0; i < nl.getLength(); i++) {
038 Node n = nl.item(i);
039
040 // If it's a comment, skip
041 if (n instanceof Comment) {
042 continue;
043 }
044
045 // If it's Text, but contains only white space, skip
046 if (n instanceof Text) {
047 String text = ((Text) n).getData();
048 if (text.trim().equals("")) {
049 continue;
050 }
051 }
052 return n;
053 }
054 return null;
055 }
056 /**
057 * Return the method with the given signature, or null if it doesn't
058 * exist. This method simply catches and ignores NoSuchMethodException.
059 * @return java.lang.reflect.Method
060 * @param c java.lang.Class
061 * @param name java.lang.String
062 */
063 public static Method getMethodByName(Class c, String name, Class[] args) {
064 Method mResult = null;
065 try {
066 mResult = c.getMethod(name, args);
067 } catch (NoSuchMethodException ex) {
068 ; // Ignore
069 }
070 return mResult;
071 }
072 }