KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > Util


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb.binding;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.security.AccessController JavaDoc;
27 import java.security.PrivilegedAction JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import javax.xml.XMLConstants JavaDoc;
31 import javax.xml.namespace.QName JavaDoc;
32 import javax.xml.namespace.NamespaceContext JavaDoc;
33
34 import org.apache.xerces.xs.XSImplementation;
35 import org.apache.xerces.xs.XSLoader;
36 import org.apache.xerces.xs.XSModel;
37 import org.jboss.logging.Logger;
38 import org.jboss.util.Classes;
39 import org.jboss.xb.binding.sunday.unmarshalling.LSInputAdaptor;
40 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
41 import org.jboss.xb.binding.sunday.unmarshalling.XsdBinderTerminatingErrorHandler;
42 import org.w3c.dom.DOMConfiguration JavaDoc;
43 import org.w3c.dom.DOMErrorHandler JavaDoc;
44 import org.w3c.dom.bootstrap.DOMImplementationRegistry JavaDoc;
45 import org.w3c.dom.ls.LSInput JavaDoc;
46 import org.w3c.dom.ls.LSResourceResolver JavaDoc;
47 import org.xml.sax.Attributes JavaDoc;
48
49 /**
50  * Various utilities for XML binding.
51  *
52  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
53  * @version <tt>$Revision: 1958 $</tt>
54  */

