RelaxNGDataModel.java
6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.xmp.schema.model.SchemaDescription
* com.adobe.xmp.schema.model.XMPSchemaException
* org.kohsuke.rngom.rngparser.ast.builder.BuildException
* org.kohsuke.rngom.rngparser.ast.builder.SchemaBuilder
* org.kohsuke.rngom.rngparser.ast.om.ParsedPattern
* org.kohsuke.rngom.rngparser.ast.util.CheckingSchemaBuilder
* org.kohsuke.rngom.rngparser.digested.DAnnotation
* org.kohsuke.rngom.rngparser.digested.DGrammarPattern
* org.kohsuke.rngom.rngparser.digested.DPatternVisitor
* org.kohsuke.rngom.rngparser.digested.DSchemaBuilderImpl
* org.kohsuke.rngom.rngparser.parse.IllegalSchemaException
* org.kohsuke.rngom.rngparser.parse.xml.SAXParseable
*/
package com.adobe.xmp.schema.rng.parser;
import com.adobe.xmp.schema.model.SchemaDescription;
import com.adobe.xmp.schema.model.XMPSchemaException;
import com.adobe.xmp.schema.rng.model.PropertyInfo;
import com.adobe.xmp.schema.rng.model.SchemaInfo;
import com.adobe.xmp.schema.rng.parser.SchemaGenerationHandler;
import com.adobe.xmp.schema.rng.parser.annotation.AnnotationsFactory;
import com.adobe.xmp.schema.rng.parser.annotation.RNGAnnotation;
import com.adobe.xmp.schema.rng.parser.annotation.RNGSchemaAnnotation;
import com.adobe.xmp.schema.rng.parser.exceptions.RNGInvalidSchemaException;
import com.adobe.xmp.schema.rng.parser.exceptions.RNGParseException;
import com.adobe.xmp.schema.rng.parser.generator.XMPSchemaGenerator;
import com.adobe.xmp.schema.rng.parser.traverser.GrammarWalker;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.kohsuke.rngom.rngparser.ast.builder.BuildException;
import org.kohsuke.rngom.rngparser.ast.builder.SchemaBuilder;
import org.kohsuke.rngom.rngparser.ast.om.ParsedPattern;
import org.kohsuke.rngom.rngparser.ast.util.CheckingSchemaBuilder;
import org.kohsuke.rngom.rngparser.digested.DAnnotation;
import org.kohsuke.rngom.rngparser.digested.DGrammarPattern;
import org.kohsuke.rngom.rngparser.digested.DPatternVisitor;
import org.kohsuke.rngom.rngparser.digested.DSchemaBuilderImpl;
import org.kohsuke.rngom.rngparser.parse.IllegalSchemaException;
import org.kohsuke.rngom.rngparser.parse.xml.SAXParseable;
import org.w3c.dom.Element;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
public class RelaxNGDataModel {
private GrammarWalker visitor;
private DGrammarPattern grammar;
private RelaxNGDataModel(DGrammarPattern grammar) throws RNGParseException {
this.grammar = grammar;
this.visitor = new GrammarWalker(grammar);
}
private static DGrammarPattern constructGrammar(InputSource is, EntityResolver er, ErrorHandler eh) throws RNGInvalidSchemaException {
SAXParseable p = new SAXParseable(is, eh, er);
CheckingSchemaBuilder sb = new CheckingSchemaBuilder((SchemaBuilder)new DSchemaBuilderImpl(), eh);
DGrammarPattern grammar = null;
try {
grammar = (DGrammarPattern)p.parse((SchemaBuilder)sb);
}
catch (BuildException e) {
throw new RNGParseException("Exception occured while parsing input RelaxNG schema", (Throwable)e);
}
catch (IllegalSchemaException e) {
throw new RNGInvalidSchemaException(e);
}
return grammar;
}
public static RelaxNGDataModel newInstance(URI rngFile, EntityResolver er) throws RNGParseException, RNGInvalidSchemaException, MalformedURLException, IOException {
DefaultHandler eh = new DefaultHandler(){
public void error(SAXParseException e) throws SAXException {
throw e;
}
};
return RelaxNGDataModel.newInstance(rngFile, er, (ErrorHandler)eh);
}
public static RelaxNGDataModel newInstance(URI rngFile, EntityResolver er, ErrorHandler eh) throws RNGInvalidSchemaException, MalformedURLException, IOException {
InputSource is = new InputSource(rngFile.toURL().openStream());
return RelaxNGDataModel.newInstance(is, er, eh);
}
public static RelaxNGDataModel newInstance(InputSource is, EntityResolver er) throws RNGInvalidSchemaException {
DefaultHandler eh = new DefaultHandler(){
public void error(SAXParseException e) throws SAXException {
throw e;
}
};
return RelaxNGDataModel.newInstance(is, er, (ErrorHandler)eh);
}
public static RelaxNGDataModel newInstance(InputSource is, EntityResolver er, ErrorHandler eh) throws RNGInvalidSchemaException {
DGrammarPattern grammar = RelaxNGDataModel.constructGrammar(is, er, eh);
return new RelaxNGDataModel(grammar);
}
public SchemaDescription constructXMPSchema(SchemaGenerationHandler handler) throws RNGParseException, XMPSchemaException {
if (handler == null) {
throw new IllegalArgumentException("ISchemaGenerationTraceHandler used for constructing schemas should not be null");
}
this.visitor.setSchemaGenerationHandler(handler);
this.grammar.accept((DPatternVisitor)this.visitor);
ArrayList<PropertyInfo> propInfoList = this.visitor.getPropInfoList();
if (propInfoList == null || propInfoList.size() == 0) {
throw new RNGParseException("Schema files defined a schema without any properties.");
}
PropertyInfo firstProperty = propInfoList.get(0);
SchemaInfo info = firstProperty.getSchemaInfo();
handler.startSchemaConstruction(firstProperty.getNS(), info.getPrefix());
this.handleAnnotation(info, 0);
XMPSchemaGenerator rngGenerator = new XMPSchemaGenerator(firstProperty.getNS(), info.getLabel(), info.getDescription(), propInfoList);
return rngGenerator.getModel();
}
private void handleAnnotation(SchemaInfo schemaInfo, int index) {
DAnnotation annot = this.grammar.getAnnotation();
if (annot == null) {
return;
}
List elements = annot.getChildren();
if (elements == null || index >= elements.size()) {
return;
}
RNGAnnotation rngAnnot = AnnotationsFactory.createAnnotation((Element)elements.get(index));
if (rngAnnot == null) {
return;
}
if (rngAnnot instanceof RNGSchemaAnnotation) {
((RNGSchemaAnnotation)rngAnnot).setAnnotationData(schemaInfo);
}
}
}