Skip to content
This repository was archived by the owner on Feb 12, 2022. It is now read-only.

Commit 89cd8b4

Browse files
committed
Support XSD includes
fixes #51
1 parent 6b69168 commit 89cd8b4

26 files changed

+656
-27
lines changed

src/main/java/org/raml/model/MimeType.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.raml.parser.annotation.Key;
2424
import org.raml.parser.annotation.Mapping;
2525
import org.raml.parser.annotation.Scalar;
26+
import org.raml.parser.builder.SchemaTupleBuilder;
2627

2728
public class MimeType implements Serializable
2829
{
@@ -32,9 +33,11 @@ public class MimeType implements Serializable
3233
@Key
3334
private String type;
3435

35-
@Scalar(rule = org.raml.parser.rule.SchemaRule.class)
36+
@Scalar(rule = org.raml.parser.rule.SchemaRule.class, builder = SchemaTupleBuilder.class)
3637
private String schema;
3738

39+
private Object compiledSchema;
40+
3841
@Scalar
3942
private String example;
4043

@@ -70,6 +73,16 @@ public void setSchema(String schema)
7073
this.schema = schema;
7174
}
7275

76+
public Object getCompiledSchema()
77+
{
78+
return compiledSchema;
79+
}
80+
81+
public void setCompiledSchema(Object compiledSchema)
82+
{
83+
this.compiledSchema = compiledSchema;
84+
}
85+
7386
public String getExample()
7487
{
7588
return example;

src/main/java/org/raml/model/Raml.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public class Raml implements Serializable
5656
@Sequence(rule = org.raml.parser.rule.GlobalSchemasRule.class)
5757
private List<Map<String, String>> schemas = new ArrayList<Map<String, String>>();
5858

59+
private Map<String, Object> compiledSchemas;
60+
5961
@Sequence
6062
private List<Map<String, Template>> resourceTypes = new ArrayList<Map<String, Template>>();
6163

@@ -236,6 +238,16 @@ public Map<String, String> getConsolidatedSchemas()
236238
return consolidated;
237239
}
238240

