Source Code Cross Referenced for DateTime.java in  » J2EE » enhydra » org » apache » xmlbeans » samples » datetime » 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 » J2EE » enhydra » org.apache.xmlbeans.samples.datetime 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*   Copyright 2004 The Apache Software Foundation
002:         *
003:         *   Licensed under the Apache License, Version 2.0 (the "License");
004:         *   you may not use this file except in compliance with the License.
005:         *   You may obtain a copy of the License at
006:         *
007:         *       http://www.apache.org/licenses/LICENSE-2.0
008:         *
009:         *   Unless required by applicable law or agreed to in writing, software
010:         *   distributed under the License is distributed on an "AS IS" BASIS,
011:         *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012:         *   See the License for the specific language governing permissions and
013:         *  limitations under the License.
014:         */
015:
016:        package org.apache.xmlbeans.samples.datetime;
017:
018:        import java.io.File;
019:        import java.io.IOException;
020:        import java.util.Calendar;
021:        import java.util.ArrayList;
022:
023:        import org.apache.xmlbeans.XmlException;
024:        import org.apache.xmlbeans.XmlOptions;
025:        import org.apache.xmlbeans.XmlError;
026:        import org.apache.xmlbeans.XmlObject;
027:
028:        import org.apache.xmlbeans.GDuration;
029:        import org.apache.xmlbeans.XmlDate;
030:        import org.apache.xmlbeans.XmlCalendar;
031:
032:        import java.util.Calendar;
033:        import java.text.SimpleDateFormat;
034:        import java.util.Date;
035:
036:        import org.apache.xmlbeans.samples.datetime.ImportantDate;
037:        import org.apache.xmlbeans.samples.datetime.DatetimeDocument;
038:
039:        /**
040:         * The sample illustrates how you can work with XML Schema types date,
041:         * dateTime, time, duration, gDay.
042:         * It parses the XML Document, prints first occurence of <important-date>
043:         * value, creates a new <important-date> element and saves it in a new XML Document.
044:         * This sample illustrates how you can convert XMLBean types to Java types
045:         * (java.util.Date, java.util.Calendar).
046:         * It uses the schema defined in datetime.xsd.
047:         */
048:        public class DateTime {
049:
050:            /**
051:             * Receives an XML Instance and prints the element values,
052:             * Also creates a new XML Instance.
053:             *
054:             * @param args An array containing
055:             *             (a)Path to the XML Instance conforming to the XML schema in datetime.xsd.
056:             *             (b)Path for creating a new XML Instance.
057:             */
058:            public static void main(String args[]) {
059:                // Create an instance of this class to work with.
060:                DateTime dt = new DateTime();
061:
062:                // Create an instance of a Datetime type based on the received XML's schema
063:                DatetimeDocument doc = dt.parseXml(args[0]);
064:
065:                // Prints the element values from the XML
066:                dt.printInstance(doc);
067:
068:                // Creates a new XML and saves the file
069:                dt.createDocument(doc, args[1]);
070:
071:            }
072:
073:            /**
074:             * Creates a File from the XML path provided in main arguments, then
075:             * parses the file's contents into a type generated from schema.
076:             */
077:            public DatetimeDocument parseXml(String file) {
078:                // Get the XML instance into a file using the path provided.
079:                File xmlfile = new File(file);
080:
081:                // Create an instance of a type generated from schema to hold the XML.
082:                DatetimeDocument doc = null;
083:
084:                try {
085:                    // Parse the instance into the type generated from the schema.
086:                    doc = DatetimeDocument.Factory.parse(xmlfile);
087:                } catch (XmlException e) {
088:                    e.printStackTrace();
089:                } catch (IOException e) {
090:                    e.printStackTrace();
091:                }
092:                return doc;
093:            }
094:
095:            /**
096:             * This method prints first occurence of <important-date>
097:             * value. It also prints converted values from  XMLBean types to Java types
098:             * (java.util.Date, java.util.Calendar) and org.apache.xmlbeans.GDate.
099:             */
100:            public void printInstance(DatetimeDocument doc) {
101:                // Retrieve the <datetime> element and get an array of
102:                // the <important-date> elements it contains.
103:                DatetimeDocument.Datetime dtelement = doc.getDatetime();
104:                ImportantDate[] impdate = dtelement.getImportantDateArray();
105:
106:                // Loop through the <important-date> elements, printing the
107:                // values for each.
108:                for (int i = 0; i < impdate.length; i++) {
109:
110:                    //For purpose of simplicity in output, only first occurrence is printed.
111:                    if (i == 0) {
112:
113:                        //Retrieving all <holiday> elements within  <important-date> element
114:                        XmlDate[] holiday = impdate[i].xgetHolidayArray();
115:                        System.out.println("Holiday(xs:date): ");
116:
117:                        for (int j = 0; j < holiday.length; j++) {
118:                            if (j == 0) {
119:                                //XmlDate to java.util.Calendar,org.apache.xmlbeans.GDate, java.util.Date
120:                                System.out.println("Calendar:"
121:                                        + holiday[j].getCalendarValue());
122:                                System.out.println("Date:"
123:                                        + holiday[j].getDateValue());
124:                                System.out.println("GDate:"
125:                                        + holiday[j].getGDateValue() + "\n");
126:                            }
127:                        }
128:
129:                        //XmlTime to java.util.Calendar, org.apache.xmlbeans.GDate, java.util.Date
130:                        System.out.println("Fun Begin Time(xs:time): ");
131:                        System.out.println("Calendar:"
132:                                + impdate[i].getFunBeginTime());
133:                        System.out
134:                                .println("GDate:"
135:                                        + impdate[i].xgetFunBeginTime()
136:                                                .getGDateValue());
137:
138:                        //To convert java.util.Calendar to java.util.Date
139:                        SimpleDateFormat sdf = new SimpleDateFormat("H:mm:ss");
140:                        Date dt = impdate[i].getFunBeginTime().getTime();
141:                        System.out.println("Date:" + sdf.format(dt) + "\n");
142:
143:                        //XmlDuration to org.apache.xmlbeans.GDuration
144:                        System.out.println("Job Duration(xs:duration): ");
145:                        System.out.println("GDuration:"
146:                                + impdate[i].getJobDuration() + "\n");
147:
148:                        //XmlDate to Calendar,GDate, Date
149:                        System.out.println("Birth DateTime(xs:dateTime): ");
150:                        System.out.println("Calendar:"
151:                                + impdate[i].getBirthdatetime());
152:                        System.out
153:                                .println("Date:"
154:                                        + impdate[i].xgetBirthdatetime()
155:                                                .getDateValue());
156:                        System.out.println("GDate:"
157:                                + impdate[i].xgetBirthdatetime()
158:                                        .getGDateValue() + "\n");
159:
160:                        //XmlGday to Calendar,GDate, Day - primitive java int
161:                        System.out.println("Pay Day(xs:gDay): ");
162:                        System.out
163:                                .println("Calendar:" + impdate[i].getPayday());
164:                        System.out.println("GDate:"
165:                                + impdate[i].xgetPayday().getGDateValue());
166:                        System.out.println("Day:"
167:                                + impdate[i].xgetPayday().getGDateValue()
168:                                        .getDay() + "\n");
169:
170:                        System.out.println("\n\n");
171:                    }
172:                }
173:
174:            }
175:
176:            /**
177:             * This method creates an new <important-date> element and attaches to the existing XML Instance, and saves the
178:             * new Instance to a file(args[1]).
179:             */
180:            public void createDocument(DatetimeDocument doc, String file) {
181:                // Retrieve the <datetime> element and add a new <important-date> element.
182:                DatetimeDocument.Datetime dtelement = doc.getDatetime();
183:
184:                //
185:                // add an important date using XmlCalendar
186:                //
187:
188:                ImportantDate impdate = dtelement.addNewImportantDate();
189:
190:                //Creating value for <holiday> element
191:                Calendar holiday = new XmlCalendar("2004-07-04");
192:
193:                //Creating value for <fun-begin-time> element
194:                Calendar funbegintime = new XmlCalendar("10:30:33");
195:
196:                //Creating value for <fun-end-time> element
197:                Calendar funendtime = new XmlCalendar("12:40:12");
198:
199:                //Creating value for <birthdatetime> element
200:                Calendar birthdatetime = new XmlCalendar("1977-11-29T10:10:12");
201:
202:                //Creating value for <job-duration> element
203:                GDuration jobduration = new GDuration(1, 2, 4, 5, 10, 12, 15,
204:                        null);
205:
206:                //Creating value for <payday> element
207:                Calendar payday = new XmlCalendar("---12");
208:
209:                //Setting all the elements
210:                impdate.addHoliday(holiday);
211:                impdate.setFunBeginTime(funbegintime);
212:                impdate.setFunEndTime(funendtime);
213:                impdate.setJobDuration(jobduration);
214:                impdate.setBirthdatetime(birthdatetime);
215:                impdate.setPayday(payday);
216:                impdate.setDescription("Using XmlCalendar");
217:
218:                //
219:                // add another important date using Calendar
220:                //
221:
222:                impdate = dtelement.addNewImportantDate();
223:
224:                //Creating value for <holiday> element using XmlCalendar
225:                holiday = new XmlCalendar("2004-07-04");
226:
227:                //Creating value for <fun-begin-time> element using GregorianCalendar
228:                funbegintime = Calendar.getInstance();
229:                funbegintime.clear();
230:                funbegintime.set(Calendar.AM_PM, Calendar.AM);
231:                funbegintime.set(Calendar.HOUR, 10);
232:                funbegintime.set(Calendar.MINUTE, 30);
233:                funbegintime.set(Calendar.SECOND, 35);
234:
235:                //Creating value for <fun-end-time> element
236:                funendtime = Calendar.getInstance();
237:                funendtime.clear();
238:                funendtime.set(Calendar.AM_PM, Calendar.AM);
239:                funendtime.set(Calendar.HOUR, 12);
240:                funendtime.set(Calendar.MINUTE, 40);
241:                funendtime.set(Calendar.SECOND, 12);
242:
243:                //Creating value for <birthdatetime> element
244:                birthdatetime = Calendar.getInstance();
245:                birthdatetime.clear();
246:                birthdatetime.set(1977, 10, 29, 10, 10, 12);
247:
248:                //Creating value for <job-duration> element
249:                jobduration = new GDuration(1, 2, 4, 5, 10, 12, 15, null);
250:
251:                //Creating value for <payday> element
252:                payday = Calendar.getInstance();
253:                payday.clear();
254:                payday.set(Calendar.DAY_OF_MONTH, 12);
255:
256:                //Setting all the elements
257:                impdate.addHoliday(holiday);
258:                impdate.setFunBeginTime(funbegintime);
259:                impdate.setFunEndTime(funendtime);
260:                impdate.setJobDuration(jobduration);
261:                impdate.setBirthdatetime(birthdatetime);
262:                impdate.setPayday(payday);
263:                impdate.setDescription("Using Calendar");
264:
265:                XmlOptions xmlOptions = new XmlOptions();
266:                xmlOptions.setSavePrettyPrint();
267:
268:                // Validate the new XML
269:                boolean isXmlValid = validateXml(doc);
270:                if (isXmlValid) {
271:                    File f = new File(file);
272:
273:                    try {
274:                        //Writing the XML Instance to a file.
275:                        doc.save(f, xmlOptions);
276:                    } catch (IOException e) {
277:                        e.printStackTrace();
278:                    }
279:                    System.out.println("\nXML Instance Document saved at : "
280:                            + f.getPath());
281:                }
282:            }
283:
284:            /**
285:             * <p>Validates the XML, printing error messages when the XML is invalid. Note
286:             * that this method will properly validate any instance of a compiled schema
287:             * type because all of these types extend XmlObject.</p>
288:             *
289:             * <p>Note that in actual practice, you'll probably want to use an assertion
290:             * when validating if you want to ensure that your code doesn't pass along
291:             * invalid XML. This sample prints the generated XML whether or not it's
292:             * valid so that you can see the result in both cases.</p>
293:             *
294:             * @param xml The XML to validate.
295:             * @return <code>true</code> if the XML is valid; otherwise, <code>false</code>
296:             */
297:            public boolean validateXml(XmlObject xml) {
298:                boolean isXmlValid = false;
299:
300:                // A collection instance to hold validation error messages.
301:                ArrayList validationMessages = new ArrayList();
302:
303:                // Validate the XML, collecting messages.
304:                isXmlValid = xml.validate(new XmlOptions()
305:                        .setErrorListener(validationMessages));
306:
307:                if (!isXmlValid) {
308:                    System.out.println("Invalid XML: ");
309:                    for (int i = 0; i < validationMessages.size(); i++) {
310:                        XmlError error = (XmlError) validationMessages.get(i);
311:                        System.out.println(error.getMessage());
312:                        System.out.println(error.getObjectLocation());
313:                    }
314:                }
315:                return isXmlValid;
316:            }
317:
318:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.