1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.jdtaus.core.container.ri.client;
22
23 import java.io.IOException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.net.URL;
27 import java.util.Locale;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 import org.xml.sax.EntityResolver;
31 import org.xml.sax.InputSource;
32 import org.xml.sax.SAXException;
33
34
35
36
37
38
39
40
41 public class BootstrapEntityResolver implements EntityResolver
42 {
43
44
45
46 private static final String[] SCHEMA_LOCATIONS =
47 {
48 "jdtaus-module-1.0.xsd", "org/jdtaus/core/container/xml/",
49 "jdtaus-module-1.1.xsd", "org/jdtaus/core/container/xml/",
50 "jdtaus-module-1.2.xsd", "org/jdtaus/core/model/container/module/",
51 "jdtaus-core-1.0.xsd", "org/jdtaus/core/model/",
52 "jdtaus-core-1.1.xsd", "org/jdtaus/core/model/",
53 "jdtaus-text-1.0.xsd", "org/jdtaus/core/model/text/",
54 "jdtaus-text-1.1.xsd", "org/jdtaus/core/model/text/",
55 "jdtaus-monitor-1.0.xsd", "org/jdtaus/core/model/monitor/",
56 "jdtaus-monitor-1.1.xsd", "org/jdtaus/core/model/monitor/",
57 "jdtaus-container-1.0.xsd", "org/jdtaus/core/model/container/",
58 "jdtaus-container-1.1.xsd", "org/jdtaus/core/model/container/"
59 };
60
61
62
63
64 public InputSource resolveEntity( final String publicId,
65 final String systemId )
66 throws SAXException, IOException
67 {
68 if ( systemId == null )
69 {
70 throw new NullPointerException( "systemId" );
71 }
72
73 InputSource schemaSource = null;
74
75 try
76 {
77 final URI systemUri = new URI( systemId );
78 String schemaName = systemUri.getPath();
79 if ( schemaName != null )
80 {
81 final int lastIndexOfSlash = schemaName.lastIndexOf( '/' );
82 if ( lastIndexOfSlash != -1 &&
83 lastIndexOfSlash < schemaName.length() )
84 {
85 schemaName = schemaName.substring( lastIndexOfSlash + 1 );
86 }
87
88 for ( int i = SCHEMA_LOCATIONS.length - 2; i >= 0; i -= 2 )
89 {
90 if ( SCHEMA_LOCATIONS[i].equals( schemaName ) )
91 {
92 final String schemaLocation =
93 SCHEMA_LOCATIONS[i + 1] + schemaName;
94
95 final URL schemaUrl = this.getClass().getClassLoader().
96 getResource( schemaLocation );
97
98 if ( schemaUrl == null )
99 {
100 Logger.getLogger( this.getClass().getName() ).log(
101 Level.WARNING,
102 BootstrapEntityResolverBundle.getInstance().
103 getResourceNotAvailableMessage(
104 Locale.getDefault(), schemaLocation ) );
105
106 continue;
107 }
108
109 schemaSource = new InputSource();
110 schemaSource.setPublicId( publicId );
111 schemaSource.setSystemId( schemaUrl.toExternalForm() );
112
113 Logger.getLogger( this.getClass().getName() ).log(
114 Level.FINE,
115 BootstrapEntityResolverBundle.getInstance().
116 getResolvedSystemIdUriMessage(
117 Locale.getDefault(), systemUri.toASCIIString(),
118 schemaSource.getSystemId() ) );
119
120 break;
121 }
122
123 }
124 }
125 }
126 catch ( final URISyntaxException e )
127 {
128 Logger.getLogger( this.getClass().getName() ).log(
129 Level.WARNING,
130 BootstrapEntityResolverBundle.getInstance().
131 getUnsupportedSystemIdUriMessage( Locale.getDefault(), systemId,
132 e.getMessage() ) );
133
134 schemaSource = null;
135 }
136
137 return schemaSource;
138 }
139
140
141
142
143
144 public BootstrapEntityResolver()
145 {
146 super();
147 }
148
149
150 }