241+
public Map<String, Object> getCompiledSchemas()
242+
{
243+
return compiledSchemas;
244+
}
245+
246+
public void setCompiledSchemas(Map<String, Object> compiledSchemas)
247+
{
248+
this.compiledSchemas = compiledSchemas;
249+
}
250+
239251
public Resource getResource(String path)
240252
{
241253
for (Resource resource : resources.values())
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2013 (c) MuleSoft, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing,
11+
* software distributed under the License is distributed on an
12+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific
14+
* language governing permissions and limitations under the License.
15+
*/
16+
package org.raml.parser;
17+
18+
import java.io.IOException;
19+
20+
public class ResolveResourceException extends RuntimeException
21+
{
22+
23+
public ResolveResourceException(IOException e)
24+
{
25+
super(e);
26+
}
27+
28+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2013 (c) MuleSoft, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing,
11+
* software distributed under the License is distributed on an
12+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific
14+
* language governing permissions and limitations under the License.
15+
*/
16+
package org.raml.parser;
17+
18+
import java.io.ByteArrayInputStream;
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
22+
import org.apache.commons.io.IOUtils;
23+
import org.raml.parser.loader.ResourceLoader;
24+
import org.raml.parser.rule.LSInputImpl;
25+
import org.raml.parser.tagresolver.ContextPath;
26+
import org.raml.parser.utils.StreamUtils;
27+
import org.w3c.dom.ls.LSInput;
28+
import org.w3c.dom.ls.LSResourceResolver;
29+
30+
public class XsdResourceResolver implements LSResourceResolver
31+
{
32+
33+
private final ContextPath contextPath;
34+
private final ResourceLoader resourceLoader;
35+
36+
public XsdResourceResolver(ContextPath contextPath, ResourceLoader resourceLoader)
37+
{
38+
this.contextPath = contextPath;
39+
this.resourceLoader = resourceLoader;
40+
}
41+
42+
@Override
43+
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI)
44+
{
45+
String path = contextPath.resolveAbsolutePath(systemId);
46+
if (path == null || path.startsWith("http://"))
47+
{
48+
//delegate resource resolution to xml parser
49+
return null;
50+
}
51+
InputStream inputStream = resourceLoader.fetchResource(path);
52+
if (inputStream == null)
53+
{
54+
//delegate resource resolution to xml parser
55+
return null;
56+
}
57+
byte[] content;
58+
try
59+
{
60+
content = IOUtils.toByteArray(inputStream);
61+
}
62+
catch (IOException e)
63+
{
64+
throw new ResolveResourceException(e);
65+
}
66+
LSInput input = new LSInputImpl(publicId, systemId, baseURI, new ByteArrayInputStream(content), StreamUtils.detectEncoding(content));
67+
return input;
68+
}
69+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2013 (c) MuleSoft, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing,
11+
* software distributed under the License is distributed on an
12+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific
14+
* language governing permissions and limitations under the License.
15+
*/
16+
package org.raml.parser.builder;
17+
18+
import javax.xml.validation.Schema;
19+
20+
import org.raml.parser.utils.NodeUtils;
21+
import org.raml.parser.utils.ReflectionUtils;
22+
import org.raml.parser.visitor.SchemaCompiler;
23+
import org.yaml.snakeyaml.nodes.ScalarNode;
24+
25+
public class SchemaTupleBuilder extends ScalarTupleBuilder
26+
{
27+
28+
private static final String SCHEMA_FIELD_NAME = "schema";
29+
private static final String PARSED_SCHEMA_FIELD_NAME = "compiledSchema";
30+
31+
public SchemaTupleBuilder()
32+
{
33+
super(SCHEMA_FIELD_NAME, String.class);
34+
}
35+
36+
@Override
37+
public Object buildValue(Object parent, ScalarNode node)
38+
{
39+
Object result = super.buildValue(parent, node);
40+
Object schema = compileSchema(node);
41+
ReflectionUtils.setProperty(parent, PARSED_SCHEMA_FIELD_NAME, schema);
42+
return result;
43+
}
44+
45+
private Object compileSchema(ScalarNode node)
46+
{
47+
String value = node.getValue();
48+
49+
if (value == null || NodeUtils.isNonStringTag(node.getTag()))
50+
{
51+
return null;
52+
}
53+
54+
Schema schema = null;
55+
String mimeType = getParent() instanceof PojoTupleBuilder ? ((PojoTupleBuilder) getParent()).getFieldName() : null;
56+
if (mimeType != null && mimeType.contains("xml"))
57+
{
58+
schema = SchemaCompiler.getInstance().compile(value);
59+
}
60+
return schema;
61+
}
62+
63+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2013 (c) MuleSoft, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing,
11+
* software distributed under the License is distributed on an
12+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific
14+
* language governing permissions and limitations under the License.
15+
*/
16+
package org.raml.parser.loader;
17+
18+
public interface ResourceLoaderAware
19+
{
20+
21+
void setResourceLoader(ResourceLoader resourceLoader);
22+
23+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright 2013 (c) MuleSoft, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing,
11+
* software distributed under the License is distributed on an
12+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific
14+
* language governing permissions and limitations under the License.
15+
*/
16+
package org.raml.parser.rule;
17+
18+
import java.io.ByteArrayInputStream;
19+
import java.io.InputStream;
20+
import java.io.Reader;
21+
22+
import org.w3c.dom.ls.LSInput;
23+
24+
public class LSInputImpl implements LSInput
25+
{
26+
27+
protected Reader characterStream;
28+
protected InputStream byteStream;
29+
protected String stringData;
30+
31+
protected String systemId;
32+
protected String publicId;
33+
protected String baseUri;
34+
35+
protected String encoding;
36+
protected boolean certifiedText;
37+
38+
public LSInputImpl(String publicId, String systemId, String baseURI, ByteArrayInputStream content, String encoding)
39+
{
40+
this.publicId = publicId;
41+
this.systemId = systemId;
42+
this.baseUri = baseURI;
43+
this.byteStream = content;
44+
this.encoding = encoding;
45+
}
46+
47+
@Override
48+
public Reader getCharacterStream()
49+
{
50+
return characterStream;
51+
}
52+
53+
@Override
54+
public void setCharacterStream(Reader characterStream)
55+
{
56+
this.characterStream = characterStream;
57+
}
58+
59+
@Override
60+
public InputStream getByteStream()
61+
{
62+
return byteStream;
63+
}
64+
65+
@Override
66+
public void setByteStream(InputStream byteStream)
67+
{
68+
this.byteStream = byteStream;
69+
}
70+
71+
@Override
72+
public String getStringData()
73+
{
74+
return stringData;
75+
}
76+
77+
@Override
78+
public void setStringData(String stringData)
79+
{
80+
this.stringData = stringData;
81+
}
82+
83+
@Override
84+
public String getSystemId()
85+
{
86+
return systemId;
87+
}
88+
89+
@Override
90+
public void setSystemId(String systemId)
91+
{
92+
this.systemId = systemId;
93+
}
94+
95+
@Override
96+
public String getPublicId()
97+
{
98+
return publicId;
99+
}
100+
101+
@Override
102+
public void setPublicId(String publicId)
103+
{
104+
this.publicId = publicId;
105+
}
106+
107+
@Override
108+
public String getBaseURI()
109+
{
110+
return baseUri;
111+
}
112+
113+
@Override
114+
public void setBaseURI(String baseURI)
115+
{
116+
this.baseUri = baseURI;
117+
}
118+
119+
@Override
120+
public String getEncoding()
121+
{
122+
return encoding;
123+
}
124+
125+
@Override
126+
public void setEncoding(String encoding)
127+
{
128+
this.encoding = encoding;
129+
}
130+
131+
@Override
132+
public boolean getCertifiedText()
133+
{
134+
return certifiedText;
135+
}
136+
137+
@Override
138+
public void setCertifiedText(boolean certifiedText)
139+
{
140+
this.certifiedText = certifiedText;
141+
}
142+
}

0 commit comments

Comments
 (0)