1 | /* |
2 | * Copyright (c) 2009 The JOMC Project |
3 | * Copyright (c) 2005 Christian Schulte <cs@jomc.org> |
4 | * All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * |
10 | * o Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * |
13 | * o Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in |
15 | * the documentation and/or other materials provided with the |
16 | * distribution. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS" |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
20 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
21 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR |
22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
25 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
27 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
28 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * |
30 | * $Id: DefaultBootstrapContext.java 1255 2010-01-09 23:23:47Z schulte2005 $ |
31 | * |
32 | */ |
33 | package org.jomc.model.bootstrap; |
34 | |
35 | import javax.xml.XMLConstants; |
36 | import javax.xml.bind.JAXBContext; |
37 | import javax.xml.bind.JAXBException; |
38 | import javax.xml.bind.Marshaller; |
39 | import javax.xml.bind.Unmarshaller; |
40 | import javax.xml.validation.SchemaFactory; |
41 | import org.xml.sax.SAXException; |
42 | |
43 | /** |
44 | * Default {@code BootstrapContext} implementation. |
45 | * |
46 | * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> |
47 | * @version $Id: DefaultBootstrapContext.java 1255 2010-01-09 23:23:47Z schulte2005 $ |
48 | * @see BootstrapContext#createBootstrapContext(java.lang.ClassLoader) |
49 | */ |
50 | public class DefaultBootstrapContext extends BootstrapContext |
51 | { |
52 | |
53 | /** |
54 | * Creates a new {@code DefaultBootstrapContext} instance taking a class loader. |
55 | * |
56 | * @param classLoader The class loader of the context. |
57 | */ |
58 | public DefaultBootstrapContext( final ClassLoader classLoader ) |
59 | { |
60 | super( classLoader ); |
61 | } |
62 | |
63 | @Override |
64 | public javax.xml.validation.Schema createSchema() throws BootstrapException |
65 | { |
66 | try |
67 | { |
68 | return SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI ). |
69 | newSchema( this.getClass().getResource( "jomc-bootstrap-1.0.xsd" ) ); |
70 | |
71 | } |
72 | catch ( final SAXException e ) |
73 | { |
74 | throw new BootstrapException( e ); |
75 | } |
76 | } |
77 | |
78 | @Override |
79 | public JAXBContext createContext() throws BootstrapException |
80 | { |
81 | try |
82 | { |
83 | return JAXBContext.newInstance( Schemas.class.getPackage().getName(), this.getClassLoader() ); |
84 | } |
85 | catch ( final JAXBException e ) |
86 | { |
87 | throw new BootstrapException( e ); |
88 | } |
89 | } |
90 | |
91 | @Override |
92 | public Marshaller createMarshaller() throws BootstrapException |
93 | { |
94 | try |
95 | { |
96 | final Marshaller m = this.createContext().createMarshaller(); |
97 | m.setProperty( Marshaller.JAXB_SCHEMA_LOCATION, |
98 | "http://jomc.org/model/bootstrap http://jomc.org/model/bootstrap/jomc-bootstrap-1.0.xsd" ); |
99 | |
100 | return m; |
101 | } |
102 | catch ( final JAXBException e ) |
103 | { |
104 | throw new BootstrapException( e ); |
105 | } |
106 | } |
107 | |
108 | @Override |
109 | public Unmarshaller createUnmarshaller() throws BootstrapException |
110 | { |
111 | try |
112 | { |
113 | return this.createContext().createUnmarshaller(); |
114 | } |
115 | catch ( final JAXBException e ) |
116 | { |
117 | throw new BootstrapException( e ); |
118 | } |
119 | } |
120 | |
121 | // SECTION-END |
122 | } |