Source Code Cross Referenced for ConverterUtil.java in  » Web-Services-AXIS2 » adb » org » apache » axis2 » databinding » utils » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Services AXIS2 » adb » org.apache.axis2.databinding.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Licensed to the Apache Software Foundation (ASF) under one
0003:         * or more contributor license agreements. See the NOTICE file
0004:         * distributed with this work for additional information
0005:         * regarding copyright ownership. The ASF licenses this file
0006:         * to you under the Apache License, Version 2.0 (the
0007:         * "License"); you may not use this file except in compliance
0008:         * with the License. You may obtain a copy of the License at
0009:         *
0010:         * http://www.apache.org/licenses/LICENSE-2.0
0011:         *
0012:         * Unless required by applicable law or agreed to in writing,
0013:         * software distributed under the License is distributed on an
0014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015:         * KIND, either express or implied. See the License for the
0016:         * specific language governing permissions and limitations
0017:         * under the License.
0018:         */
0019:        package org.apache.axis2.databinding.utils;
0020:
0021:        import org.apache.axiom.attachments.ByteArrayDataSource;
0022:        import org.apache.axiom.attachments.utils.IOUtils;
0023:        import org.apache.axiom.om.OMAbstractFactory;
0024:        import org.apache.axiom.om.OMElement;
0025:        import org.apache.axiom.om.impl.builder.StAXOMBuilder;
0026:        import org.apache.axiom.om.util.Base64;
0027:        import org.apache.axiom.om.util.StAXUtils;
0028:        import org.apache.axis2.databinding.ADBBean;
0029:        import org.apache.axis2.databinding.i18n.ADBMessages;
0030:        import org.apache.axis2.databinding.types.*;
0031:
0032:        import javax.activation.DataHandler;
0033:        import javax.xml.namespace.QName;
0034:        import javax.xml.stream.XMLStreamException;
0035:        import javax.xml.stream.XMLStreamReader;
0036:        import javax.xml.stream.XMLStreamWriter;
0037:        import java.io.ByteArrayInputStream;
0038:        import java.io.InputStream;
0039:        import java.lang.reflect.Array;
0040:        import java.lang.reflect.Constructor;
0041:        import java.lang.reflect.Method;
0042:        import java.math.BigDecimal;
0043:        import java.math.BigInteger;
0044:        import java.text.ParseException;
0045:        import java.text.SimpleDateFormat;
0046:        import java.util.ArrayList;
0047:        import java.util.Calendar;
0048:        import java.util.Date;
0049:        import java.util.GregorianCalendar;
0050:        import java.util.List;
0051:        import java.util.TimeZone;
0052:
0053:        /**
0054:         * Converter methods to go from 1. simple type -> String 2. simple type -> Object 3. String ->
0055:         * simpletype 4. Object list -> array
0056:         */
0057:        public class ConverterUtil {
0058:            private static final String POSITIVE_INFINITY = "INF";
0059:            private static final String NEGATIVE_INFINITY = "-INF";
0060:
0061:            /* String conversion methods */
0062:            public static String convertToString(int i) {
0063:                return Integer.toString(i);
0064:            }
0065:
0066:            public static String convertToString(float i) {
0067:                return Float.toString(i);
0068:            }
0069:
0070:            public static String convertToString(long i) {
0071:                return Long.toString(i);
0072:            }
0073:
0074:            public static String convertToString(double i) {
0075:                return Double.toString(i);
0076:            }
0077:
0078:            public static String convertToString(byte i) {
0079:                return Byte.toString(i);
0080:            }
0081:
0082:            public static String convertToString(char i) {
0083:                return Character.toString(i);
0084:            }
0085:
0086:            public static String convertToString(short i) {
0087:                return Short.toString(i);
0088:            }
0089:
0090:            public static String convertToString(boolean i) {
0091:                return Boolean.toString(i);
0092:            }
0093:
0094:            public static String convertToString(Date value) {
0095:                // lexical form of the date is '-'? yyyy '-' mm '-' dd zzzzzz?
0096:                // we have to serialize it with the GMT timezone
0097:                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
0098:                        "yyyy-MM-dd'Z'");
0099:                simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
0100:                return simpleDateFormat.format(value);
0101:            }
0102:
0103:            public static String convertToString(Calendar value) {
0104:                // lexical form of the calendar is '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?
0105:                SimpleDateFormat zulu = new SimpleDateFormat(
0106:                        "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
0107:                zulu.setTimeZone(TimeZone.getTimeZone("GMT"));
0108:                // Sun JDK bug http://developer.java.sun.com/developer/bugParade/bugs/4229798.html
0109:                return zulu.format(value.getTime());
0110:            }
0111:
0112:            public static String convertToString(Day o) {
0113:                return o.toString();
0114:            }
0115:
0116:            public static String convertToString(YearMonth o) {
0117:                return o.toString();
0118:            }
0119:
0120:            public static String convertToString(Year o) {
0121:                return o.toString();
0122:            }
0123:
0124:            public static String convertToString(HexBinary o) {
0125:                return o.toString();
0126:            }
0127:
0128:            public static String convertToString(MonthDay o) {
0129:                return o.toString();
0130:            }
0131:
0132:            public static String convertToString(Time o) {
0133:                return o.toString();
0134:            }
0135:
0136:            public static String convertToString(Byte o) {
0137:                return o.toString();
0138:            }
0139:
0140:            public static String convertToString(BigInteger o) {
0141:                return o.toString();
0142:            }
0143:
0144:            public static String convertToString(Integer o) {
0145:                return o.toString();
0146:            }
0147:
0148:            public static String convertToString(Long o) {
0149:                return o.toString();
0150:            }
0151:
0152:            public static String convertToString(Short o) {
0153:                return o.toString();
0154:            }
0155:
0156:            public static String convertToString(UnsignedByte o) {
0157:                return o.toString();
0158:            }
0159:
0160:            public static String convertToString(UnsignedInt o) {
0161:                return o.toString();
0162:            }
0163:
0164:            public static String convertToString(UnsignedLong o) {
0165:                return o.toString();
0166:            }
0167:
0168:            public static String convertToString(QName o) {
0169:                if (o != null) {
0170:                    return o.getLocalPart();
0171:                } else {
0172:                    return "";
0173:                }
0174:            }
0175:
0176:            public static String convertToString(Object o) {
0177:                return o.toString();
0178:            }
0179:
0180:            public static String convertToString(Double o) {
0181:                return o.toString();
0182:            }
0183:
0184:            public static String convertToString(Duration o) {
0185:                return o.toString();
0186:            }
0187:
0188:            public static String convertToString(Float o) {
0189:                return o.toString();
0190:            }
0191:
0192:            public static String convertToString(Month o) {
0193:                return o.toString();
0194:            }
0195:
0196:            public static String convertToString(byte[] bytes) {
0197:                return Base64.encode(bytes);
0198:            }
0199:
0200:            public static String convertToString(
0201:                    javax.activation.DataHandler handler) {
0202:                return getStringFromDatahandler(handler);
0203:            }
0204:
0205:            /* ################################################################################ */
0206:            /* String to java type conversions
0207:               These methods have a special signature structure
0208:               <code>convertTo</code> followed by the schema type name
0209:               Say for int, convertToint(String) is the converter method
0210:
0211:               Not very elegant but it seems to be the only way!
0212:
0213:             */
0214:
0215:            public static int convertToInt(String s) {
0216:                if (s.startsWith("+")) {
0217:                    s = s.substring(1);
0218:                }
0219:                return Integer.parseInt(s);
0220:            }
0221:
0222:            public static BigDecimal convertToBigDecimal(String s) {
0223:                if (s.startsWith("+")) {
0224:                    s = s.substring(1);
0225:                }
0226:                return new BigDecimal(s);
0227:            }
0228:
0229:            public static double convertToDouble(String s) {
0230:                if (s.startsWith("+")) {
0231:                    s = s.substring(1);
0232:                }
0233:                if (POSITIVE_INFINITY.equals(s)) {
0234:                    return Double.POSITIVE_INFINITY;
0235:                } else if (NEGATIVE_INFINITY.equals(s)) {
0236:                    return Double.NEGATIVE_INFINITY;
0237:                }
0238:                return Double.parseDouble(s);
0239:            }
0240:
0241:            public static BigDecimal convertToDecimal(String s) {
0242:                if (s.startsWith("+")) {
0243:                    s = s.substring(1);
0244:                }
0245:                return new BigDecimal(s);
0246:            }
0247:
0248:            public static float convertToFloat(String s) {
0249:                if (s.startsWith("+")) {
0250:                    s = s.substring(1);
0251:                }
0252:                if (POSITIVE_INFINITY.equals(s)) {
0253:                    return Float.POSITIVE_INFINITY;
0254:                } else if (NEGATIVE_INFINITY.equals(s)) {
0255:                    return Float.NEGATIVE_INFINITY;
0256:                }
0257:                return Float.parseFloat(s);
0258:            }
0259:
0260:            public static String convertToString(String s) {
0261:                return s;
0262:            }
0263:
0264:            public static long convertToLong(String s) {
0265:                if (s.startsWith("+")) {
0266:                    s = s.substring(1);
0267:                }
0268:                return Long.parseLong(s);
0269:            }
0270:
0271:            public static short convertToShort(String s) {
0272:                if (s.startsWith("+")) {
0273:                    s = s.substring(1);
0274:                }
0275:                return Short.parseShort(s);
0276:            }
0277:
0278:            public static boolean convertToBoolean(String s) {
0279:
0280:                boolean returnValue = false;
0281:                if ((s != null) && (s.length() > 0)) {
0282:                    if ("1".equals(s) || s.toLowerCase().equals("true")) {
0283:                        returnValue = true;
0284:                    } else if (!"0".equals(s)
0285:                            && !s.toLowerCase().equals("false")) {
0286:                        throw new RuntimeException("in valid string -" + s
0287:                                + " for boolean value");
0288:                    }
0289:                }
0290:                return returnValue;
0291:            }
0292:
0293:            public static String convertToAnySimpleType(String s) {
0294:                return s;
0295:            }
0296:
0297:            public static OMElement convertToAnyType(String s) {
0298:                try {
0299:                    XMLStreamReader r = StAXUtils
0300:                            .createXMLStreamReader(new ByteArrayInputStream(s
0301:                                    .getBytes()));
0302:                    StAXOMBuilder builder = new StAXOMBuilder(OMAbstractFactory
0303:                            .getOMFactory(), r);
0304:                    return builder.getDocumentElement();
0305:                } catch (XMLStreamException e) {
0306:                    return null;
0307:                }
0308:            }
0309:
0310:            public static YearMonth convertToGYearMonth(String s) {
0311:                return new YearMonth(s);
0312:            }
0313:
0314:            public static MonthDay convertToGMonthDay(String s) {
0315:                return new MonthDay(s);
0316:            }
0317:
0318:            public static Year convertToGYear(String s) {
0319:                return new Year(s);
0320:            }
0321:
0322:            public static Month convertToGMonth(String s) {
0323:                return new Month(s);
0324:            }
0325:
0326:            public static Day convertToGDay(String s) {
0327:                return new Day(s);
0328:            }
0329:
0330:            public static Duration convertToDuration(String s) {
0331:                return new Duration(s);
0332:            }
0333:
0334:            public static HexBinary convertToHexBinary(String s) {
0335:                return new HexBinary(s);
0336:            }
0337:
0338:            public static javax.activation.DataHandler convertToBase64Binary(
0339:                    String s) {
0340:                // reusing the byteArrayDataSource from the Axiom classes
0341:                ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource(
0342:                        Base64.decode(s));
0343:                return new DataHandler(byteArrayDataSource);
0344:            }
0345:
0346:            public static javax.activation.DataHandler convertToDataHandler(
0347:                    String s) {
0348:                return convertToBase64Binary(s);
0349:            }
0350:
0351:            /**
0352:             * Converts a given string into a date. Code from Axis1 DateDeserializer.
0353:             *
0354:             * @param source
0355:             * @return Returns Date.
0356:             */
0357:            public static Date convertToDate(String source) {
0358:
0359:                // the lexical form of the date is '-'? yyyy '-' mm '-' dd zzzzzz?
0360:                Calendar calendar = Calendar.getInstance();
0361:                SimpleDateFormat simpleDateFormat = null;
0362:                boolean bc = false;
0363:                if (source.startsWith("-")) {
0364:                    source = source.substring(1);
0365:                    bc = true;
0366:                }
0367:
0368:                if ((source != null) && (source.length() >= 10)) {
0369:                    if (source.length() == 10) {
0370:                        //i.e this stirng has only the compulsory part
0371:                        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
0372:                    } else {
0373:                        String restpart = source.substring(10);
0374:                        if (restpart.startsWith("Z")) {
0375:                            // this is a gmt time zone value
0376:                            simpleDateFormat = new SimpleDateFormat(
0377:                                    "yyyy-MM-dd'Z'");
0378:                            simpleDateFormat.setTimeZone(TimeZone
0379:                                    .getTimeZone("GMT"));
0380:                        } else if (restpart.startsWith("+")
0381:                                || restpart.startsWith("-")) {
0382:                            // this is a specific time format string
0383:                            simpleDateFormat = new SimpleDateFormat(
0384:                                    "yyyy-MM-ddz");
0385:                            // have to add the GMT part to process the message
0386:                            source = source.substring(0, 10) + "GMT" + restpart;
0387:                        } else {
0388:                            throw new RuntimeException("In valid string sufix");
0389:                        }
0390:                    }
0391:                } else {
0392:                    throw new RuntimeException("In valid string to parse");
0393:                }
0394:
0395:                Date date;
0396:                try {
0397:                    date = simpleDateFormat.parse(source);
0398:                    if (bc) {
0399:                        calendar.setTime(date);
0400:                        calendar.set(Calendar.ERA, GregorianCalendar.BC);
0401:                        date = calendar.getTime();
0402:                    }
0403:                } catch (ParseException e) {
0404:                    throw new RuntimeException("In valid string to parse");
0405:                }
0406:
0407:                return date;
0408:            }
0409:
0410:            public static Time convertToTime(String s) {
0411:                return new Time(s);
0412:            }
0413:
0414:            public static Token convertToToken(String s) {
0415:                return new Token(s);
0416:            }
0417:
0418:            public static NormalizedString convertToNormalizedString(String s) {
0419:                return new NormalizedString(s);
0420:            }
0421:
0422:            public static UnsignedLong convertToUnsignedLong(String s) {
0423:                if (s.startsWith("+")) {
0424:                    s = s.substring(1);
0425:                }
0426:                return new UnsignedLong(s);
0427:            }
0428:
0429:            public static UnsignedInt convertToUnsignedInt(String s) {
0430:                if (s.startsWith("+")) {
0431:                    s = s.substring(1);
0432:                }
0433:                return new UnsignedInt(s);
0434:            }
0435:
0436:            public static UnsignedShort convertToUnsignedShort(String s) {
0437:                if (s.startsWith("+")) {
0438:                    s = s.substring(1);
0439:                }
0440:                return new UnsignedShort(s);
0441:            }
0442:
0443:            public static UnsignedByte convertToUnsignedByte(String s) {
0444:                if (s.startsWith("+")) {
0445:                    s = s.substring(1);
0446:                }
0447:                return new UnsignedByte(s);
0448:            }
0449:
0450:            public static NonNegativeInteger convertToNonNegativeInteger(
0451:                    String s) {
0452:                if (s.startsWith("+")) {
0453:                    s = s.substring(1);
0454:                }
0455:                return new NonNegativeInteger(s);
0456:            }
0457:
0458:            public static NegativeInteger convertToNegativeInteger(String s) {
0459:                if (s.startsWith("+")) {
0460:                    s = s.substring(1);
0461:                }
0462:                return new NegativeInteger(s);
0463:            }
0464:
0465:            public static PositiveInteger convertToPositiveInteger(String s) {
0466:                if (s.startsWith("+")) {
0467:                    s = s.substring(1);
0468:                }
0469:                return new PositiveInteger(s);
0470:            }
0471:
0472:            public static NonPositiveInteger convertToNonPositiveInteger(
0473:                    String s) {
0474:                if (s.startsWith("+")) {
0475:                    s = s.substring(1);
0476:                }
0477:                return new NonPositiveInteger(s);
0478:            }
0479:
0480:            public static Name convertToName(String s) {
0481:                return new Name(s);
0482:            }
0483:
0484:            public static NCName convertToNCName(String s) {
0485:                return new NCName(s);
0486:            }
0487:
0488:            public static Id convertToID(String s) {
0489:                return new Id(s);
0490:            }
0491:
0492:            public static Id convertToId(String s) {
0493:                return convertToID(s);
0494:            }
0495:
0496:            public static Language convertToLanguage(String s) {
0497:                return new Language(s);
0498:            }
0499:
0500:            public static NMToken convertToNMTOKEN(String s) {
0501:                return new NMToken(s);
0502:            }
0503:
0504:            public static NMTokens convertToNMTOKENS(String s) {
0505:                return new NMTokens(s);
0506:            }
0507:
0508:            public static Notation convertToNOTATION(String s) {
0509:                return null; //todo Need to fix this
0510:                // return new Notation(s);
0511:            }
0512:
0513:            public static Entity convertToENTITY(String s) {
0514:                return new Entity(s);
0515:            }
0516:
0517:            public static Entities convertToENTITIES(String s) {
0518:                return new Entities(s);
0519:            }
0520:
0521:            public static IDRef convertToIDREF(String s) {
0522:                return new IDRef(s);
0523:            }
0524:
0525:            public static IDRefs convertToIDREFS(String s) {
0526:                return new IDRefs(s);
0527:            }
0528:
0529:            public static URI convertToURI(String s) {
0530:                return convertToAnyURI(s);
0531:            }
0532:
0533:            public static URI convertToAnyURI(String s) {
0534:                try {
0535:                    return new URI(s);
0536:                } catch (URI.MalformedURIException e) {
0537:                    throw new ObjectConversionException(ADBMessages.getMessage(
0538:                            "converter.cannotParse", s), e);
0539:                }
0540:            }
0541:
0542:            public static BigInteger convertToInteger(String s) {
0543:                if (s.startsWith("+")) {
0544:                    s = s.substring(1);
0545:                }
0546:                return new BigInteger(s);
0547:            }
0548:
0549:            public static BigInteger convertToBigInteger(String s) {
0550:                if (s.startsWith("+")) {
0551:                    s = s.substring(1);
0552:                }
0553:                return convertToInteger(s);
0554:            }
0555:
0556:            public static byte convertToByte(String s) {
0557:                return Byte.parseByte(s);
0558:            }
0559:
0560:            /**
0561:             * Code from Axis1 code base Note - We only follow the convention in the latest schema spec
0562:             *
0563:             * @param source
0564:             * @return Returns Calendar.
0565:             */
0566:            public static Calendar convertToDateTime(String source) {
0567:
0568:                // the lexical representation of the date time as follows
0569:                // '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?
0570:                SimpleDateFormat simpleDateFormat = null;
0571:                Date date = null;
0572:                Calendar calendar = Calendar.getInstance();
0573:
0574:                if (source.startsWith("-")) {
0575:                    source = source.substring(1);
0576:                    calendar.set(Calendar.ERA, GregorianCalendar.BC);
0577:                }
0578:
0579:                try {
0580:                    if ((source != null) && (source.length() >= 19)) {
0581:                        if (source.length() == 19) {
0582:                            // i.e. this does not have any additional assume this time in current local
0583:                            simpleDateFormat = new SimpleDateFormat(
0584:                                    "yyyy-MM-dd'T'HH:mm:ss");
0585:
0586:                        } else {
0587:                            String rest = source.substring(19);
0588:                            if (rest.startsWith(".")) {
0589:                                // i.e this have the ('.'s+) part
0590:                                if (rest.endsWith("Z")) {
0591:                                    // this is in gmt time zone
0592:                                    simpleDateFormat = new SimpleDateFormat(
0593:                                            "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'");
0594:                                    simpleDateFormat.setTimeZone(TimeZone
0595:                                            .getTimeZone("GMT"));
0596:
0597:                                } else if ((rest.lastIndexOf("+") > 0)
0598:                                        || (rest.lastIndexOf("-") > 0)) {
0599:                                    // this is given in a general time zione
0600:                                    simpleDateFormat = new SimpleDateFormat(
0601:                                            "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSz");
0602:                                    if (rest.lastIndexOf("+") > 0) {
0603:                                        source = source.substring(0, source
0604:                                                .lastIndexOf("+"))
0605:                                                + "GMT"
0606:                                                + rest.substring(rest
0607:                                                        .lastIndexOf("+"));
0608:                                    } else if (rest.lastIndexOf("-") > 0) {
0609:                                        source = source.substring(0, source
0610:                                                .lastIndexOf("-"))
0611:                                                + "GMT"
0612:                                                + rest.substring(rest
0613:                                                        .lastIndexOf("-"));
0614:                                    }
0615:
0616:                                } else {
0617:                                    // i.e it does not have time zone
0618:                                    simpleDateFormat = new SimpleDateFormat(
0619:                                            "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS");
0620:                                }
0621:
0622:                            } else {
0623:                                if (rest.startsWith("Z")) {
0624:                                    // this is in gmt time zone
0625:                                    simpleDateFormat = new SimpleDateFormat(
0626:                                            "yyyy-MM-dd'T'HH:mm:ss'Z'");
0627:                                    simpleDateFormat.setTimeZone(TimeZone
0628:                                            .getTimeZone("GMT"));
0629:                                } else if (rest.startsWith("+")
0630:                                        || rest.startsWith("-")) {
0631:                                    // this is given in a general time zione
0632:                                    simpleDateFormat = new SimpleDateFormat(
0633:                                            "yyyy-MM-dd'T'HH:mm:ssZ");
0634:                                    source = source.substring(0, 19) + "GMT"
0635:                                            + rest;
0636:                                } else {
0637:                                    throw new NumberFormatException(
0638:                                            "in valid time zone attribute");
0639:                                }
0640:                            }
0641:                        }
0642:                        date = simpleDateFormat.parse(source);
0643:                        calendar.setTime(date);
0644:
0645:                    } else {
0646:                        throw new NumberFormatException(
0647:                                "date string can not be less than 19 charactors");
0648:                    }
0649:                } catch (ParseException e) {
0650:                    throw new NumberFormatException(e.getMessage());
0651:                }
0652:                return calendar;
0653:            }
0654:
0655:            /**
0656:             * Code from Axis1 code base
0657:             *
0658:             * @param source
0659:             * @return Returns QName.
0660:             */
0661:            public static QName convertToQName(String source,
0662:                    String nameSpaceuri) {
0663:                source = source.trim();
0664:                int colon = source.lastIndexOf(":");
0665:                //context.getNamespaceURI(source.substring(0, colon));
0666:                String localPart = colon < 0 ? source : source
0667:                        .substring(colon + 1);
0668:                String perfix = colon <= 0 ? "" : source.substring(0, colon);
0669:                return new QName(nameSpaceuri, localPart, perfix);
0670:            }
0671:
0672:            /* ################################################################# */
0673:
0674:            /* java Primitive types to Object conversion methods */
0675:            public static Object convertToObject(String i) {
0676:                return i;
0677:            }
0678:
0679:            public static Object convertToObject(boolean i) {
0680:                return Boolean.valueOf(i);
0681:            }
0682:
0683:            public static Object convertToObject(double i) {
0684:                return new Double(i);
0685:            }
0686:
0687:            public static Object convertToObject(byte i) {
0688:                return new Byte(i);
0689:            }
0690:
0691:            public static Object convertToObject(char i) {
0692:                return new Character(i);
0693:            }
0694:
0695:            public static Object convertToObject(short i) {
0696:                return new Short(i);
0697:            }
0698:
0699:            /* list to array conversion methods */
0700:
0701:            public static Object convertToArray(Class baseArrayClass,
0702:                    String[] valueArray) {
0703:                //create a list using the string array
0704:                List valuesList = new ArrayList(valueArray.length);
0705:                for (int i = 0; i < valueArray.length; i++) {
0706:                    valuesList.add(valueArray[i]);
0707:
0708:                }
0709:
0710:                return convertToArray(baseArrayClass, valuesList);
0711:            }
0712:
0713:            /**
0714:             * @param baseArrayClass
0715:             * @param objectList     -> for primitive type array conversion we assume the content to be
0716:             *                       strings!
0717:             * @return Returns Object.
0718:             */
0719:            public static Object convertToArray(Class baseArrayClass,
0720:                    List objectList) {
0721:                int listSize = objectList.size();
0722:                Object returnArray = null;
0723:                if (int.class.equals(baseArrayClass)) {
0724:                    int[] array = new int[listSize];
0725:                    for (int i = 0; i < listSize; i++) {
0726:                        Object o = objectList.get(i);
0727:                        if (o != null) {
0728:                            array[i] = Integer.parseInt(o.toString());
0729:                        } else {
0730:                            array[i] = Integer.MIN_VALUE;
0731:                        }
0732:                    }
0733:                    returnArray = array;
0734:                } else if (float.class.equals(baseArrayClass)) {
0735:                    float[] array = new float[listSize];
0736:                    for (int i = 0; i < listSize; i++) {
0737:                        Object o = objectList.get(i);
0738:                        if (o != null) {
0739:                            array[i] = Float.parseFloat(o.toString());
0740:                        } else {
0741:                            array[i] = Float.NaN;
0742:                        }
0743:                    }
0744:                    returnArray = array;
0745:                } else if (short.class.equals(baseArrayClass)) {
0746:                    short[] array = new short[listSize];
0747:                    for (int i = 0; i < listSize; i++) {
0748:                        Object o = objectList.get(i);
0749:                        if (o != null) {
0750:                            array[i] = Short.parseShort(o.toString());
0751:                        } else {
0752:                            array[i] = Short.MIN_VALUE;
0753:                        }
0754:                    }
0755:                    returnArray = array;
0756:                } else if (byte.class.equals(baseArrayClass)) {
0757:                    byte[] array = new byte[listSize];
0758:                    for (int i = 0; i < listSize; i++) {
0759:                        Object o = objectList.get(i);
0760:                        if (o != null) {
0761:                            array[i] = Byte.parseByte(o.toString());
0762:                        } else {
0763:                            array[i] = Byte.MIN_VALUE;
0764:                        }
0765:                    }
0766:                    returnArray = array;
0767:                } else if (long.class.equals(baseArrayClass)) {
0768:                    long[] array = new long[listSize];
0769:                    for (int i = 0; i < listSize; i++) {
0770:                        Object o = objectList.get(i);
0771:                        if (o != null) {
0772:                            array[i] = Long.parseLong(o.toString());
0773:                        } else {
0774:                            array[i] = Long.MIN_VALUE;
0775:                        }
0776:                    }
0777:                    returnArray = array;
0778:                } else if (boolean.class.equals(baseArrayClass)) {
0779:                    boolean[] array = new boolean[listSize];
0780:                    for (int i = 0; i < listSize; i++) {
0781:                        Object o = objectList.get(i);
0782:                        if (o != null) {
0783:                            array[i] = Boolean.getBoolean(o.toString());
0784:                        }
0785:                    }
0786:                    returnArray = array;
0787:                } else if (char.class.equals(baseArrayClass)) {
0788:                    char[] array = new char[listSize];
0789:                    for (int i = 0; i < listSize; i++) {
0790:                        Object o = objectList.get(i);
0791:                        if (o != null) {
0792:                            array[i] = o.toString().toCharArray()[0];
0793:                        }
0794:                    }
0795:                    returnArray = array;
0796:                } else if (double.class.equals(baseArrayClass)) {
0797:                    double[] array = new double[listSize];
0798:                    for (int i = 0; i < listSize; i++) {
0799:                        Object o = objectList.get(i);
0800:                        if (o != null) {
0801:                            array[i] = Double.parseDouble(o.toString());
0802:                        } else {
0803:                            array[i] = Double.NaN;
0804:                        }
0805:                    }
0806:                    returnArray = array;
0807:                } else if (Calendar.class.equals(baseArrayClass)) {
0808:                    Calendar[] array = new Calendar[listSize];
0809:                    for (int i = 0; i < listSize; i++) {
0810:                        Object o = objectList.get(i);
0811:                        if (o != null) {
0812:                            if (o instanceof  String) {
0813:                                array[i] = ConverterUtil.convertToDateTime(o
0814:                                        .toString());
0815:                            } else if (o instanceof  Calendar) {
0816:                                array[i] = (Calendar) o;
0817:                            }
0818:                        }
0819:                    }
0820:                    returnArray = array;
0821:                } else {
0822:                    returnArray = Array.newInstance(baseArrayClass, listSize);
0823:                    ConvertToArbitraryObjectArray(returnArray, baseArrayClass,
0824:                            objectList);
0825:                }
0826:                return returnArray;
0827:            }
0828:
0829:            /**
0830:             * @param returnArray
0831:             * @param baseArrayClass
0832:             * @param objectList
0833:             */
0834:            private static void ConvertToArbitraryObjectArray(
0835:                    Object returnArray, Class baseArrayClass, List objectList) {
0836:                if (!(ADBBean.class.isAssignableFrom(baseArrayClass))) {
0837:                    try {
0838:                        for (int i = 0; i < objectList.size(); i++) {
0839:                            Object o = objectList.get(i);
0840:                            if (o == null) {
0841:                                // if the string is null the object value must be null
0842:                                Array.set(returnArray, i, null);
0843:                            } else {
0844:                                Array.set(returnArray, i, getObjectForClass(
0845:                                        baseArrayClass, o.toString()));
0846:                            }
0847:
0848:                        }
0849:                        return;
0850:                    } catch (Exception e) {
0851:                        //oops! - this cannot be converted fall through and
0852:                        //try the other alternative
0853:                    }
0854:                }
0855:
0856:                try {
0857:                    objectList.toArray((Object[]) returnArray);
0858:                } catch (Exception e) {
0859:                    //we are over with alternatives - throw the
0860:                    //converison exception
0861:                    throw new ObjectConversionException(e);
0862:                }
0863:            }
0864:
0865:            /**
0866:             * We could have used the Arraya.asList() method but that returns an *immutable* list !!!!!
0867:             *
0868:             * @param array
0869:             * @return list
0870:             */
0871:            public static List toList(Object[] array) {
0872:                if (array == null) {
0873:                    return new ArrayList();
0874:                } else {
0875:                    ArrayList list = new ArrayList();
0876:                    for (int i = 0; i < array.length; i++) {
0877:                        list.add(array[i]);
0878:                    }
0879:                    return list;
0880:                }
0881:            }
0882:
0883:            /**
0884:             * @param intValue
0885:             * @param value
0886:             * @return 0 if equal , + value if greater than , - value if less than
0887:             */
0888:            public static int compare(int intValue, String value) {
0889:                return intValue - Integer.parseInt(value);
0890:            }
0891:
0892:            /**
0893:             * @param doubleValue
0894:             * @param value
0895:             * @return 0 if equal , + value if greater than , - value if less than
0896:             */
0897:            public static double compare(double doubleValue, String value) {
0898:                return doubleValue - Double.parseDouble(value);
0899:            }
0900:
0901:            /**
0902:             * @param floatValue
0903:             * @param value
0904:             * @return 0 if equal , + value if greater than , - value if less than
0905:             */
0906:            public static float compare(float floatValue, String value) {
0907:                return floatValue - Float.parseFloat(value);
0908:            }
0909:
0910:            /**
0911:             * @param longValue
0912:             * @param value
0913:             * @return 0 if equal , + value if greater than , - value if less than
0914:             */
0915:            public static long compare(long longValue, String value) {
0916:                return longValue - Long.parseLong(value);
0917:            }
0918:
0919:            /**
0920:             * @param shortValue
0921:             * @param value
0922:             * @return 0 if equal , + value if greater than , - value if less than
0923:             */
0924:            public static int compare(short shortValue, String value) {
0925:                return shortValue - Short.parseShort(value);
0926:            }
0927:
0928:            /**
0929:             * @param byteVlaue
0930:             * @param value
0931:             * @return 0 if equal , + value if greater than , - value if less than
0932:             */
0933:            public static int compare(byte byteVlaue, String value) {
0934:                return byteVlaue - Byte.parseByte(value);
0935:            }
0936:
0937:            /**
0938:             * @param binBigInteger
0939:             * @param value
0940:             * @return 0 if equal , + value if greater than , - value if less than
0941:             */
0942:            public static int compare(BigInteger binBigInteger, String value) {
0943:                return binBigInteger.intValue() - Integer.parseInt(value);
0944:            }
0945:
0946:            /**
0947:             * @param binBigDecimal
0948:             * @param value
0949:             * @return 0 if equal , + value if greater than , - value if less than
0950:             */
0951:            public static double compare(BigDecimal binBigDecimal, String value) {
0952:                return binBigDecimal.doubleValue() - Double.parseDouble(value);
0953:            }
0954:
0955:            public static long compare(Duration duration, String value) {
0956:                Duration compareValue = new Duration(value);
0957:                return duration.compare(compareValue);
0958:            }
0959:
0960:            public static long compare(Date date, String value) {
0961:                Date newDate = convertToDate(value);
0962:                return date.getTime() - newDate.getTime();
0963:            }
0964:
0965:            public static long compare(Time time, String value) {
0966:                Time newTime = new Time(value);
0967:                return time.getAsCalendar().getTimeInMillis()
0968:                        - newTime.getAsCalendar().getTimeInMillis();
0969:            }
0970:
0971:            public static long compare(Calendar calendar, String value) {
0972:                Calendar newCalendar = convertToDateTime(value);
0973:                return calendar.getTimeInMillis()
0974:                        - newCalendar.getTimeInMillis();
0975:            }
0976:
0977:            /**
0978:             * Converts the given .datahandler to a string
0979:             *
0980:             * @return string
0981:             */
0982:            public static String getStringFromDatahandler(
0983:                    DataHandler dataHandler) {
0984:                try {
0985:                    InputStream inStream;
0986:                    if (dataHandler == null) {
0987:                        return "";
0988:                    }
0989:                    inStream = dataHandler.getDataSource().getInputStream();
0990:                    byte[] data = IOUtils.getStreamAsByteArray(inStream);
0991:                    return Base64.encode(data);
0992:                } catch (Exception e) {
0993:                    throw new RuntimeException(e);
0994:                }
0995:            }
0996:
0997:            /**
0998:             * A reflection based method to generate an instance of a given class and populate it with a
0999:             * given value
1000:             *
1001:             * @param clazz
1002:             * @param value
1003:             * @return object
1004:             */
1005:            public static Object getObjectForClass(Class clazz, String value) {
1006:                //first see whether this class has a constructor that can
1007:                //take the string as an argument.
1008:                boolean continueFlag = false;
1009:                try {
1010:                    Constructor stringConstructor = clazz
1011:                            .getConstructor(new Class[] { String.class });
1012:                    return stringConstructor
1013:                            .newInstance(new Object[] { value });
1014:                } catch (NoSuchMethodException e) {
1015:                    //oops - no such constructors - continue with the
1016:                    //parse method
1017:                    continueFlag = true;
1018:                } catch (Exception e) {
1019:                    throw new ObjectConversionException(ADBMessages.getMessage(
1020:                            "converter.cannotGenerate", clazz.getName()), e);
1021:                }
1022:
1023:                if (!continueFlag) {
1024:                    throw new ObjectConversionException(ADBMessages.getMessage(
1025:                            "converter.cannotConvert", clazz.getName()));
1026:                }
1027:
1028:                try {
1029:                    Method parseMethod = clazz.getMethod("parse",
1030:                            new Class[] { String.class });
1031:                    Object instance = clazz.newInstance();
1032:                    return parseMethod.invoke(instance, new Object[] { value });
1033:                } catch (NoSuchMethodException e) {
1034:                    throw new ObjectConversionException(e);
1035:                } catch (Exception e) {
1036:                    throw new ObjectConversionException(ADBMessages.getMessage(
1037:                            "converter.cannotGenerate", clazz.getName()), e);
1038:                }
1039:
1040:            }
1041:
1042:            /** A simple exception that is thrown when the conversion fails */
1043:            public static class ObjectConversionException extends
1044:                    RuntimeException {
1045:                public ObjectConversionException() {
1046:                }
1047:
1048:                public ObjectConversionException(String message) {
1049:                    super (message);
1050:                }
1051:
1052:                public ObjectConversionException(Throwable cause) {
1053:                    super (cause);
1054:                }
1055:
1056:                public ObjectConversionException(String message, Throwable cause) {
1057:                    super (message, cause);
1058:                }
1059:
1060:            }
1061:
1062:            // serialization methods for xsd any type
1063:
1064:            /**
1065:             * serialize the any type string object
1066:             * @param string
1067:             * @param xmlStreamWriter
1068:             */
1069:            public static void serializeAnyType(String string,
1070:                    XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
1071:                if (xmlStreamWriter.getPrefix(Constants.XSI_NAMESPACE) == null) {
1072:                    String prefix = BeanUtil.getUniquePrefix();
1073:                    xmlStreamWriter.writeNamespace(prefix,
1074:                            Constants.XSI_NAMESPACE);
1075:                    xmlStreamWriter.setPrefix(prefix, Constants.XSI_NAMESPACE);
1076:                }
1077:
1078:            }
1079:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.