Source Code Cross Referenced for SampleXmlUtil.java in  » IDE-Netbeans » compapp » org » netbeans » modules » compapp » test » wsdl » 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 » IDE Netbeans » compapp » org.netbeans.modules.compapp.test.wsdl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003:         *
0004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005:         *
0006:         * The contents of this file are subject to the terms of either the GNU
0007:         * General Public License Version 2 only ("GPL") or the Common
0008:         * Development and Distribution License("CDDL") (collectively, the
0009:         * "License"). You may not use this file except in compliance with the
0010:         * License. You can obtain a copy of the License at
0011:         * http://www.netbeans.org/cddl-gplv2.html
0012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013:         * specific language governing permissions and limitations under the
0014:         * License.  When distributing the software, include this License Header
0015:         * Notice in each file and include the License file at
0016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
0017:         * particular file as subject to the "Classpath" exception as provided
0018:         * by Sun in the GPL Version 2 section of the License file that
0019:         * accompanied this code. If applicable, add the following below the
0020:         * License Header, with the fields enclosed by brackets [] replaced by
0021:         * your own identifying information:
0022:         * "Portions Copyrighted [year] [name of copyright owner]"
0023:         *
0024:         * Contributor(s):
0025:         *
0026:         * The Original Software is NetBeans. The Initial Developer of the Original
0027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
0028:         * Microsystems, Inc. All Rights Reserved.
0029:         *
0030:         * If you wish your version of this file to be governed by only the CDDL
0031:         * or only the GPL Version 2, indicate your decision by adding
0032:         * "[Contributor] elects to include this software in this distribution
0033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034:         * single choice of license, a recipient has the option to distribute
0035:         * your version of this file under either the CDDL, the GPL Version 2 or
0036:         * to extend the choice of license to its licensees as provided above.
0037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
0038:         * Version 2 license, then the option applies only if the new code is
0039:         * made subject to such option by the copyright holder.
0040:         */
0041:
0042:        package org.netbeans.modules.compapp.test.wsdl;
0043:
0044:        /*
0045:         * XmlBeans class for generating XML from XML Schemas
0046:         *
0047:         * TODO:
0048:         *  Comment on enumerations?
0049:         *  Comment on facets?
0050:         *  Have a verbose option?
0051:         *  Have a sample data option, would create valid instance with sample data?
0052:         *  Add the pattern facet; this is tricky, considering the relationship with length
0053:         */
0054:
0055:        import java.math.BigDecimal;
0056:        import java.math.BigInteger;
0057:        import java.util.ArrayList;
0058:        import java.util.Arrays;
0059:        import java.util.Calendar;
0060:        import java.util.Date;
0061:        import java.util.HashSet;
0062:        import java.util.Random;
0063:        import java.util.Set;
0064:
0065:        import javax.xml.namespace.QName;
0066:
0067:        import org.apache.xmlbeans.GDate;
0068:        import org.apache.xmlbeans.GDateBuilder;
0069:        import org.apache.xmlbeans.GDuration;
0070:        import org.apache.xmlbeans.GDurationBuilder;
0071:        import org.apache.xmlbeans.SchemaLocalElement;
0072:        import org.apache.xmlbeans.SchemaParticle;
0073:        import org.apache.xmlbeans.SchemaProperty;
0074:        import org.apache.xmlbeans.SchemaType;
0075:        import org.apache.xmlbeans.SimpleValue;
0076:        import org.apache.xmlbeans.XmlAnySimpleType;
0077:        import org.apache.xmlbeans.XmlCursor;
0078:        import org.apache.xmlbeans.XmlDate;
0079:        import org.apache.xmlbeans.XmlDateTime;
0080:        import org.apache.xmlbeans.XmlDecimal;
0081:        import org.apache.xmlbeans.XmlDuration;
0082:        import org.apache.xmlbeans.XmlGDay;
0083:        import org.apache.xmlbeans.XmlGMonth;
0084:        import org.apache.xmlbeans.XmlGMonthDay;
0085:        import org.apache.xmlbeans.XmlGYear;
0086:        import org.apache.xmlbeans.XmlGYearMonth;
0087:        import org.apache.xmlbeans.XmlInteger;
0088:        import org.apache.xmlbeans.XmlObject;
0089:        import org.apache.xmlbeans.XmlOptions;
0090:        import org.apache.xmlbeans.XmlTime;
0091:        import org.apache.xmlbeans.impl.util.Base64;
0092:        import org.apache.xmlbeans.impl.util.HexBin;
0093:        import org.apache.xmlbeans.soap.SOAPArrayType;
0094:        import org.apache.xmlbeans.soap.SchemaWSDLArrayType;
0095:        import org.openide.util.NbBundle;
0096:
0097:        public class SampleXmlUtil {
0098:            private boolean _soapEnc;
0099:
0100:            public SampleXmlUtil(boolean soapEnc) {
0101:                _soapEnc = soapEnc;
0102:            }
0103:
0104:            public boolean isSoapEnc() {
0105:                return _soapEnc;
0106:            }
0107:
0108:            public static String createSampleForType(SchemaType sType) {
0109:                XmlObject object = XmlObject.Factory.newInstance();
0110:                XmlCursor cursor = object.newCursor();
0111:                // Skip the document node
0112:                cursor.toNextToken();
0113:                // Using the type and the cursor, call the utility method to get a
0114:                // sample XML payload for that Schema element
0115:                new SampleXmlUtil(false).createSampleForType(sType, cursor);
0116:                // Cursor now contains the sample payload
0117:                // Pretty print the result.  Note that the cursor is positioned at the
0118:                // end of the doc so we use the original xml object that the cursor was
0119:                // created upon to do the xmlText() against.
0120:                XmlOptions options = new XmlOptions();
0121:                options.put(XmlOptions.SAVE_PRETTY_PRINT);
0122:                options.put(XmlOptions.SAVE_PRETTY_PRINT_INDENT, 2);
0123:                options.put(XmlOptions.SAVE_AGGRESSIVE_NAMESPACES);
0124:                String result = object.xmlText(options);
0125:
0126:                return result;
0127:            }
0128:
0129:            Random _picker = new Random(1);
0130:
0131:            private boolean ignoreOptional;
0132:
0133:            /**
0134:             * Cursor position
0135:             * Before:
0136:             * <theElement>^</theElement>
0137:             * After:
0138:             * <theElement><lots of stuff/>^</theElement>
0139:             */
0140:            public void createSampleForType(SchemaType stype, XmlCursor xmlc) {
0141:                if (_typeStack.contains(stype))
0142:                    return;
0143:
0144:                _typeStack.add(stype);
0145:
0146:                try {
0147:                    if (stype.isSimpleType() || stype.isURType()) {
0148:                        processSimpleType(stype, xmlc);
0149:                        return;
0150:                    }
0151:
0152:                    // complex Type
0153:                    // <theElement>^</theElement>
0154:                    processAttributes(stype, xmlc);
0155:
0156:                    // <theElement attri1="string">^</theElement>
0157:                    switch (stype.getContentType()) {
0158:                    case SchemaType.NOT_COMPLEX_TYPE:
0159:                    case SchemaType.EMPTY_CONTENT:
0160:                        // noop
0161:                        break;
0162:                    case SchemaType.SIMPLE_CONTENT: {
0163:                        processSimpleType(stype, xmlc);
0164:                    }
0165:                        break;
0166:                    case SchemaType.MIXED_CONTENT:
0167:                        xmlc.insertChars(pick(WORDS) + " "); // NOI18N
0168:                        if (stype.getContentModel() != null) {
0169:                            processParticle(stype.getContentModel(), xmlc, true);
0170:                        }
0171:                        xmlc.insertChars(pick(WORDS));
0172:                        break;
0173:                    case SchemaType.ELEMENT_CONTENT:
0174:                        if (stype.getContentModel() != null) {
0175:                            processParticle(stype.getContentModel(), xmlc,
0176:                                    false);
0177:                        }
0178:                        break;
0179:                    }
0180:                } finally {
0181:                    _typeStack.remove(_typeStack.size() - 1);
0182:                }
0183:            }
0184:
0185:            private void processSimpleType(SchemaType stype, XmlCursor xmlc) {
0186:                if (_soapEnc) {
0187:                    QName typeName = stype.getName();
0188:                    if (typeName != null) {
0189:                        xmlc.insertAttributeWithValue(XSI_TYPE, formatQName(
0190:                                xmlc, typeName));
0191:                    }
0192:                }
0193:
0194:                String sample = sampleDataForSimpleType(stype);
0195:                xmlc.insertChars(sample);
0196:            }
0197:
0198:            private String sampleDataForSimpleType(SchemaType sType) {
0199:                return "?" + _sampleDataForSimpleType(sType) + "?"; // NOI18N
0200:            }
0201:
0202:            private String _sampleDataForSimpleType(SchemaType sType) {
0203:                //        if( sType != null )
0204:                //            return "";
0205:
0206:                if (XmlObject.type.equals(sType))
0207:                    return "anyType"; // NOI18N
0208:
0209:                if (XmlAnySimpleType.type.equals(sType))
0210:                    return "anySimpleType"; // NOI18N
0211:
0212:                if (sType.getSimpleVariety() == SchemaType.LIST) {
0213:                    SchemaType itemType = sType.getListItemType();
0214:                    StringBuffer sb = new StringBuffer();
0215:                    int length = pickLength(sType);
0216:                    if (length > 0)
0217:                        sb.append(sampleDataForSimpleType(itemType));
0218:                    for (int i = 1; i < length; i += 1) {
0219:                        sb.append(' ');
0220:                        sb.append(sampleDataForSimpleType(itemType));
0221:                    }
0222:                    return sb.toString();
0223:                }
0224:
0225:                if (sType.getSimpleVariety() == SchemaType.UNION) {
0226:                    SchemaType[] possibleTypes = sType
0227:                            .getUnionConstituentTypes();
0228:                    if (possibleTypes.length == 0)
0229:                        return ""; // NOI18N
0230:                    return sampleDataForSimpleType(possibleTypes[pick(possibleTypes.length)]);
0231:                }
0232:
0233:                XmlAnySimpleType[] enumValues = sType.getEnumerationValues();
0234:                if (enumValues != null && enumValues.length > 0) {
0235:                    return enumValues[pick(enumValues.length)].getStringValue();
0236:                }
0237:
0238:                switch (sType.getPrimitiveType().getBuiltinTypeCode()) {
0239:                default:
0240:                case SchemaType.BTC_NOT_BUILTIN:
0241:                    return ""; // NOI18N
0242:
0243:                case SchemaType.BTC_ANY_TYPE:
0244:                case SchemaType.BTC_ANY_SIMPLE:
0245:                    return "anything"; // NOI18N
0246:
0247:                case SchemaType.BTC_BOOLEAN:
0248:                    return pick(2) == 0 ? "true" : "false"; // NOI18N
0249:
0250:                case SchemaType.BTC_BASE_64_BINARY: {
0251:                    String result = null;
0252:                    try {
0253:                        result = new String(Base64.encode(formatToLength(
0254:                                pick(WORDS), sType).getBytes("utf-8"))); // NOI18N
0255:                    } catch (java.io.UnsupportedEncodingException e) {
0256:                        /* Can't possibly happen */
0257:                    }
0258:                    return result;
0259:                }
0260:
0261:                case SchemaType.BTC_HEX_BINARY:
0262:                    return HexBin.encode(formatToLength(pick(WORDS), sType));
0263:
0264:                case SchemaType.BTC_ANY_URI:
0265:                    return formatToLength("http://www." + pick(DNS1) + "."
0266:                            + pick(DNS2) + "/" + pick(WORDS) + "/"
0267:                            + pick(WORDS), sType); // NOI18N
0268:
0269:                case SchemaType.BTC_QNAME:
0270:                    return formatToLength("qname", sType); // NOI18N
0271:
0272:                case SchemaType.BTC_NOTATION:
0273:                    return formatToLength("notation", sType); // NOI18N
0274:
0275:                case SchemaType.BTC_FLOAT:
0276:                    return "1.5E2"; // NOI18N
0277:                case SchemaType.BTC_DOUBLE:
0278:                    return "1.051732E7"; // NOI18N
0279:                case SchemaType.BTC_DECIMAL:
0280:                    switch (closestBuiltin(sType).getBuiltinTypeCode()) {
0281:                    case SchemaType.BTC_SHORT:
0282:                        return formatDecimal("1", sType); // NOI18N
0283:                    case SchemaType.BTC_UNSIGNED_SHORT:
0284:                        return formatDecimal("5", sType); // NOI18N
0285:                    case SchemaType.BTC_BYTE:
0286:                        return formatDecimal("2", sType); // NOI18N
0287:                    case SchemaType.BTC_UNSIGNED_BYTE:
0288:                        return formatDecimal("6", sType); // NOI18N
0289:                    case SchemaType.BTC_INT:
0290:                        return formatDecimal("3", sType); // NOI18N
0291:                    case SchemaType.BTC_UNSIGNED_INT:
0292:                        return formatDecimal("7", sType); // NOI18N
0293:                    case SchemaType.BTC_LONG:
0294:                        return formatDecimal("10", sType); // NOI18N
0295:                    case SchemaType.BTC_UNSIGNED_LONG:
0296:                        return formatDecimal("11", sType); // NOI18N
0297:                    case SchemaType.BTC_INTEGER:
0298:                        return formatDecimal("100", sType); // NOI18N
0299:                    case SchemaType.BTC_NON_POSITIVE_INTEGER:
0300:                        return formatDecimal("-200", sType); // NOI18N
0301:                    case SchemaType.BTC_NEGATIVE_INTEGER:
0302:                        return formatDecimal("-201", sType); // NOI18N
0303:                    case SchemaType.BTC_NON_NEGATIVE_INTEGER:
0304:                        return formatDecimal("200", sType); // NOI18N
0305:                    case SchemaType.BTC_POSITIVE_INTEGER:
0306:                        return formatDecimal("201", sType); // NOI18N
0307:                    default:
0308:                    case SchemaType.BTC_DECIMAL:
0309:                        return formatDecimal("1000.00", sType); // NOI18N
0310:                    }
0311:
0312:                case SchemaType.BTC_STRING: {
0313:                    String result;
0314:                    switch (closestBuiltin(sType).getBuiltinTypeCode()) {
0315:                    case SchemaType.BTC_STRING:
0316:                    case SchemaType.BTC_NORMALIZED_STRING:
0317:                        result = "string"; // NOI18N
0318:                        break;
0319:
0320:                    case SchemaType.BTC_TOKEN:
0321:                        result = "token"; // NOI18N
0322:                        break;
0323:
0324:                    default:
0325:                        result = "string"; // NOI18N
0326:                        break;
0327:                    }
0328:
0329:                    return formatToLength(result, sType);
0330:                }
0331:
0332:                case SchemaType.BTC_DURATION:
0333:                    return formatDuration(sType);
0334:
0335:                case SchemaType.BTC_DATE_TIME:
0336:                case SchemaType.BTC_TIME:
0337:                case SchemaType.BTC_DATE:
0338:                case SchemaType.BTC_G_YEAR_MONTH:
0339:                case SchemaType.BTC_G_YEAR:
0340:                case SchemaType.BTC_G_MONTH_DAY:
0341:                case SchemaType.BTC_G_DAY:
0342:                case SchemaType.BTC_G_MONTH:
0343:                    return formatDate(sType);
0344:                }
0345:            }
0346:
0347:            // a bit from the Aenid
0348:            public static final String[] WORDS = new String[] {
0349:                    "ipsa",
0350:                    "iovis",
0351:                    "rapidum",
0352:                    "iaculata",
0353:                    "e",
0354:                    "nubibus",
0355:                    "ignem", // NOI18N
0356:                    "disiecitque",
0357:                    "rates",
0358:                    "evertitque",
0359:                    "aequora",
0360:                    "ventis", // NOI18N
0361:                    "illum",
0362:                    "exspirantem",
0363:                    "transfixo",
0364:                    "pectore",
0365:                    "flammas", // NOI18N
0366:                    "turbine",
0367:                    "corripuit",
0368:                    "scopuloque",
0369:                    "infixit",
0370:                    "acuto", // NOI18N
0371:                    "ast",
0372:                    "ego",
0373:                    "quae",
0374:                    "divum",
0375:                    "incedo",
0376:                    "regina",
0377:                    "iovisque", // NOI18N
0378:                    "et",
0379:                    "soror",
0380:                    "et",
0381:                    "coniunx",
0382:                    "una",
0383:                    "cum",
0384:                    "gente",
0385:                    "tot",
0386:                    "annos", // NOI18N
0387:                    "bella",
0388:                    "gero",
0389:                    "et",
0390:                    "quisquam",
0391:                    "numen",
0392:                    "iunonis",
0393:                    "adorat", // NOI18N
0394:                    "praeterea",
0395:                    "aut",
0396:                    "supplex",
0397:                    "aris",
0398:                    "imponet",
0399:                    "honorem", // NOI18N
0400:                    "talia",
0401:                    "flammato",
0402:                    "secum",
0403:                    "dea",
0404:                    "corde",
0405:                    "volutans", // NOI18N
0406:                    "nimborum",
0407:                    "in",
0408:                    "patriam",
0409:                    "loca",
0410:                    "feta",
0411:                    "furentibus",
0412:                    "austris", // NOI18N
0413:                    "aeoliam",
0414:                    "venit",
0415:                    "hic",
0416:                    "vasto",
0417:                    "rex",
0418:                    "aeolus",
0419:                    "antro", // NOI18N
0420:                    "luctantis",
0421:                    "ventos",
0422:                    "tempestatesque",
0423:                    "sonoras", // NOI18N
0424:                    "imperio",
0425:                    "premit",
0426:                    "ac",
0427:                    "vinclis",
0428:                    "et",
0429:                    "carcere",
0430:                    "frenat", // NOI18N
0431:                    "illi",
0432:                    "indignantes",
0433:                    "magno",
0434:                    "cum",
0435:                    "murmure",
0436:                    "montis", // NOI18N
0437:                    "circum",
0438:                    "claustra",
0439:                    "fremunt",
0440:                    "celsa",
0441:                    "sedet",
0442:                    "aeolus",
0443:                    "arce", // NOI18N
0444:                    "sceptra",
0445:                    "tenens",
0446:                    "mollitque",
0447:                    "animos",
0448:                    "et",
0449:                    "temperat",
0450:                    "iras", // NOI18N
0451:                    "ni", "faciat",
0452:                    "maria",
0453:                    "ac",
0454:                    "terras",
0455:                    "caelumque",
0456:                    "profundum", // NOI18N
0457:                    "quippe", "ferant", "rapidi",
0458:                    "secum",
0459:                    "verrantque",
0460:                    "per",
0461:                    "auras", // NOI18N
0462:                    "sed", "pater", "omnipotens",
0463:                    "speluncis",
0464:                    "abdidit",
0465:                    "atris", // NOI18N
0466:                    "hoc", "metuens", "molemque", "et", "montis",
0467:                    "insuper",
0468:                    "altos", // NOI18N
0469:                    "imposuit", "regemque", "dedit", "qui", "foedere",
0470:                    "certo", // NOI18N
0471:                    "et", "premere", "et", "laxas", "sciret", "dare", "iussus",
0472:                    "habenas", // NOI18N
0473:            };
0474:
0475:            private static final String[] DNS1 = new String[] { "corp", "your",
0476:                    "my", "sample", "company", "test", "any" }; // NOI18N
0477:            private static final String[] DNS2 = new String[] { "com", "org",
0478:                    "com", "gov", "org", "com", "org", "com", "edu" }; // NOI18N
0479:
0480:            private int pick(int n) {
0481:                return _picker.nextInt(n);
0482:            }
0483:
0484:            private String pick(String[] a) {
0485:                return a[pick(a.length)];
0486:            }
0487:
0488:            private String pick(String[] a, int count) {
0489:                if (count <= 0)
0490:                    return ""; // NOI18N
0491:
0492:                int i = pick(a.length);
0493:                StringBuffer sb = new StringBuffer(a[i]);
0494:                while (count-- > 0) {
0495:                    i += 1;
0496:                    if (i >= a.length)
0497:                        i = 0;
0498:                    sb.append(' ');
0499:                    sb.append(a[i]);
0500:                }
0501:                return sb.toString();
0502:            }
0503:
0504:            private String pickDigits(int digits) {
0505:                StringBuffer sb = new StringBuffer();
0506:                while (digits-- > 0)
0507:                    sb.append(Integer.toString(pick(10)));
0508:                return sb.toString();
0509:            }
0510:
0511:            private int pickLength(SchemaType sType) {
0512:                XmlInteger length = (XmlInteger) sType
0513:                        .getFacet(SchemaType.FACET_LENGTH);
0514:                if (length != null)
0515:                    return length.getBigIntegerValue().intValue();
0516:                XmlInteger min = (XmlInteger) sType
0517:                        .getFacet(SchemaType.FACET_MIN_LENGTH);
0518:                XmlInteger max = (XmlInteger) sType
0519:                        .getFacet(SchemaType.FACET_MAX_LENGTH);
0520:                int minInt, maxInt;
0521:                if (min == null)
0522:                    minInt = 0;
0523:                else
0524:                    minInt = min.getBigIntegerValue().intValue();
0525:                if (max == null)
0526:                    maxInt = Integer.MAX_VALUE;
0527:                else
0528:                    maxInt = max.getBigIntegerValue().intValue();
0529:                // We try to keep the length of the array within reasonable limits,
0530:                // at least 1 item and at most 3 if possible
0531:                if (minInt == 0 && maxInt >= 1)
0532:                    minInt = 1;
0533:                if (maxInt > minInt + 2)
0534:                    maxInt = minInt + 2;
0535:                if (maxInt < minInt)
0536:                    maxInt = minInt;
0537:                return minInt + pick(maxInt - minInt);
0538:            }
0539:
0540:            /**
0541:             * Formats a given string to the required length, using the following operations:
0542:             * - append the source string to itself as necessary to pass the minLength;
0543:             * - truncate the result of previous step, if necessary, to keep it within minLength.
0544:             */
0545:            private String formatToLength(String s, SchemaType sType) {
0546:                String result = s;
0547:                try {
0548:                    SimpleValue min = (SimpleValue) sType
0549:                            .getFacet(SchemaType.FACET_LENGTH);
0550:                    if (min == null)
0551:                        min = (SimpleValue) sType
0552:                                .getFacet(SchemaType.FACET_MIN_LENGTH);
0553:                    if (min != null) {
0554:                        int len = min.getIntValue();
0555:                        while (result.length() < len)
0556:                            result = result + result;
0557:                    }
0558:                    SimpleValue max = (SimpleValue) sType
0559:                            .getFacet(SchemaType.FACET_LENGTH);
0560:                    if (max == null)
0561:                        max = (SimpleValue) sType
0562:                                .getFacet(SchemaType.FACET_MAX_LENGTH);
0563:                    if (max != null) {
0564:                        int len = max.getIntValue();
0565:                        if (result.length() > len)
0566:                            result = result.substring(0, len);
0567:                    }
0568:                } catch (Exception e) // intValue can be out of range
0569:                {
0570:                }
0571:                return result;
0572:            }
0573:
0574:            private String formatDecimal(String start, SchemaType sType) {
0575:                BigDecimal result = new BigDecimal(start);
0576:                XmlDecimal xmlD;
0577:                xmlD = (XmlDecimal) sType
0578:                        .getFacet(SchemaType.FACET_MIN_INCLUSIVE);
0579:                BigDecimal min = xmlD != null ? xmlD.getBigDecimalValue()
0580:                        : null;
0581:                xmlD = (XmlDecimal) sType
0582:                        .getFacet(SchemaType.FACET_MAX_INCLUSIVE);
0583:                BigDecimal max = xmlD != null ? xmlD.getBigDecimalValue()
0584:                        : null;
0585:                boolean minInclusive = true, maxInclusive = true;
0586:                xmlD = (XmlDecimal) sType
0587:                        .getFacet(SchemaType.FACET_MIN_EXCLUSIVE);
0588:                if (xmlD != null) {
0589:                    BigDecimal minExcl = xmlD.getBigDecimalValue();
0590:                    if (min == null || min.compareTo(minExcl) < 0) {
0591:                        min = minExcl;
0592:                        minInclusive = false;
0593:                    }
0594:                }
0595:                xmlD = (XmlDecimal) sType
0596:                        .getFacet(SchemaType.FACET_MAX_EXCLUSIVE);
0597:                if (xmlD != null) {
0598:                    BigDecimal maxExcl = xmlD.getBigDecimalValue();
0599:                    if (max == null || max.compareTo(maxExcl) > 0) {
0600:                        max = maxExcl;
0601:                        maxInclusive = false;
0602:                    }
0603:                }
0604:                xmlD = (XmlDecimal) sType
0605:                        .getFacet(SchemaType.FACET_TOTAL_DIGITS);
0606:                int totalDigits = -1;
0607:                if (xmlD != null) {
0608:                    totalDigits = xmlD.getBigDecimalValue().intValue();
0609:
0610:                    StringBuffer sb = new StringBuffer(totalDigits);
0611:                    for (int i = 0; i < totalDigits; i++)
0612:                        sb.append('9');
0613:                    BigDecimal digitsLimit = new BigDecimal(sb.toString());
0614:                    if (max != null && max.compareTo(digitsLimit) > 0) {
0615:                        max = digitsLimit;
0616:                        maxInclusive = true;
0617:                    }
0618:                    digitsLimit = digitsLimit.negate();
0619:                    if (min != null && min.compareTo(digitsLimit) < 0) {
0620:                        min = digitsLimit;
0621:                        minInclusive = true;
0622:                    }
0623:                }
0624:
0625:                int sigMin = min == null ? 1 : result.compareTo(min);
0626:                int sigMax = max == null ? -1 : result.compareTo(max);
0627:                boolean minOk = sigMin > 0 || sigMin == 0 && minInclusive;
0628:                boolean maxOk = sigMax < 0 || sigMax == 0 && maxInclusive;
0629:
0630:                // Compute the minimum increment
0631:                xmlD = (XmlDecimal) sType
0632:                        .getFacet(SchemaType.FACET_FRACTION_DIGITS);
0633:                int fractionDigits = -1;
0634:                BigDecimal increment;
0635:                if (xmlD == null)
0636:                    increment = new BigDecimal(1);
0637:                else {
0638:                    fractionDigits = xmlD.getBigDecimalValue().intValue();
0639:                    if (fractionDigits > 0) {
0640:                        StringBuffer sb = new StringBuffer("0."); // NOI18N
0641:                        for (int i = 1; i < fractionDigits; i++)
0642:                            sb.append('0');
0643:                        sb.append('1');
0644:                        increment = new BigDecimal(sb.toString());
0645:                    } else
0646:                        increment = new BigDecimal(1);
0647:                }
0648:
0649:                if (minOk && maxOk) {
0650:                    // OK
0651:                } else if (minOk && !maxOk) {
0652:                    // TOO BIG
0653:                    if (maxInclusive)
0654:                        result = max;
0655:                    else
0656:                        result = max.subtract(increment);
0657:                } else if (!minOk && maxOk) {
0658:                    // TOO SMALL
0659:                    if (minInclusive)
0660:                        result = min;
0661:                    else
0662:                        result = min.add(increment);
0663:                } else {
0664:                    // MIN > MAX!!
0665:                }
0666:
0667:                // We have the number
0668:                // Adjust the scale according to the totalDigits and fractionDigits
0669:                int digits = 0;
0670:                BigDecimal ONE = new BigDecimal(BigInteger.ONE);
0671:                for (BigDecimal n = result; n.abs().compareTo(ONE) >= 0; digits++)
0672:                    n = n.movePointLeft(1);
0673:
0674:                if (fractionDigits > 0)
0675:                    if (totalDigits >= 0)
0676:                        result.setScale(Math.max(fractionDigits, totalDigits
0677:                                - digits));
0678:                    else
0679:                        result.setScale(fractionDigits);
0680:                else if (fractionDigits == 0)
0681:                    result.setScale(0);
0682:
0683:                return result.toString();
0684:            }
0685:
0686:            private String formatDuration(SchemaType sType) {
0687:                XmlDuration d = (XmlDuration) sType
0688:                        .getFacet(SchemaType.FACET_MIN_INCLUSIVE);
0689:                GDuration minInclusive = null;
0690:                if (d != null)
0691:                    minInclusive = d.getGDurationValue();
0692:
0693:                d = (XmlDuration) sType
0694:                        .getFacet(SchemaType.FACET_MAX_INCLUSIVE);
0695:                GDuration maxInclusive = null;
0696:                if (d != null)
0697:                    maxInclusive = d.getGDurationValue();
0698:
0699:                d = (XmlDuration) sType
0700:                        .getFacet(SchemaType.FACET_MIN_EXCLUSIVE);
0701:                GDuration minExclusive = null;
0702:                if (d != null)
0703:                    minExclusive = d.getGDurationValue();
0704:
0705:                d = (XmlDuration) sType
0706:                        .getFacet(SchemaType.FACET_MAX_EXCLUSIVE);
0707:                GDuration maxExclusive = null;
0708:                if (d != null)
0709:                    maxExclusive = d.getGDurationValue();
0710:
0711:                GDurationBuilder gdurb = new GDurationBuilder();
0712:                BigInteger min, max;
0713:
0714:                gdurb.setSecond(pick(800000));
0715:                gdurb.setMonth(pick(20));
0716:
0717:                // Years
0718:                // Months
0719:                // Days
0720:                // Hours
0721:                // Minutes
0722:                // Seconds
0723:                // Fractions
0724:                if (minInclusive != null) {
0725:                    if (gdurb.getYear() < minInclusive.getYear())
0726:                        gdurb.setYear(minInclusive.getYear());
0727:                    if (gdurb.getMonth() < minInclusive.getMonth())
0728:                        gdurb.setMonth(minInclusive.getMonth());
0729:                    if (gdurb.getDay() < minInclusive.getDay())
0730:                        gdurb.setDay(minInclusive.getDay());
0731:                    if (gdurb.getHour() < minInclusive.getHour())
0732:                        gdurb.setHour(minInclusive.getHour());
0733:                    if (gdurb.getMinute() < minInclusive.getMinute())
0734:                        gdurb.setMinute(minInclusive.getMinute());
0735:                    if (gdurb.getSecond() < minInclusive.getSecond())
0736:                        gdurb.setSecond(minInclusive.getSecond());
0737:                    if (gdurb.getFraction().compareTo(
0738:                            minInclusive.getFraction()) < 0)
0739:                        gdurb.setFraction(minInclusive.getFraction());
0740:                }
0741:
0742:                if (maxInclusive != null) {
0743:                    if (gdurb.getYear() > maxInclusive.getYear())
0744:                        gdurb.setYear(maxInclusive.getYear());
0745:                    if (gdurb.getMonth() > maxInclusive.getMonth())
0746:                        gdurb.setMonth(maxInclusive.getMonth());
0747:                    if (gdurb.getDay() > maxInclusive.getDay())
0748:                        gdurb.setDay(maxInclusive.getDay());
0749:                    if (gdurb.getHour() > maxInclusive.getHour())
0750:                        gdurb.setHour(maxInclusive.getHour());
0751:                    if (gdurb.getMinute() > maxInclusive.getMinute())
0752:                        gdurb.setMinute(maxInclusive.getMinute());
0753:                    if (gdurb.getSecond() > maxInclusive.getSecond())
0754:                        gdurb.setSecond(maxInclusive.getSecond());
0755:                    if (gdurb.getFraction().compareTo(
0756:                            maxInclusive.getFraction()) > 0)
0757:                        gdurb.setFraction(maxInclusive.getFraction());
0758:                }
0759:
0760:                if (minExclusive != null) {
0761:                    if (gdurb.getYear() <= minExclusive.getYear())
0762:                        gdurb.setYear(minExclusive.getYear() + 1);
0763:                    if (gdurb.getMonth() <= minExclusive.getMonth())
0764:                        gdurb.setMonth(minExclusive.getMonth() + 1);
0765:                    if (gdurb.getDay() <= minExclusive.getDay())
0766:                        gdurb.setDay(minExclusive.getDay() + 1);
0767:                    if (gdurb.getHour() <= minExclusive.getHour())
0768:                        gdurb.setHour(minExclusive.getHour() + 1);
0769:                    if (gdurb.getMinute() <= minExclusive.getMinute())
0770:                        gdurb.setMinute(minExclusive.getMinute() + 1);
0771:                    if (gdurb.getSecond() <= minExclusive.getSecond())
0772:                        gdurb.setSecond(minExclusive.getSecond() + 1);
0773:                    if (gdurb.getFraction().compareTo(
0774:                            minExclusive.getFraction()) <= 0)
0775:                        gdurb.setFraction(minExclusive.getFraction().add(
0776:                                new BigDecimal(0.001)));
0777:                }
0778:
0779:                if (maxExclusive != null) {
0780:                    if (gdurb.getYear() > maxExclusive.getYear())
0781:                        gdurb.setYear(maxExclusive.getYear());
0782:                    if (gdurb.getMonth() > maxExclusive.getMonth())
0783:                        gdurb.setMonth(maxExclusive.getMonth());
0784:                    if (gdurb.getDay() > maxExclusive.getDay())
0785:                        gdurb.setDay(maxExclusive.getDay());
0786:                    if (gdurb.getHour() > maxExclusive.getHour())
0787:                        gdurb.setHour(maxExclusive.getHour());
0788:                    if (gdurb.getMinute() > maxExclusive.getMinute())
0789:                        gdurb.setMinute(maxExclusive.getMinute());
0790:                    if (gdurb.getSecond() > maxExclusive.getSecond())
0791:                        gdurb.setSecond(maxExclusive.getSecond());
0792:                    if (gdurb.getFraction().compareTo(
0793:                            maxExclusive.getFraction()) > 0)
0794:                        gdurb.setFraction(maxExclusive.getFraction());
0795:                }
0796:
0797:                gdurb.normalize();
0798:                return gdurb.toString();
0799:            }
0800:
0801:            private String formatDate(SchemaType sType) {
0802:                GDateBuilder gdateb = new GDateBuilder(new Date(1000L
0803:                        * pick(365 * 24 * 60 * 60) + (30L + pick(20)) * 365
0804:                        * 24 * 60 * 60 * 1000));
0805:                GDate min = null, max = null;
0806:                GDate temp;
0807:
0808:                // Find the min and the max according to the type
0809:                switch (sType.getPrimitiveType().getBuiltinTypeCode()) {
0810:                case SchemaType.BTC_DATE_TIME: {
0811:                    XmlDateTime x = (XmlDateTime) sType
0812:                            .getFacet(SchemaType.FACET_MIN_INCLUSIVE);
0813:                    if (x != null)
0814:                        min = x.getGDateValue();
0815:                    x = (XmlDateTime) sType
0816:                            .getFacet(SchemaType.FACET_MIN_EXCLUSIVE);
0817:                    if (x != null)
0818:                        if (min == null
0819:                                || min.compareToGDate(x.getGDateValue()) <= 0)
0820:                            min = x.getGDateValue();
0821:
0822:                    x = (XmlDateTime) sType
0823:                            .getFacet(SchemaType.FACET_MAX_INCLUSIVE);
0824:                    if (x != null)
0825:                        max = x.getGDateValue();
0826:                    x = (XmlDateTime) sType
0827:                            .getFacet(SchemaType.FACET_MAX_EXCLUSIVE);
0828:                    if (x != null)
0829:                        if (max == null
0830:                                || max.compareToGDate(x.getGDateValue()) >= 0)
0831:                            max = x.getGDateValue();
0832:                    break;
0833:                }
0834:                case SchemaType.BTC_TIME: {
0835:                    XmlTime x = (XmlTime) sType
0836:                            .getFacet(SchemaType.FACET_MIN_INCLUSIVE);
0837:                    if (x != null)
0838:                        min = x.getGDateValue();
0839:                    x = (XmlTime) sType
0840:                            .getFacet(SchemaType.FACET_MIN_EXCLUSIVE);
0841:                    if (x != null)
0842:                        if (min == null
0843:                                || min.compareToGDate(x.getGDateValue()) <= 0)
0844:                            min = x.getGDateValue();
0845:
0846:                    x = (XmlTime) sType
0847:                            .getFacet(SchemaType.FACET_MAX_INCLUSIVE);
0848:                    if (x != null)
0849:                        max = x.getGDateValue();
0850:                    x = (XmlTime) sType
0851:                            .getFacet(SchemaType.FACET_MAX_EXCLUSIVE);
0852:                    if (x != null)
0853:                        if (max == null
0854:                                || max.compareToGDate(x.getGDateValue()) >= 0)
0855:                            max = x.getGDateValue();
0856:                    break;
0857:                }
0858:                case SchemaType.BTC_DATE: {
0859:                    XmlDate x = (XmlDate) sType
0860:                            .getFacet(SchemaType.FACET_MIN_INCLUSIVE);
0861:                    if (x != null)
0862:                        min = x.getGDateValue();
0863:                    x = (XmlDate) sType
0864:                            .getFacet(SchemaType.FACET_MIN_EXCLUSIVE);
0865:                    if (x != null)
0866:                        if (min == null
0867:                                || min.compareToGDate(x.getGDateValue()) <= 0)
0868:                            min = x.getGDateValue();
0869:
0870:                    x = (XmlDate) sType
0871:                            .getFacet(SchemaType.FACET_MAX_INCLUSIVE);
0872:                    if (x != null)
0873:                        max = x.getGDateValue();
0874:                    x = (XmlDate) sType
0875:                            .getFacet(SchemaType.FACET_MAX_EXCLUSIVE);
0876:                    if (x != null)
0877:                        if (max == null
0878:                                || max.compareToGDate(x.getGDateValue()) >= 0)
0879:                            max = x.getGDateValue();
0880:                    break;
0881:                }
0882:                case SchemaType.BTC_G_YEAR_MONTH: {
0883:                    XmlGYearMonth x = (XmlGYearMonth) sType
0884:                            .getFacet(SchemaType.FACET_MIN_INCLUSIVE);
0885:                    if (x != null)
0886:                        min = x.getGDateValue();
0887:                    x = (XmlGYearMonth) sType
0888:                            .getFacet(SchemaType.FACET_MIN_EXCLUSIVE);
0889:                    if (x != null)
0890:                        if (min == null
0891:                                || min.compareToGDate(x.getGDateValue()) <= 0)
0892:                            min = x.getGDateValue();
0893:
0894:                    x = (XmlGYearMonth) sType
0895:                            .getFacet(SchemaType.FACET_MAX_INCLUSIVE);
0896:                    if (x != null)
0897:                        max = x.getGDateValue();
0898:                    x = (XmlGYearMonth) sType
0899:                            .getFacet(SchemaType.FACET_MAX_EXCLUSIVE);
0900:                    if (x != null)
0901:                        if (max == null
0902:                                || max.compareToGDate(x.getGDateValue()) >= 0)
0903:                            max = x.getGDateValue();
0904:                    break;
0905:                }
0906:                case SchemaType.BTC_G_YEAR: {
0907:                    XmlGYear x = (XmlGYear) sType
0908:                            .getFacet(SchemaType.FACET_MIN_INCLUSIVE);
0909:                    if (x != null)
0910:                        min = x.getGDateValue();
0911:                    x = (XmlGYear) sType
0912:                            .getFacet(SchemaType.FACET_MIN_EXCLUSIVE);
0913:                    if (x != null)
0914:                        if (min == null
0915:                                || min.compareToGDate(x.getGDateValue()) <= 0)
0916:                            min = x.getGDateValue();
0917:
0918:                    x = (XmlGYear) sType
0919:                            .getFacet(SchemaType.FACET_MAX_INCLUSIVE);
0920:                    if (x != null)
0921:                        max = x.getGDateValue();
0922:                    x = (XmlGYear) sType
0923:                            .getFacet(SchemaType.FACET_MAX_EXCLUSIVE);
0924:                    if (x != null)
0925:                        if (max == null
0926:                                || max.compareToGDate(x.getGDateValue()) >= 0)
0927:                            max = x.getGDateValue();
0928:                    break;
0929:                }
0930:                case SchemaType.BTC_G_MONTH_DAY: {
0931:                    XmlGMonthDay x = (XmlGMonthDay) sType
0932:                            .getFacet(SchemaType.FACET_MIN_INCLUSIVE);
0933:                    if (x != null)
0934:                        min = x.getGDateValue();
0935:                    x = (XmlGMonthDay) sType
0936:                            .getFacet(SchemaType.FACET_MIN_EXCLUSIVE);
0937:                    if (x != null)
0938:                        if (min == null
0939:                                || min.compareToGDate(x.getGDateValue()) <= 0)
0940:                            min = x.getGDateValue();
0941:
0942:                    x = (XmlGMonthDay) sType
0943:                            .getFacet(SchemaType.FACET_MAX_INCLUSIVE);
0944:                    if (x != null)
0945:                        max = x.getGDateValue();
0946:                    x = (XmlGMonthDay) sType
0947:                            .getFacet(SchemaType.FACET_MAX_EXCLUSIVE);
0948:                    if (x != null)
0949:                        if (max == null
0950:                                || max.compareToGDate(x.getGDateValue()) >= 0)
0951:                            max = x.getGDateValue();
0952:                    break;
0953:                }
0954:                case SchemaType.BTC_G_DAY: {
0955:                    XmlGDay x = (XmlGDay) sType
0956:                            .getFacet(SchemaType.FACET_MIN_INCLUSIVE);
0957:                    if (x != null)
0958:                        min = x.getGDateValue();
0959:                    x = (XmlGDay) sType
0960:                            .getFacet(SchemaType.FACET_MIN_EXCLUSIVE);
0961:                    if (x != null)
0962:                        if (min == null
0963:                                || min.compareToGDate(x.getGDateValue()) <= 0)
0964:                            min = x.getGDateValue();
0965:
0966:                    x = (XmlGDay) sType
0967:                            .getFacet(SchemaType.FACET_MAX_INCLUSIVE);
0968:                    if (x != null)
0969:                        max = x.getGDateValue();
0970:                    x = (XmlGDay) sType
0971:                            .getFacet(SchemaType.FACET_MAX_EXCLUSIVE);
0972:                    if (x != null)
0973:                        if (max == null
0974:                                || max.compareToGDate(x.getGDateValue()) >= 0)
0975:                            max = x.getGDateValue();
0976:                    break;
0977:                }
0978:                case SchemaType.BTC_G_MONTH: {
0979:                    XmlGMonth x = (XmlGMonth) sType
0980:                            .getFacet(SchemaType.FACET_MIN_INCLUSIVE);
0981:                    if (x != null)
0982:                        min = x.getGDateValue();
0983:                    x = (XmlGMonth) sType
0984:                            .getFacet(SchemaType.FACET_MIN_EXCLUSIVE);
0985:                    if (x != null)
0986:                        if (min == null
0987:                                || min.compareToGDate(x.getGDateValue()) <= 0)
0988:                            min = x.getGDateValue();
0989:
0990:                    x = (XmlGMonth) sType
0991:                            .getFacet(SchemaType.FACET_MAX_INCLUSIVE);
0992:                    if (x != null)
0993:                        max = x.getGDateValue();
0994:                    x = (XmlGMonth) sType
0995:                            .getFacet(SchemaType.FACET_MAX_EXCLUSIVE);
0996:                    if (x != null)
0997:                        if (max == null
0998:                                || max.compareToGDate(x.getGDateValue()) >= 0)
0999:                            max = x.getGDateValue();
1000:                    break;
1001:                }
1002:                }
1003:
1004:                if (min != null && max == null) {
1005:                    if (min.compareToGDate(gdateb) >= 0) {
1006:                        // Reset the date to min + (1-8) hours
1007:                        Calendar c = gdateb.getCalendar();
1008:                        c.add(Calendar.HOUR_OF_DAY, pick(8));
1009:                        gdateb = new GDateBuilder(c);
1010:                    }
1011:                } else if (min == null && max != null) {
1012:                    if (max.compareToGDate(gdateb) <= 0) {
1013:                        // Reset the date to max - (1-8) hours
1014:                        Calendar c = gdateb.getCalendar();
1015:                        c.add(Calendar.HOUR_OF_DAY, 0 - pick(8));
1016:                        gdateb = new GDateBuilder(c);
1017:                    }
1018:                } else if (min != null && max != null) {
1019:                    if (min.compareToGDate(gdateb) >= 0
1020:                            || max.compareToGDate(gdateb) <= 0) {
1021:                        // Find a date between the two
1022:                        Calendar c = min.getCalendar();
1023:                        Calendar cmax = max.getCalendar();
1024:                        c.add(Calendar.HOUR_OF_DAY, 1);
1025:                        if (c.after(cmax)) {
1026:                            c.add(Calendar.HOUR_OF_DAY, -1);
1027:                            c.add(Calendar.MINUTE, 1);
1028:                            if (c.after(cmax)) {
1029:                                c.add(Calendar.MINUTE, -1);
1030:                                c.add(Calendar.SECOND, 1);
1031:                                if (c.after(cmax)) {
1032:                                    c.add(Calendar.SECOND, -1);
1033:                                    c.add(Calendar.MILLISECOND, 1);
1034:                                    if (c.after(cmax))
1035:                                        c.add(Calendar.MILLISECOND, -1);
1036:                                }
1037:                            }
1038:                        }
1039:                        gdateb = new GDateBuilder(c);
1040:                    }
1041:                }
1042:
1043:                gdateb.setBuiltinTypeCode(sType.getPrimitiveType()
1044:                        .getBuiltinTypeCode());
1045:                if (pick(2) == 0)
1046:                    gdateb.clearTimeZone();
1047:                return gdateb.toString();
1048:            }
1049:
1050:            private SchemaType closestBuiltin(SchemaType sType) {
1051:                while (!sType.isBuiltinType())
1052:                    sType = sType.getBaseType();
1053:                return sType;
1054:            }
1055:
1056:            /**
1057:             * Cracks a combined QName of the form URL:localname
1058:             */
1059:            public static QName crackQName(String qName) {
1060:                String ns;
1061:                String name;
1062:
1063:                int index = qName.lastIndexOf(':');
1064:                if (index >= 0) {
1065:                    ns = qName.substring(0, index);
1066:                    name = qName.substring(index + 1);
1067:                } else {
1068:                    ns = ""; // NOI18N
1069:                    name = qName;
1070:                }
1071:
1072:                return new QName(ns, name);
1073:            }
1074:
1075:            /**
1076:             * Cursor position:
1077:             * Before this call:
1078:             * <outer><foo/>^</outer>  (cursor at the ^)
1079:             * After this call:
1080:             * <<outer><foo/><bar/>som text<etc/>^</outer>
1081:             */
1082:            private void processParticle(SchemaParticle sp, XmlCursor xmlc,
1083:                    boolean mixed) {
1084:                int loop = determineMinMaxForSample(sp, xmlc);
1085:
1086:                while (loop-- > 0) {
1087:                    switch (sp.getParticleType()) {
1088:                    case (SchemaParticle.ELEMENT):
1089:                        processElement(sp, xmlc, mixed);
1090:                        break;
1091:                    case (SchemaParticle.SEQUENCE):
1092:                        processSequence(sp, xmlc, mixed);
1093:                        break;
1094:                    case (SchemaParticle.CHOICE):
1095:                        processChoice(sp, xmlc, mixed);
1096:                        break;
1097:                    case (SchemaParticle.ALL):
1098:                        processAll(sp, xmlc, mixed);
1099:                        break;
1100:                    case (SchemaParticle.WILDCARD):
1101:                        processWildCard(sp, xmlc, mixed);
1102:                        break;
1103:                    default:
1104:                        // throw new Exception("No Match on Schema Particle Type: " + String.valueOf(sp.getParticleType()));
1105:                    }
1106:                }
1107:            }
1108:
1109:            private int determineMinMaxForSample(SchemaParticle sp,
1110:                    XmlCursor xmlc) {
1111:                int minOccurs = sp.getIntMinOccurs();
1112:                int maxOccurs = sp.getIntMaxOccurs();
1113:
1114:                if (minOccurs == maxOccurs)
1115:                    return minOccurs;
1116:
1117:                if (minOccurs == 0 && ignoreOptional)
1118:                    return 0;
1119:
1120:                int result = minOccurs;
1121:                if (result == 0)
1122:                    result = 1;
1123:
1124:                if (sp.getParticleType() != SchemaParticle.ELEMENT)
1125:                    return result;
1126:
1127:                // it probably only makes sense to put comments in front of individual elements that repeat
1128:
1129:                if (sp.getMaxOccurs() == null) {
1130:                    // xmlc.insertComment("The next " + getItemNameOrType(sp, xmlc) + " may be repeated " + minOccurs + " or more times");
1131:                    if (minOccurs == 0)
1132:                        xmlc.insertComment(NbBundle.getMessage(
1133:                                SampleXmlUtil.class,
1134:                                "LBL_Zero_or_more_repetitions")); // NOI18N
1135:                    else
1136:                        xmlc.insertComment(NbBundle.getMessage(
1137:                                SampleXmlUtil.class,
1138:                                "LBL_N_or_more_repetitions", "" + minOccurs)); // NOI18N
1139:                } else if (sp.getIntMaxOccurs() > 1) {
1140:                    xmlc.insertComment(NbBundle.getMessage(SampleXmlUtil.class,
1141:                            "LBL_N_to_M_repetitions", "" + minOccurs, String
1142:                                    .valueOf(sp.getMaxOccurs()))); // NOI18N
1143:                } else {
1144:                    xmlc.insertComment(NbBundle.getMessage(SampleXmlUtil.class,
1145:                            "LBL_Optional")); // NOI18N
1146:                }
1147:                return result;
1148:            }
1149:
1150:            /*
1151:             Return a name for the element or the particle type to use in the comment for minoccurs, max occurs
1152:             */
1153:            private String getItemNameOrType(SchemaParticle sp, XmlCursor xmlc) {
1154:                String elementOrTypeName = null;
1155:                if (sp.getParticleType() == SchemaParticle.ELEMENT) {
1156:                    elementOrTypeName = "Element ("
1157:                            + sp.getName().getLocalPart() + ")"; // NOI18N
1158:                } else {
1159:                    elementOrTypeName = printParticleType(sp.getParticleType());
1160:                }
1161:                return elementOrTypeName;
1162:            }
1163:
1164:            private void processElement(SchemaParticle sp, XmlCursor xmlc,
1165:                    boolean mixed) {
1166:                // cast as schema local element
1167:                SchemaLocalElement element = (SchemaLocalElement) sp;
1168:                /// ^  -> <elemenname></elem>^
1169:                if (_soapEnc)
1170:                    xmlc.insertElement(element.getName().getLocalPart()); // soap encoded? drop namespaces.
1171:                else
1172:                    xmlc.insertElement(element.getName().getLocalPart(),
1173:                            element.getName().getNamespaceURI());
1174:                /// -> <elem>^</elem>
1175:                // processAttributes( sp.getType(), xmlc );
1176:
1177:                xmlc.toPrevToken();
1178:                // -> <elem>stuff^</elem>
1179:
1180:                createSampleForType(element.getType(), xmlc);
1181:                // -> <elem>stuff</elem>^
1182:                xmlc.toNextToken();
1183:
1184:            }
1185:
1186:            private void moveToken(int numToMove, XmlCursor xmlc) {
1187:                for (int i = 0; i < Math.abs(numToMove); i++) {
1188:                    if (numToMove < 0) {
1189:                        xmlc.toPrevToken();
1190:                    } else {
1191:                        xmlc.toNextToken();
1192:                    }
1193:                }
1194:            }
1195:
1196:            private static final String formatQName(XmlCursor xmlc, QName qName) {
1197:                XmlCursor parent = xmlc.newCursor();
1198:                parent.toParent();
1199:                String prefix = parent.prefixForNamespace(qName
1200:                        .getNamespaceURI());
1201:                parent.dispose();
1202:                String name;
1203:                if (prefix == null || prefix.length() == 0)
1204:                    name = qName.getLocalPart();
1205:                else
1206:                    name = prefix + ":" + qName.getLocalPart(); // NOI18N
1207:                return name;
1208:            }
1209:
1210:            private static final QName HREF = new QName("href"); // NOI18N
1211:            private static final QName ID = new QName("id"); // NOI18N
1212:            public static final QName XSI_TYPE = new QName(
1213:                    "http://www.w3.org/2001/XMLSchema-instance", "type"); // NOI18N
1214:            private static final QName ENC_ARRAYTYPE = new QName(
1215:                    "http://schemas.xmlsoap.org/soap/encoding/", "arrayType"); // NOI18N
1216:            private static final QName ENC_OFFSET = new QName(
1217:                    "http://schemas.xmlsoap.org/soap/encoding/", "offset"); // NOI18N
1218:
1219:            private static final Set SKIPPED_SOAP_ATTRS = new HashSet(Arrays
1220:                    .asList(new QName[] { HREF, ID, ENC_OFFSET }));
1221:
1222:            private void processAttributes(SchemaType stype, XmlCursor xmlc) {
1223:                if (_soapEnc) {
1224:                    QName typeName = stype.getName();
1225:                    if (typeName != null) {
1226:                        xmlc.insertAttributeWithValue(XSI_TYPE, formatQName(
1227:                                xmlc, typeName));
1228:                    }
1229:                }
1230:
1231:                SchemaProperty[] attrProps = stype.getAttributeProperties();
1232:                for (int i = 0; i < attrProps.length; i++) {
1233:                    SchemaProperty attr = attrProps[i];
1234:                    if (_soapEnc) {
1235:                        if (SKIPPED_SOAP_ATTRS.contains(attr.getName()))
1236:                            continue;
1237:                        if (ENC_ARRAYTYPE.equals(attr.getName())) {
1238:                            SOAPArrayType arrayType = ((SchemaWSDLArrayType) stype
1239:                                    .getAttributeModel().getAttribute(
1240:                                            attr.getName())).getWSDLArrayType();
1241:                            if (arrayType != null)
1242:                                xmlc
1243:                                        .insertAttributeWithValue(
1244:                                                attr.getName(),
1245:                                                formatQName(xmlc, arrayType
1246:                                                        .getQName())
1247:                                                        + arrayType
1248:                                                                .soap11DimensionString());
1249:                            continue;
1250:                        }
1251:                    }
1252:                    String defaultValue = attr.getDefaultText();
1253:                    xmlc.insertAttributeWithValue(attr.getName(),
1254:                            defaultValue == null ? sampleDataForSimpleType(attr
1255:                                    .getType()) : defaultValue);
1256:                }
1257:            }
1258:
1259:            private void processSequence(SchemaParticle sp, XmlCursor xmlc,
1260:                    boolean mixed) {
1261:                SchemaParticle[] spc = sp.getParticleChildren();
1262:                for (int i = 0; i < spc.length; i++) {
1263:                    /// <parent>maybestuff^</parent>
1264:                    processParticle(spc[i], xmlc, mixed);
1265:                    //<parent>maybestuff...morestuff^</parent>
1266:                    if (mixed && i < spc.length - 1)
1267:                        xmlc.insertChars(pick(WORDS));
1268:                }
1269:            }
1270:
1271:            private void processChoice(SchemaParticle sp, XmlCursor xmlc,
1272:                    boolean mixed) {
1273:                SchemaParticle[] spc = sp.getParticleChildren();
1274:                xmlc.insertComment("You have a CHOICE of the next "
1275:                        + String.valueOf(spc.length) + " items at this level"); // NOI18N
1276:                for (int i = 0; i < spc.length; i++) {
1277:                    processParticle(spc[i], xmlc, mixed);
1278:                }
1279:            }
1280:
1281:            private void processAll(SchemaParticle sp, XmlCursor xmlc,
1282:                    boolean mixed) {
1283:                SchemaParticle[] spc = sp.getParticleChildren();
1284:                // xmlc.insertComment("You may enter the following " + String.valueOf(spc.length) + " items in any order");
1285:                for (int i = 0; i < spc.length; i++) {
1286:                    processParticle(spc[i], xmlc, mixed);
1287:                    if (mixed && i < spc.length - 1)
1288:                        xmlc.insertChars(pick(WORDS));
1289:                }
1290:            }
1291:
1292:            private void processWildCard(SchemaParticle sp, XmlCursor xmlc,
1293:                    boolean mixed) {
1294:                xmlc.insertComment("You may enter ANY elements at this point"); // NOI18N
1295:                xmlc.insertElement("AnyElement"); // NOI18N
1296:            }
1297:
1298:            /**
1299:             * This method will get the base type for the schema type
1300:             */
1301:
1302:            private static QName getClosestName(SchemaType sType) {
1303:                while (sType.getName() == null)
1304:                    sType = sType.getBaseType();
1305:
1306:                return sType.getName();
1307:            }
1308:
1309:            private String printParticleType(int particleType) {
1310:                StringBuffer returnParticleType = new StringBuffer();
1311:                returnParticleType.append("Schema Particle Type: "); // NOI18N
1312:
1313:                switch (particleType) {
1314:                case SchemaParticle.ALL:
1315:                    returnParticleType.append("ALL\n"); // NOI18N
1316:                    break;
1317:                case SchemaParticle.CHOICE:
1318:                    returnParticleType.append("CHOICE\n"); // NOI18N
1319:                    break;
1320:                case SchemaParticle.ELEMENT:
1321:                    returnParticleType.append("ELEMENT\n"); // NOI18N
1322:                    break;
1323:                case SchemaParticle.SEQUENCE:
1324:                    returnParticleType.append("SEQUENCE\n"); // NOI18N
1325:                    break;
1326:                case SchemaParticle.WILDCARD:
1327:                    returnParticleType.append("WILDCARD\n"); // NOI18N
1328:                    break;
1329:                default:
1330:                    returnParticleType.append("Schema Particle Type Unknown"); // NOI18N
1331:                    break;
1332:                }
1333:
1334:                return returnParticleType.toString();
1335:            }
1336:
1337:            private ArrayList _typeStack = new ArrayList();
1338:
1339:            public boolean isIgnoreOptional() {
1340:                return ignoreOptional;
1341:            }
1342:
1343:            public void setIgnoreOptional(boolean ignoreOptional) {
1344:                this.ignoreOptional = ignoreOptional;
1345:            }
1346:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.