2020import java .util .Map ;
2121
2222import javax .xml .XMLConstants ;
23+ import javax .xml .bind .DatatypeConverter ;
2324import javax .xml .transform .stream .StreamSource ;
2425import javax .xml .validation .Schema ;
2526import javax .xml .validation .SchemaFactory ;
2829import org .raml .parser .XsdResourceResolver ;
2930import org .raml .parser .loader .ResourceLoader ;
3031import org .raml .parser .tagresolver .ContextPath ;
32+ import org .raml .parser .tagresolver .IncludeResolver ;
33+ import org .yaml .snakeyaml .nodes .ScalarNode ;
3134
32- public class SchemaCompiler
35+ public final class SchemaCompiler
3336{
3437
35- private final static SchemaCompiler instance = new SchemaCompiler ();
38+ private static final String SEPARATOR = "-|_" ;
39+ private static final SchemaCompiler instance = new SchemaCompiler ();
3640 private ContextPath contextPath ;
3741 private ResourceLoader resourceLoader ;
3842
@@ -55,28 +59,35 @@ public void init(ContextPath contextPath, ResourceLoader resourceLoader)
5559 this .resourceLoader = resourceLoader ;
5660 }
5761
58- public Map <String , Object > compile (Map <String , String > schemas )
62+ public Map <String , Object > compile (Map <String , String > encodedSchemas )
5963 {
6064 Map <String , Object > compiledSchemas = new HashMap <String , Object >();
61- for (Map .Entry <String , String > schema : schemas .entrySet ())
65+ for (Map .Entry <String , String > encodedSchema : encodedSchemas .entrySet ())
6266 {
63- Schema compiledSchema = compile (schema .getValue ());
67+ String [] pathAndSchema = decodeIncludePath (encodedSchema .getValue ());
68+ Schema compiledSchema = compile (pathAndSchema [1 ], pathAndSchema [0 ]);
6469 if (compiledSchema != null )
6570 {
66- compiledSchemas .put (schema .getKey (), compiledSchema );
71+ compiledSchemas .put (encodedSchema .getKey (), compiledSchema );
6772 }
73+ encodedSchema .setValue (pathAndSchema [1 ]);
6874 }
6975 return compiledSchemas ;
7076 }
7177
72- public Schema compile (String schema )
78+ public Schema compile (String schema , String path )
7379 {
7480 Schema compiledSchema = null ;
7581 String trimmedSchema = StringUtils .trimToEmpty (schema );
7682 if (trimmedSchema .startsWith ("<" ) && trimmedSchema .endsWith (">" ))
7783 {
7884 SchemaFactory factory = SchemaFactory .newInstance (XMLConstants .W3C_XML_SCHEMA_NS_URI );
79- factory .setResourceResolver (new XsdResourceResolver (contextPath , resourceLoader ));
85+ ContextPath actualContextPath = contextPath ;
86+ if (path != null )
87+ {
88+ actualContextPath = new ContextPath (new IncludeInfo (path ));
89+ }
90+ factory .setResourceResolver (new XsdResourceResolver (actualContextPath , resourceLoader ));
8091 try
8192 {
8293 compiledSchema = factory .newSchema (new StreamSource (new StringReader (trimmedSchema )));
@@ -89,4 +100,35 @@ public Schema compile(String schema)
89100 }
90101 return compiledSchema ;
91102 }
103+
104+ public Schema compile (String schema )
105+ {
106+ return compile (schema , null );
107+ }
108+
109+ public static String encodeIncludePath (ScalarNode node )
110+ {
111+ String schema = node .getValue ();
112+ String includePath = "" ;
113+ if (node instanceof IncludeResolver .IncludeScalarNode )
114+ {
115+ includePath = ((IncludeResolver .IncludeScalarNode ) node ).getIncludeName ();
116+ }
117+ String includeEncoded = DatatypeConverter .printBase64Binary (includePath .getBytes ());
118+
119+ return includeEncoded + SEPARATOR + schema ;
120+ }
121+
122+ public static String [] decodeIncludePath (String encodedSchema )
123+ {
124+ int idx = encodedSchema .indexOf (SEPARATOR );
125+ if (idx == -1 )
126+ {
127+ throw new IllegalArgumentException ("Invalid include encoded schema." );
128+ }
129+ String base64Path = encodedSchema .substring (0 , idx );
130+ String includePath = new String (DatatypeConverter .parseBase64Binary (base64Path ));
131+ String schema = encodedSchema .substring (idx + SEPARATOR .length (), encodedSchema .length ());
132+ return new String [] {includePath , schema };
133+ }
92134}
0 commit comments