55 public final class Util
56 {
57    /**
58     * Characters that are considered to be word separators while convertinging XML names to Java identifiers
59     * according to JAXB 2.0 spec.
60     */

61    public static final char HYPHEN_MINUS = '\u002D';
62    public static final char FULL_STOP = '\u002E';
63    public static final char COLLON = '\u003A';
64    public static final char LOW_LINE = '\u005F';
65    public static final char MIDDLE_DOT = '\u00B7';
66    public static final char GREEK_ANO_TELEIA = '\u0387';
67    public static final char ARABIC_END_OF_AYAH = '\u06DD';
68    public static final char ARABIC_START_OF_RUB_EL_HIZB = '\u06DE';
69
70    private static final Logger log = Logger.getLogger(Util.class);
71
72    private static XSImplementation xsImpl;
73
74    /**
75     * Returns a prefixed name for the passed in QName instance.
76     * If the argument has a prefix, the prefix is used. If not then
77     * local part is returned.
78     *
79     * @param qName an instance of QName to generate prefix name for
80     * @return generated prefixed name or empty string in case the argument is null
81     */

82    public static String JavaDoc getPrefixedName(QName JavaDoc qName)
83    {
84       String JavaDoc result = "";
85       if(qName != null)
86       {
87          String JavaDoc prefix = qName.getPrefix();
88          if(prefix.length() > 0)
89          {
90             result = prefix + ':' + qName.getLocalPart();
91          }
92          else
93          {
94             result = qName.getLocalPart();
95          }
96       }
97       return result;
98    }
99
100    /**
101     * Returns a prefixed name for passed in QName instance.
102     * If the argument has a prefix then its prefix is used.
103     * If not and the argument has namespace URI then namespace context is used
104     * to get the prefix.
105     *
106     * @param qName an instance of QName to generate prefix name for
107     * @param nc an instance of the NamespaceContext
108     * @return generated prefixed name or empty string in case the argument is null
109     */

110    public static String JavaDoc getPrefixedName(QName JavaDoc qName, NamespaceContext JavaDoc nc)
111    {
112       String JavaDoc result = "";
113       if(qName != null)
114       {
115          String JavaDoc prefix = qName.getPrefix();
116          if(prefix.length() > 0)
117          {
118             result = prefix + ':' + qName.getLocalPart();
119          }
120          else
121          {
122             String JavaDoc ns = qName.getNamespaceURI();
123             prefix = nc.getPrefix(ns);
124             if(prefix != null && prefix.length() > 0)
125             {
126                result = prefix + ':' + qName.getLocalPart();
127             }
128             else
129             {
130                result = qName.getLocalPart();
131             }
132          }
133       }
134       return result;
135    }
136
137    /**
138     * Converts XML name to Java class name according to
139     * Binding XML Names to Java Identifiers
140     * C.2. The Name to Identifier Mapping Algorithm
141     * jaxb-2_0-edr-spec-10_jun_2004.pdf
142     *
143     * @param name XML name
144     * @param ignoreLowLine whether low lines should not be parts of Java identifiers
145     * @return Java class name
146     */

147    public static String JavaDoc xmlNameToClassName(String JavaDoc name, boolean ignoreLowLine)
148    {
149       return XMLNameToJavaIdentifierConverter.PARSER.parse(XMLNameToJavaIdentifierConverter.CLASS_NAME,
150          name,
151          ignoreLowLine
152       );
153    }
154
155    public static String JavaDoc xmlNameToFieldName(String JavaDoc name, boolean ignoreLowLine)
156    {
157       return XMLNameToJavaIdentifierConverter.PARSER.parse(XMLNameToJavaIdentifierConverter.FIELD_NAME,
158          name,
159          ignoreLowLine
160       );
161    }
162
163    /**
164     * Converts XML name to Java getter method name according to
165     * Binding XML Names to Java Identifiers
166     * C.2. The Name to Identifier Mapping Algorithm
167     * jaxb-2_0-edr-spec-10_jun_2004.pdf
168     *
169     * @param name XML name
170     * @param ignoreLowLine whether low lines should not be parts of Java identifiers
171     * @return Java getter method name
172     */

173    public static String JavaDoc xmlNameToGetMethodName(String JavaDoc name, boolean ignoreLowLine)
174    {
175       return "get" + xmlNameToClassName(name, ignoreLowLine);
176    }
177
178    /**
179     * Converts XML name to Java setter method name according to
180     * Binding XML Names to Java Identifiers
181     * C.2. The Name to Identifier Mapping Algorithm
182     * jaxb-2_0-edr-spec-10_jun_2004.pdf
183     *
184     * @param name XML name
185     * @param ignoreLowLine whether low lines should not be parts of Java identifiers
186     * @return Java setter method name
187     */

188    public static String JavaDoc xmlNameToSetMethodName(String JavaDoc name, boolean ignoreLowLine)
189    {
190       return "set" + xmlNameToClassName(name, ignoreLowLine);
191    }
192
193    /**
194     * Converts XML name to Java constant name according to
195     * Binding XML Names to Java Identifiers
196     * C.2. The Name to Identifier Mapping Algorithm
197     * jaxb-2_0-edr-spec-10_jun_2004.pdf
198     *
199     * @param name XML name
200     * @return Java constant name
201     */

202    public static String JavaDoc xmlNameToConstantName(String JavaDoc name)
203    {
204       return XMLNameToJavaIdentifierConverter.PARSER.parse(XMLNameToJavaIdentifierConverter.CONSTANT_NAME,
205          name,
206          true
207       );
208    }
209
210    /**
211     * Converts XML namespace to Java package name.
212     * The base algorithm is described in JAXB-2.0 spec in 'C.5 Generating a Java package name'.
213     *
214     * @param namespace XML namespace
215     * @return Java package name
216     */

217    public static String JavaDoc xmlNamespaceToJavaPackage(String JavaDoc namespace)
218    {
219       if(namespace.length() == 0)
220       {
221          return namespace;
222       }
223
224       char[] src = namespace.toLowerCase().toCharArray();
225       char[] dst = new char[namespace.length()];
226
227       int srcInd = 0;
228       // skip protocol part, i.e. http://, urn://
229
while(src[srcInd++] != ':')
230       {
231       }
232
233       while(src[srcInd] == '/')
234       {
235          ++srcInd;
236       }
237
238       // skip www part
239
if(src[srcInd] == 'w' && src[srcInd + 1] == 'w' && src[srcInd + 2] == 'w')
240       {
241          srcInd += 4;
242       }
243
244       // find domain start and end indexes
245
int domainStart = srcInd;
246       while(srcInd < src.length && src[srcInd] != '/')
247       {
248          ++srcInd;
249       }
250
251       int dstInd = 0;
252       // copy domain parts in the reverse order
253
for(int start = srcInd - 1, end = srcInd; true; --start)
254       {
255          if(start == domainStart)
256          {
257             System.arraycopy(src, start, dst, dstInd, end - start);
258             dstInd += end - start;
259             break;
260          }
261
262          if(src[start] == '.')
263          {
264             System.arraycopy(src, start + 1, dst, dstInd, end - start - 1);
265             dstInd += end - start;
266             dst[dstInd - 1] = '.';
267             end = start;
268          }
269       }
270
271       // copy the rest
272
while(srcInd < src.length)
273       {
274          char c = src[srcInd++];
275          if(c == '/')
276          {
277             if(srcInd < src.length)
278             {
279                dst = append(dst, dstInd++, '.');
280                if(!Character.isJavaIdentifierStart(src[srcInd]))
281                {
282                   dst = append(dst, dstInd++, '_');
283                }
284             }
285          }
286          else if(c == '.')
287          {
288             // for now assume it's an extention, i.e. '.xsd'
289
break;
290          }
291          else
292          {
293             dst = append(dst, dstInd++, Character.isJavaIdentifierPart(c) ? c : '_');
294          }
295       }
296
297       return String.valueOf(dst, 0, dstInd);
298    }
299
300    /**
301     * Converts XML namespace URI and local name to fully qualified class name.
302     *
303     * @param namespaceUri namespace URI
304     * @param localName local name
305     * @param ignoreLowLine should low lines be ignored in the class name
306     * @return fully qualified class name
307     */

308    public static String JavaDoc xmlNameToClassName(String JavaDoc namespaceUri, String JavaDoc localName, boolean ignoreLowLine)
309    {
310       return namespaceUri == null || namespaceUri.length() == 0 ?
311          xmlNameToClassName(localName, ignoreLowLine) :
312          xmlNamespaceToJavaPackage(namespaceUri) + '.' + xmlNameToClassName(localName, ignoreLowLine);
313    }
314
315    public static boolean isAttributeType(final Class JavaDoc type)
316    {
317       return Classes.isPrimitive(type) ||
318          type == String JavaDoc.class ||
319          type == java.util.Date JavaDoc.class;
320    }
321
322    /**
323     * Parse the namespace location pairs in the schemaLocation and return the
324     * location that matches the nsURI argument.
325     *
326     * @return the location uri if found, null otherwise
327     */

328    public static String JavaDoc getSchemaLocation(Attributes JavaDoc attrs, String JavaDoc nsUri)
329    {
330       String JavaDoc location = null;
331       String JavaDoc schemaLocation = attrs.getValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "schemaLocation");
332       if(schemaLocation != null)
333       {
334          StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(schemaLocation, " \t\n\r");
335          while (tokenizer.hasMoreTokens())
336          {
337             String JavaDoc namespace = tokenizer.nextToken();
338             if (namespace.equals(nsUri) && tokenizer.hasMoreTokens())
339             {
340                location = tokenizer.nextToken();
341                break;
342             }
343          }
344       }
345       return location;
346    }
347
348    public static XSModel loadSchema(String JavaDoc xsdURL, SchemaBindingResolver schemaResolver)
349    {
350       boolean trace = log.isTraceEnabled();
351       long start = System.currentTimeMillis();
352       if(trace)
353          log.trace("loading xsd: " + xsdURL);
354
355       if(xsImpl == null)
356       {
357          xsImpl = getXSImplementation();
358       }
359
360       XSLoader schemaLoader = xsImpl.createXSLoader(null);
361       if(schemaResolver != null)
362       {
363          setResourceResolver(schemaLoader, schemaResolver);
364       }
365
366       setDOMErrorHandler(schemaLoader);
367       XSModel model = schemaLoader.loadURI(xsdURL);
368       if(model == null)
369       {
370          throw new IllegalArgumentException JavaDoc("Invalid URI for schema: " + xsdURL);
371       }
372
373       if (trace)
374          log.trace("Loaded xsd: " + xsdURL + " in " + (System.currentTimeMillis() - start) + "ms");
375       return model;
376    }
377
378    public static XSModel loadSchema(InputStream JavaDoc is, String JavaDoc encoding, SchemaBindingResolver schemaResolver)
379    {
380       if(log.isTraceEnabled())
381       {
382          log.trace("loading xsd from InputStream");
383       }
384
385       LSInputAdaptor input = new LSInputAdaptor(is, encoding);
386
387       XSImplementation impl = getXSImplementation();
388       XSLoader schemaLoader = impl.createXSLoader(null);
389       setDOMErrorHandler(schemaLoader);
390       if(schemaResolver != null)
391       {
392          setResourceResolver(schemaLoader, schemaResolver);
393       }
394
395       return schemaLoader.load(input);
396    }
397
398    public static XSModel loadSchema(Reader JavaDoc reader, String JavaDoc encoding, SchemaBindingResolver schemaResolver)
399    {
400       if(log.isTraceEnabled())
401       {
402          log.trace("loading xsd from Reader");
403       }
404
405       LSInputAdaptor input = new LSInputAdaptor(reader, encoding);
406
407       XSImplementation impl = getXSImplementation();
408       XSLoader schemaLoader = impl.createXSLoader(null);
409       setDOMErrorHandler(schemaLoader);
410       if(schemaResolver != null)
411       {
412          setResourceResolver(schemaLoader, schemaResolver);
413       }
414
415       return schemaLoader.load(input);
416    }
417
418    public static XSModel loadSchema(String JavaDoc data, String JavaDoc encoding)
419    {
420       if(log.isTraceEnabled())
421       {
422          log.trace("loading xsd from string");
423       }
424
425       LSInputAdaptor input = new LSInputAdaptor(data, encoding);
426
427       XSImplementation impl = getXSImplementation();
428       XSLoader schemaLoader = impl.createXSLoader(null);
429       setDOMErrorHandler(schemaLoader);
430       return schemaLoader.load(input);
431    }
432
433    // Private
434

435    /**
436     * Sets an array character's element with the given index to a character value.
437     * If index is more or equal to the length of the array, a new array is created with enough length to set
438     * the element.
439     *
440     * @param buf array of characters
441     * @param index index of the element to set
442     * @param ch character to set
443     * @return if the index parameter is less then array's length then the original array is returned,
444     * otherwise a new array is returned
445     */

446    private static char[] append(char[] buf, int index, char ch)
447    {
448       if(index >= buf.length)
449       {
450          char[] tmp = buf;
451          buf = new char[index + 4];
452          System.arraycopy(tmp, 0, buf, 0, tmp.length);
453       }
454       buf[index] = ch;
455       return buf;
456    }
457
458    private static void setResourceResolver(XSLoader schemaLoader, final SchemaBindingResolver schemaResolver)
459    {
460       DOMConfiguration JavaDoc config = schemaLoader.getConfig();
461       config.setParameter("resource-resolver", new LSResourceResolver JavaDoc()
462       {
463          public LSInput JavaDoc resolveResource(String JavaDoc type, String JavaDoc namespaceURI, String JavaDoc publicId, String JavaDoc systemId, String JavaDoc baseURI)
464          {
465             if (Constants.NS_XML_SCHEMA.equals(type))
466             {
467                String JavaDoc schemaLocation = systemId;
468                return schemaResolver.resolveAsLSInput(namespaceURI, baseURI, schemaLocation);
469             }
470             return null;
471          }
472       }
473       );
474    }
475
476    private static void setDOMErrorHandler(XSLoader schemaLoader)
477    {
478       DOMConfiguration JavaDoc config = schemaLoader.getConfig();
479       DOMErrorHandler JavaDoc errorHandler = (DOMErrorHandler JavaDoc)config.getParameter("error-handler");
480       if (errorHandler == null)
481       {
482          config.setParameter("error-handler", XsdBinderTerminatingErrorHandler.newInstance());
483       }
484    }
485
486    private static XSImplementation getXSImplementation()
487    {
488       return (XSImplementation) AccessController.doPrivileged(new PrivilegedAction JavaDoc()
489       {
490          public Object JavaDoc run()
491          {
492             
493             // Get DOM Implementation using DOM Registry
494
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
495             try
496             {
497                // Try the 2.6.2 version
498
String JavaDoc name = "org.apache.xerces.dom.DOMXSImplementationSourceImpl";
499                loader.loadClass(name);
500                System.setProperty(DOMImplementationRegistry.PROPERTY, name);
501             }
502             catch(ClassNotFoundException JavaDoc e)
503             {
504                // Try the 2.7.0 version
505
String JavaDoc name = "org.apache.xerces.dom.DOMXSImplementationSourceImpl";
506                System.setProperty(DOMImplementationRegistry.PROPERTY, name);
507             }
508
509             XSImplementation impl;
510             try
511             {
512                DOMImplementationRegistry JavaDoc registry = DOMImplementationRegistry.newInstance();
513                impl = (XSImplementation)registry.getDOMImplementation("XS-Loader");
514             }
515             catch(Exception JavaDoc e)
516             {
517                log.error("Failed to create schema loader.", e);
518                throw new IllegalStateException JavaDoc("Failed to create schema loader: " + e.getMessage());
519             }
520             return impl;
521          }
522       });
523    }
524
525    // Inner
526

527    /**
528     * An interface for XML name to Java identifier (class name, get/set methods, constant names) converter.
529     * The following rules and algorithms should be supported
530     * <ul>
531     * <li>Binding XML Names to Java Identifiers,
532     * C.2. The Name to Identifier Mapping Algorithm,
533     * jaxb-2_0-edr-spec-10_jun_2004.pdf</li>
534     * <li>http://www.w3.org/TR/soap12-part2/#namemap</li>
535     * </ul>
536     * <p/>
537     * But these are not guaranteed to work yet. Instead, a simplified implementation is provided.
538     * Incompatabilities should be fixed.
539     */

540    interface XMLNameToJavaIdentifierConverter
541    {
542       // commands indicating what should be done with the next character from the XML name
543
byte IGNORE = 0;
544       byte APPEND = 1;
545       byte APPEND_WITH_LOW_LINE = 2;
546       byte APPEND_UPPER_CASED = 3;
547       byte APPEND_UPPER_CASED_WITH_LOW_LINE = 4;
548
549       /**
550        * Returns a command for the next character given the previous character.
551        *
552        * @param prev previous character
553        * @param next next character
554        * @param ignoreLowLine whether low lines are allowed in the Java identifier or should be ignored
555        * @return command for the next character
556        */

557       byte commandForNext(char prev, char next, boolean ignoreLowLine);
558
559       char firstCharacter(char ch, String JavaDoc str, int secondCharIndex);
560
561       /**
562        * An XML name parser class that parses the XML name and asks the outer interface implementation
563        * what to do with the next parsed character from the XML name.
564        */

565       final class PARSER
566       {
567          /**
568           * Parses an XML name, asks the converter for a command for the next parsed character,
569           * applies the command, composed the resulting Java identifier.
570           *
571           * @param converter an implementation of XMLNameToJavaIdentifierConverter
572           * @param xmlName XML name
573           * @param ignoreLowLine indicated whether low lines are allowed as part of the Java identifier or
574           * should be ignored
575           * @return Java identifier
576           */

577          static String JavaDoc parse(XMLNameToJavaIdentifierConverter converter, String JavaDoc xmlName, boolean ignoreLowLine)
578          {
579             if(xmlName == null || xmlName.length() == 0)
580             {
581                throw new IllegalArgumentException JavaDoc("Bad XML name: " + xmlName);
582             }
583
584             char c = xmlName.charAt(0);
585             int i = 1;
586             if(!Character.isJavaIdentifierStart(c) || (c == LOW_LINE && ignoreLowLine))
587             {
588                while(i < xmlName.length())
589                {
590                   c = xmlName.charAt(i++);
591                   if(Character.isJavaIdentifierStart(c) && !(c == LOW_LINE && ignoreLowLine))
592                   {
593                      break;
594                   }
595                }
596
597                if(i == xmlName.length())
598                {
599                   throw new IllegalArgumentException JavaDoc(
600                      "XML name contains no valid character to start Java identifier: " + xmlName
601                   );
602                }
603             }
604
605             char[] buf = new char[xmlName.length() - i + 1];
606             buf[0] = converter.firstCharacter(c, xmlName, i);
607             int bufInd = 1;
608             while(i < xmlName.length())
609             {
610                char prev = c;
611                c = xmlName.charAt(i++);
612                byte command = converter.commandForNext(prev, c, ignoreLowLine);
613                switch(command)
614                {
615                   case IGNORE:
616                      break;
617                   case APPEND:
618                      buf = Util.append(buf, bufInd++, c);
619                      break;
620                   case APPEND_WITH_LOW_LINE:
621                      buf = Util.append(buf, bufInd++, LOW_LINE);
622                      buf = Util.append(buf, bufInd++, c);
623                      break;
624                   case APPEND_UPPER_CASED:
625                      buf = Util.append(buf, bufInd++, Character.toUpperCase(c));
626                      break;
627                   case APPEND_UPPER_CASED_WITH_LOW_LINE:
628                      buf = Util.append(buf, bufInd++, LOW_LINE);
629                      buf = Util.append(buf, bufInd++, Character.toUpperCase(c));
630                      break;
631                   default:
632                      throw new IllegalArgumentException JavaDoc("Unexpected command: " + command);
633                }
634             }
635
636             return new String JavaDoc(buf, 0, bufInd);
637          }
638       }
639
640       /**
641        * XML name to Java class name converter
642        */

643       XMLNameToJavaIdentifierConverter CLASS_NAME = new XMLNameToJavaIdentifierConverter()
644       {
645          public char firstCharacter(char ch, String JavaDoc str, int secondCharIndex)
646          {
647             return Character.toUpperCase(ch);
648          }
649
650          public byte commandForNext(char prev, char next, boolean ignoreLowLine)
651          {
652             byte command;
653             if(Character.isDigit(next))
654             {
655                command = APPEND;
656             }
657             else if(next == LOW_LINE)
658             {
659                command = ignoreLowLine ? IGNORE : APPEND;
660             }
661             else if(Character.isJavaIdentifierPart(next))
662             {
663                if(Character.isJavaIdentifierPart(prev) && !Character.isDigit(prev))
664                {
665                   command = prev == LOW_LINE ? APPEND_UPPER_CASED : APPEND;
666                }
667                else
668                {
669                   command = APPEND_UPPER_CASED;
670                }
671             }
672             else
673             {
674                command = IGNORE;
675             }
676             return command;
677          }
678       };
679
680       /**
681        * XML name to Java class name converter
682        */

683       XMLNameToJavaIdentifierConverter FIELD_NAME = new XMLNameToJavaIdentifierConverter()
684       {
685          public char firstCharacter(char ch, String JavaDoc str, int secondCharIndex)
686          {
687             if(Character.isLowerCase(ch))
688             {
689                return ch;
690             }
691             else
692             {
693                return (str.length() > secondCharIndex &&
694                   Character.isJavaIdentifierPart(str.charAt(secondCharIndex)) &&
695                   Character.isUpperCase(str.charAt(secondCharIndex))
696                   ) ?
697                   Character.toUpperCase(ch) :
698                   Character.toLowerCase(ch);
699             }
700          }
701
702          public byte commandForNext(char prev, char next, boolean ignoreLowLine)
703          {
704             return CLASS_NAME.commandForNext(prev, next, ignoreLowLine);
705          }
706       };
707
708       /**
709        * XML name to Java constant name converter
710        */

711       XMLNameToJavaIdentifierConverter CONSTANT_NAME = new XMLNameToJavaIdentifierConverter()
712       {
713          public char firstCharacter(char ch, String JavaDoc str, int secondCharIndex)
714          {
715             return Character.toUpperCase(ch);
716          }
717
718          public byte commandForNext(char prev, char next, boolean ignoreLowLine)
719          {
720             byte command;
721             if(Character.isDigit(next))
722             {
723                command = Character.isDigit(prev) ? APPEND : APPEND_UPPER_CASED_WITH_LOW_LINE;
724             }
725             else if(Character.isJavaIdentifierPart(next))
726             {
727                if(Character.isDigit(prev))
728                {
729                   command = APPEND_UPPER_CASED_WITH_LOW_LINE;
730                }
731                else if(Character.isJavaIdentifierPart(prev))
732                {
733                   command = Character.isUpperCase(next) ?
734                      (Character.isUpperCase(prev) ? APPEND_UPPER_CASED : APPEND_WITH_LOW_LINE) :
735                      APPEND_UPPER_CASED;
736                }
737                else
738                {
739                   command = APPEND_UPPER_CASED_WITH_LOW_LINE;
740                }
741             }
742             else
743             {
744                command = IGNORE;
745             }
746             return command;
747          }
748       };
749    }
750 }
751
Popular Tags