use of org.apache.ws.commons.schema.XmlSchemaComplexType in project convertigo by convertigo.
the class GetRequestHeaderStep method getXmlSchemaObject.
@Override
public XmlSchemaElement getXmlSchemaObject(XmlSchemaCollection collection, XmlSchema schema) {
XmlSchemaElement element = (XmlSchemaElement) super.getXmlSchemaObject(collection, schema);
XmlSchemaComplexType cType = XmlSchemaUtils.makeDynamic(this, new XmlSchemaComplexType(schema));
element.setType(cType);
XmlSchemaSequence sequence = XmlSchemaUtils.makeDynamic(this, new XmlSchemaSequence());
cType.setParticle(sequence);
SchemaMeta.setContainerXmlSchemaGroupBase(element, sequence);
XmlSchemaElement elt = XmlSchemaUtils.makeDynamic(this, new XmlSchemaElement());
sequence.getItems().add(elt);
elt.setName("name");
elt.setMinOccurs(1);
elt.setMaxOccurs(1);
elt.setSchemaTypeName(Constants.XSD_STRING);
elt = XmlSchemaUtils.makeDynamic(this, new XmlSchemaElement());
sequence.getItems().add(elt);
elt.setName("value");
elt.setMinOccurs(1);
elt.setMaxOccurs(1);
elt.setSchemaTypeName(Constants.XSD_STRING);
return element;
}
use of org.apache.ws.commons.schema.XmlSchemaComplexType in project convertigo by convertigo.
the class Migration7_0_0 method handleSteps.
private static void handleSteps(XmlSchema projectSchema, Map<String, Reference> referenceMap, List<Step> stepList) {
for (Step step : stepList) {
if (step instanceof XMLActionStep) {
XMLVector<XMLVector<Object>> sourcesDefinition = ((XMLActionStep) step).getSourcesDefinition();
for (XMLVector<Object> row : sourcesDefinition) {
if (row.size() > 1) {
XMLVector<String> definition = GenericUtils.cast(row.get(1));
handleSourceDefinition(definition);
}
}
}
if (step instanceof TransactionStep) {
XMLVector<String> definition = ((TransactionStep) step).getConnectionStringDefinition();
handleSourceDefinition(definition);
}
if (step instanceof IStepSourceContainer) {
/**
* Case step's xpath has not been migrated when project has been deployed
** on a 5.0 server from a Studio with an older version *
*/
IStepSourceContainer stepSourceContainer = (IStepSourceContainer) step;
XMLVector<String> definition = stepSourceContainer.getSourceDefinition();
handleSourceDefinition(definition);
}
if (step instanceof IVariableContainer) {
IVariableContainer variableContainer = (IVariableContainer) step;
for (Variable variable : variableContainer.getVariables()) {
if (variable instanceof IStepSourceContainer) {
IStepSourceContainer stepSourceContainer = (IStepSourceContainer) variable;
XMLVector<String> definition = stepSourceContainer.getSourceDefinition();
handleSourceDefinition(definition);
}
}
}
String targetProjectName = null;
String typeLocalName = null;
if (step instanceof TransactionStep) {
targetProjectName = ((TransactionStep) step).getProjectName();
typeLocalName = ((TransactionStep) step).getConnectorName() + "__" + ((TransactionStep) step).getTransactionName() + "ResponseType";
} else if (step instanceof SequenceStep) {
targetProjectName = ((SequenceStep) step).getProjectName();
typeLocalName = ((SequenceStep) step).getSequenceName() + "ResponseType";
}
String namespaceURI = null;
// Case of Requestable steps
if (targetProjectName != null) {
try {
namespaceURI = Project.CONVERTIGO_PROJECTS_NAMESPACEURI + targetProjectName;
if (!targetProjectName.equals(step.getProject().getName())) {
try {
namespaceURI = Engine.theApp.databaseObjectsManager.getOriginalProjectByName(targetProjectName).getTargetNamespace();
} catch (Exception e) {
}
// Add reference
String location = "../" + targetProjectName + "/" + targetProjectName + ".xsd";
addReferenceToMap(referenceMap, namespaceURI, location);
}
// Set step's typeQName
step.setXmlComplexTypeAffectation(new XmlQName(new QName(namespaceURI, typeLocalName)));
} catch (Exception e) {
e.printStackTrace();
}
} else // Other steps
{
try {
String targetNamespace = projectSchema.getTargetNamespace();
String targetPrefix = projectSchema.getNamespaceContext().getPrefix(targetNamespace);
String s = null;
try {
if (step instanceof XMLCopyStep) {
XmlSchemaCollection collection = SchemaMeta.getCollection(projectSchema);
XmlSchemaObject ob = step.getXmlSchemaObject(collection, projectSchema);
if (ob != null) {
if (ob instanceof XmlSchemaSequence) {
ob = ((XmlSchemaSequence) ob).getItems().getItem(0);
}
if (ob instanceof XmlSchemaElement || ob instanceof XmlSchemaAttribute) {
QName schemaTypeName = ob instanceof XmlSchemaElement ? ((XmlSchemaElement) ob).getSchemaTypeName() : ((XmlSchemaAttribute) ob).getSchemaTypeName();
String schemaTypePrefix = projectSchema.getNamespaceContext().getPrefix(schemaTypeName.getNamespaceURI());
String schemaTypeLocalName = schemaTypeName.getLocalPart();
s = schemaTypePrefix + ":" + schemaTypeLocalName;
}
}
} else {
String schemaType = step.getSchemaDataType();
s = schemaType.equals("") ? "xsd:string" : schemaType;
}
} catch (Exception e) {
s = "xsd:string";
}
if ((s != null) && (!s.equals("")) && (!s.startsWith("xsd:"))) {
String prefix = s.split(":")[0];
typeLocalName = s.split(":")[1];
if (prefix.equals(targetPrefix)) {
// ignore
} else {
// Retrieve namespace uri
namespaceURI = projectSchema.getNamespaceContext().getNamespaceURI(prefix);
// Set step's typeQName
QName qname = new QName(namespaceURI, typeLocalName);
XmlSchemaType schemaType = projectSchema.getTypeByName(qname);
if (schemaType instanceof XmlSchemaComplexType)
step.setXmlComplexTypeAffectation(new XmlQName(qname));
if (schemaType instanceof XmlSchemaSimpleType)
step.setXmlSimpleTypeAffectation(new XmlQName(qname));
}
}
} catch (Exception e) {
}
}
if (step instanceof ISimpleTypeAffectation) {
QName qName = XmlSchemaUtils.getSchemaDataTypeName(step.getSchemaDataType());
step.setXmlSimpleTypeAffectation(new XmlQName(qName));
}
if (step instanceof StepWithExpressions) {
handleSteps(projectSchema, referenceMap, ((StepWithExpressions) step).getSteps());
}
}
}
use of org.apache.ws.commons.schema.XmlSchemaComplexType in project convertigo by convertigo.
the class XmlSchemaBuilder method midBuildSchema.
private void midBuildSchema(final int nb) throws EngineException {
// System.out.println("buildSchema for "+ getTargetNamespace());
boolean fullSchema = isFull;
try {
new WalkHelper() {
@Override
protected void walk(DatabaseObject databaseObject) throws Exception {
if (databaseObject instanceof ISchemaGenerator) {
// generate itself and add to the caller list
if (databaseObject instanceof ISchemaImportGenerator) {
// Import case
if (databaseObject instanceof ProjectSchemaReference) {
ProjectSchemaReference ref = (ProjectSchemaReference) databaseObject;
String targetProjectName = ref.getParser().getProjectName();
String tns = Project.getProjectTargetNamespace(targetProjectName);
if (collection.schemaForNamespace(tns) == null) {
if (SchemaMeta.getXmlSchemaObject(schema, databaseObject) == null) {
XmlSchemaImport schemaImport = new XmlSchemaImport();
schemaImport.setNamespace(tns);
SchemaMeta.setXmlSchemaObject(schema, databaseObject, schemaImport);
XmlSchemaUtils.add(schema, schemaImport);
} else {
XmlSchemaBuilder builder = builderExecutor.getBuilderByTargetNamespace(tns);
if (builder != null) {
XmlSchemaUtils.remove(schema, SchemaMeta.getXmlSchemaObject(schema, databaseObject));
XmlSchema xmlSchema = collection.read(builder.schema.getSchemaDocument(), null);
XmlSchemaImport schemaImport = new XmlSchemaImport();
schemaImport.setNamespace(tns);
schemaImport.setSchema(xmlSchema);
SchemaMeta.setXmlSchemaObject(schema, databaseObject, schemaImport);
XmlSchemaUtils.add(schema, schemaImport);
}
}
}
}
}
} else {
// doesn't generate schema, just deep walk
super.walk(databaseObject);
}
}
@Override
protected boolean before(DatabaseObject databaseObject, Class<? extends DatabaseObject> dboClass) {
// just walk references
return Reference.class.isAssignableFrom(dboClass);
}
}.init(project);
// add missing references import
if (nb == 1) {
if (this.equals(builderExecutor.getMainBuilder())) {
List<String> refs = new ArrayList<String>();
SchemaManager.getProjectReferences(refs, projectName);
List<String> missing = new ArrayList<String>();
missing.addAll(refs);
missing.remove(projectName);
for (String pname : refs) {
XmlSchemaObjectCollection col = schema.getIncludes();
for (int i = 0; i < col.getCount(); i++) {
XmlSchemaObject ob = col.getItem(i);
if (ob instanceof XmlSchemaImport) {
XmlSchemaImport xmlSchemaImport = (XmlSchemaImport) ob;
String tns = Project.getProjectTargetNamespace(pname);
if (xmlSchemaImport.getNamespace().equals(tns)) {
missing.remove(pname);
}
}
}
}
for (String pname : missing) {
String tns = Project.getProjectTargetNamespace(pname);
XmlSchemaBuilder builder = builderExecutor.getBuilderByTargetNamespace(tns);
if (builder != null) {
XmlSchema xmlSchema = collection.read(builder.schema.getSchemaDocument(), null);
XmlSchemaImport schemaImport = new XmlSchemaImport();
schemaImport.setNamespace(tns);
schemaImport.setSchema(xmlSchema);
XmlSchemaUtils.add(schema, schemaImport);
}
}
}
}
new WalkHelper() {
List<XmlSchemaParticle> particleChildren;
List<XmlSchemaAttribute> attributeChildren;
@Override
protected void walk(DatabaseObject databaseObject) throws Exception {
// Transaction case
if (databaseObject instanceof Transaction) {
Transaction transaction = (Transaction) databaseObject;
String ns = schema.getTargetNamespace();
List<QName> partElementQNames = new ArrayList<QName>();
partElementQNames.add(new QName(ns, transaction.getXsdRequestElementName()));
partElementQNames.add(new QName(ns, transaction.getXsdResponseElementName()));
LinkedHashMap<QName, XmlSchemaObject> map = new LinkedHashMap<QName, XmlSchemaObject>();
XmlSchemaWalker dw = XmlSchemaWalker.newDependencyWalker(map, true, true);
for (QName qname : partElementQNames) {
dw.walkByElementRef(schema, qname);
}
for (QName qname : map.keySet()) {
String nsURI = qname.getNamespaceURI();
if (nsURI.equals(ns))
continue;
if (nsURI.equals(Constants.URI_2001_SCHEMA_XSD))
continue;
SchemaManager.addXmlSchemaImport(collection, schema, nsURI);
}
map.clear();
// add the 'statistics' element
if (transaction.getAddStatistics()) {
XmlSchemaComplexType xmlSchemaComplexType = (XmlSchemaComplexType) schema.getTypeByName(transaction.getXsdResponseTypeName());
XmlSchemaGroupBase xmlSchemaGroupBase = (XmlSchemaGroupBase) xmlSchemaComplexType.getParticle();
XmlSchemaType statisticsType = schema.getTypeByName("ConvertigoStatsType");
XmlSchemaElement eStatistics = XmlSchemaUtils.makeDynamicReadOnly(databaseObject, new XmlSchemaElement());
eStatistics.setName("statistics");
eStatistics.setMinOccurs(0);
eStatistics.setMaxOccurs(1);
eStatistics.setSchemaTypeName(statisticsType.getQName());
xmlSchemaGroupBase.getItems().add(eStatistics);
SchemaMeta.getReferencedDatabaseObjects(statisticsType).add(transaction);
}
} else // Sequence case
if (databaseObject instanceof Sequence) {
Sequence sequence = (Sequence) databaseObject;
// System.out.println("--sequence:"+ sequence.toString());
particleChildren = new LinkedList<XmlSchemaParticle>();
attributeChildren = new LinkedList<XmlSchemaAttribute>();
super.walk(databaseObject);
// check for an 'error' element if needed
boolean errorFound = false;
XmlSchemaType errorType = schema.getTypeByName("ConvertigoError");
if (errorType != null) {
Set<DatabaseObject> dbos = SchemaMeta.getReferencedDatabaseObjects(errorType);
for (DatabaseObject dbo : dbos) {
if (dbo instanceof Step) {
Step errorStep = (Step) dbo;
if (errorStep.getSequence().equals(sequence) && (errorStep instanceof XMLCopyStep || errorStep.getStepNodeName().equals("error"))) {
errorFound = true;
break;
}
}
}
}
// set particle : choice or sequence
XmlSchemaComplexType cType = (XmlSchemaComplexType) schema.getTypeByName(sequence.getComplexTypeAffectation().getLocalPart());
XmlSchemaSequence xmlSeq = new XmlSchemaSequence();
XmlSchemaChoice xmlChoice = new XmlSchemaChoice();
cType.setParticle(errorFound ? xmlSeq : xmlChoice);
if (!errorFound) {
XmlSchemaElement eError = XmlSchemaUtils.makeDynamicReadOnly(databaseObject, new XmlSchemaElement());
eError.setName("error");
eError.setMinOccurs(0);
eError.setMaxOccurs(1);
eError.setSchemaTypeName(errorType.getQName());
SchemaMeta.getReferencedDatabaseObjects(errorType).add(sequence);
xmlChoice.getItems().add(xmlSeq);
xmlChoice.getItems().add(eError);
}
// add child particles
if (!particleChildren.isEmpty()) {
for (XmlSchemaParticle child : particleChildren) {
xmlSeq.getItems().add(child);
}
}
particleChildren.clear();
// add child attributes
for (XmlSchemaAttribute attribute : attributeChildren) {
cType.getAttributes().add(attribute);
}
attributeChildren.clear();
// add the 'statistics' element
if (sequence.getAddStatistics()) {
XmlSchemaType statisticsType = schema.getTypeByName("ConvertigoStatsType");
XmlSchemaElement eStatistics = XmlSchemaUtils.makeDynamicReadOnly(databaseObject, new XmlSchemaElement());
eStatistics.setName("statistics");
eStatistics.setMinOccurs(0);
eStatistics.setMaxOccurs(1);
eStatistics.setSchemaTypeName(statisticsType.getQName());
xmlSeq.getItems().add(eStatistics);
SchemaMeta.getReferencedDatabaseObjects(statisticsType).add(sequence);
}
// --------------------------- For Further Use -------------------------------------------------//
// Modify schema to avoid 'cosamb' (same tagname&type in different groupBase at same level)
// TODO : IfThenElse steps must be modified for xsd:sequence instead of xsd:choice
// TODO : Then/Else steps must be modified to add minOccurs=0 on xsd:sequence
// TODO : review/improve cosnoamb(XmlSchema, XmlSchemaGroupBase, XmlSchemaGroupBase) method
// ---------------------------------------------------------------------------------------------//
} else // Step case
if (databaseObject instanceof Step) {
Step step = (Step) databaseObject;
if (!step.isEnabled()) {
// stop walking for disabled steps
return;
}
List<XmlSchemaParticle> parentParticleChildren = particleChildren;
List<XmlSchemaAttribute> parentAttributeChildren = attributeChildren;
// System.out.println("step:"+ step.toString());
if (step instanceof TransactionStep) {
// System.out.println("SCHEMA TARGET STEP "+ step.toString() + "(" + step.hashCode() + ")");
}
if (step.isGenerateSchema() || (fullSchema && step.isXmlOrOutput())) {
// System.out.println("-> generate schema...");
List<XmlSchemaParticle> myParticleChildren = null;
List<XmlSchemaAttribute> myAttributeChildren = null;
// is base affected ?
@SuppressWarnings("unused") XmlSchemaType base = null;
QName baseQName = step instanceof ISimpleTypeAffectation ? ((ISimpleTypeAffectation) step).getSimpleTypeAffectation() : null;
if (baseQName != null && baseQName.getLocalPart().length() > 0) {
// base = baseQName.getNamespaceURI().length() == 0 ? schema.getTypeByName(baseQName.getLocalPart()) : collection.getTypeByQName(baseQName);
base = XmlSchemaBuilder.this.resolveTypeByQName(baseQName);
}
// is type affected ?
XmlSchemaType type = null;
QName typeQName = step instanceof IComplexTypeAffectation ? ((IComplexTypeAffectation) step).getComplexTypeAffectation() : null;
if (typeQName != null && typeQName.getLocalPart().length() > 0) {
// type = typeQName.getNamespaceURI().length() == 0 ? schema.getTypeByName(typeQName.getLocalPart()) : collection.getTypeByQName(typeQName);
type = XmlSchemaBuilder.this.resolveTypeByQName(typeQName);
}
// is element affected ?
XmlSchemaElement ref = null;
QName refQName = step instanceof IElementRefAffectation ? ((IElementRefAffectation) step).getElementRefAffectation() : null;
if (refQName != null && refQName.getLocalPart().length() > 0) {
// ref = refQName.getNamespaceURI().length() == 0 ? schema.getElementByName(refQName.getLocalPart()) : collection.getElementByQName(refQName);
ref = XmlSchemaBuilder.this.resolveElementByQName(refQName);
typeQName = new QName(schema.getTargetNamespace(), refQName.getLocalPart() + "Type");
if (ref == null && refQName.getNamespaceURI().equals(schema.getTargetNamespace())) {
ref = XmlSchemaUtils.makeDynamic(step, new XmlSchemaElement());
ref.setQName(refQName);
ref.setName(refQName.getLocalPart());
ref.setSchemaTypeName(baseQName);
XmlSchemaUtils.add(schema, ref);
} else if (ref != null) {
ref.setSchemaTypeName(baseQName);
// type = typeQName.getNamespaceURI().length() == 0 ? schema.getTypeByName(typeQName.getLocalPart()) : collection.getTypeByQName(typeQName);
type = XmlSchemaBuilder.this.resolveTypeByQName(typeQName);
}
}
if (type == null || !SchemaMeta.isReadOnly(type)) {
// prepare to receive children
if (step instanceof ISchemaParticleGenerator) {
myParticleChildren = particleChildren = new LinkedList<XmlSchemaParticle>();
if (fullSchema || ((ISchemaParticleGenerator) step).isGenerateElement()) {
myAttributeChildren = attributeChildren = new LinkedList<XmlSchemaAttribute>();
}
}
// deep walk
super.walk(step);
// generate itself and add to the caller list
if (step instanceof ISchemaAttributeGenerator) {
// Attribute case
XmlSchemaAttribute attribute = ((ISchemaAttributeGenerator) step).getXmlSchemaObject(collection, schema);
SchemaMeta.setXmlSchemaObject(schema, step, attribute);
parentAttributeChildren.add(attribute);
} else if (step instanceof ISchemaParticleGenerator) {
if (step instanceof RequestableStep) {
RequestableStep requestableStep = (RequestableStep) step;
String targetProjectName = requestableStep.getProjectName();
Project targetProject = requestableStep.getSequence().getLoadedProject(targetProjectName);
if (targetProject == null) {
Engine.logEngine.warn("(XmlSchemaBuilder) Not complete schema because: Missing required or not loaded project \"" + targetProjectName + "\"");
} else if (step instanceof SequenceStep) {
// SequenceStep case : walk target sequence first
try {
Sequence targetSequence = ((SequenceStep) step).getTargetSequence();
targetProjectName = targetSequence.getProject().getName();
String targetSequenceName = targetSequence.getName();
String stepSequenceName = step.getSequence().getName();
if (projectName.equals(targetProjectName)) {
boolean isAfter = targetSequenceName.compareToIgnoreCase(stepSequenceName) > 0;
if (isAfter) {
walk(targetSequence);
}
}
} catch (EngineException e) {
if (!e.getMessage().startsWith("There is no ")) {
throw e;
} else {
Engine.logEngine.warn("(XmlSchemaBuilder) Not complete schema because: " + e.getMessage());
}
}
}
}
// Particle case
XmlSchemaParticle particle = ((ISchemaParticleGenerator) step).getXmlSchemaObject(collection, schema);
SchemaMeta.setXmlSchemaObject(schema, step, particle);
parentParticleChildren.add(particle);
// retrieve the xsd:element to add children
XmlSchemaElement element = SchemaMeta.getContainerXmlSchemaElement(ref == null ? particle : ref);
// retrieve the group to add children if any
XmlSchemaGroupBase group = SchemaMeta.getContainerXmlSchemaGroupBase(element != null ? element : particle);
// new complexType to enhance the element
XmlSchemaComplexType cType = element != null ? (XmlSchemaComplexType) element.getSchemaType() : null;
// do something only on case of child
if (!myParticleChildren.isEmpty() || (myAttributeChildren != null && !myAttributeChildren.isEmpty())) {
if (cType == null) {
cType = XmlSchemaUtils.makeDynamic(step, new XmlSchemaComplexType(schema));
}
// prepare element children in the group
if (!myParticleChildren.isEmpty()) {
if (group == null) {
group = XmlSchemaUtils.makeDynamic(step, new XmlSchemaSequence());
}
for (XmlSchemaParticle child : myParticleChildren) {
group.getItems().add(child);
}
}
if (element != null) {
XmlSchemaSimpleContentExtension sContentExt = SchemaManager.makeSimpleContentExtension(step, element, cType);
if (sContentExt != null) {
// add attributes
for (XmlSchemaAttribute attribute : myAttributeChildren) {
sContentExt.getAttributes().add(attribute);
}
} else {
// add attributes
for (XmlSchemaAttribute attribute : myAttributeChildren) {
cType.getAttributes().add(attribute);
}
// add elements
if (SchemaMeta.isDynamic(cType) && group != null) {
cType.setParticle(group);
}
}
}
}
if (element != null) {
// check if the type is named
if (typeQName != null && typeQName.getLocalPart().length() > 0) {
if (cType == null) {
cType = XmlSchemaUtils.makeDynamic(step, new XmlSchemaComplexType(schema));
SchemaManager.makeSimpleContentExtension(step, element, cType);
}
if (type == null) {
// the type doesn't exist, declare it
cType.setName(typeQName.getLocalPart());
schema.addType(cType);
schema.getItems().add(cType);
} else {
// the type already exists, merge it
XmlSchemaComplexType currentCType = (XmlSchemaComplexType) type;
SchemaManager.merge(schema, currentCType, cType);
cType = currentCType;
}
// reference the type in the current element
element.setSchemaTypeName(cType.getQName());
element.setSchemaType(null);
} else if (cType != null && SchemaMeta.isDynamic(cType) && element.getSchemaTypeName() == null) {
// the element contains an anonymous type
element.setSchemaType(cType);
}
}
} else {
XmlSchemaObject object;
XmlSchema xmlSchema = null;
if (step instanceof XMLCopyStep && !fullSchema) {
final XmlSchemaBuilder fullBuilder = builderExecutor.getBuilder(projectName, true);
if (fullBuilder != null) {
xmlSchema = fullBuilder.getXmlSchema();
XmlSchemaCollection xmlCollection = SchemaMeta.getCollection(xmlSchema);
object = step.getXmlSchemaObject(xmlCollection, xmlSchema);
} else {
xmlSchema = schema;
object = step.getXmlSchemaObject(collection, schema);
SchemaMeta.setXmlSchemaObject(schema, step, object);
}
} else {
xmlSchema = schema;
object = step.getXmlSchemaObject(collection, schema);
SchemaMeta.setXmlSchemaObject(schema, step, object);
}
if (step instanceof XMLCopyStep) {
if (object instanceof XmlSchemaElement) {
XmlSchemaElement xmlSchemaElement = (XmlSchemaElement) object;
QName qname = xmlSchemaElement.getSchemaTypeName();
if (qname != null) {
XmlSchemaType xmlSchemaType = xmlSchema.getTypeByName(qname);
if (xmlSchemaType != null) {
SchemaMeta.getReferencedDatabaseObjects(xmlSchemaType).add(step);
}
}
}
}
if (object instanceof XmlSchemaParticle) {
particleChildren.add((XmlSchemaParticle) object);
} else if (object instanceof XmlSchemaAttribute) {
attributeChildren.add((XmlSchemaAttribute) object);
}
}
} else {
// re-use read only type
XmlSchemaElement elt = XmlSchemaUtils.makeDynamic(step, new XmlSchemaElement());
SchemaMeta.getReferencedDatabaseObjects(type).add(step);
SchemaMeta.setXmlSchemaObject(schema, step, elt);
elt.setName(step.getStepNodeName());
elt.setSchemaTypeName(typeQName);
particleChildren.add(elt);
}
} else // Other case
{
// doesn't generate schema, just deep walk
// System.out.println("-> do not generate schema (deep walk)");
super.walk(step);
}
// restore lists for siblings
particleChildren = parentParticleChildren;
attributeChildren = parentAttributeChildren;
} else {
// just deep walk
super.walk(databaseObject);
}
}
@Override
protected boolean before(DatabaseObject databaseObject, Class<? extends DatabaseObject> dboClass) {
// just walk ISchemaGenerator DBO
return Step.class.isAssignableFrom(dboClass) || Sequence.class.isAssignableFrom(dboClass) || Transaction.class.isAssignableFrom(dboClass) || Connector.class.isAssignableFrom(dboClass);
}
}.init(project);
} catch (Exception e) {
throw new EngineException("midBuildSchema failed", e);
}
}
use of org.apache.ws.commons.schema.XmlSchemaComplexType in project convertigo by convertigo.
the class JsonSchemaUtils method getOasSchema.
protected static JSONObject getOasSchema(XmlSchemaCollection xmlSchemaCollection, XmlSchema xmlSchema, String oasDirUrl, boolean isOas2) {
final NamespaceMap nsMap = (NamespaceMap) xmlSchemaCollection.getNamespaceContext();
final JSONObject jsonSchema = new JSONObject();
try {
String prefix = nsMap.getPrefix(xmlSchema.getTargetNamespace());
jsonSchema.put("id", oasDirUrl + prefix + ".jsonschema#");
jsonSchema.put("ns", xmlSchema.getTargetNamespace());
jsonSchema.put("definitions", new JSONObject());
new XmlSchemaWalker(false, false) {
final Map<String, JSONObject> refs = new HashMap<String, JSONObject>(50);
final JSONObject definitions = jsonSchema.getJSONObject("definitions");
JSONObject parent = definitions;
JSONObject root = null;
private boolean isGlobal(JSONObject jParent) {
if (jParent != null) {
return jParent.equals(definitions);
}
return false;
}
private JSONObject getRefObject(String ref) throws JSONException {
JSONObject refObject = refs.get(ref);
if (refObject != null) {
if (!refObject.has("value") && !refObject.equals(root)) {
handle(refObject);
}
if (refObject.has("value")) {
JSONObject value = refObject.getJSONObject("value");
if (value.has("type") && !value.getString("type").equals("object")) {
JSONObject ob = new JSONObject();
copyOKeys(value, ob);
return ob;
}
}
}
return new JSONObject().put("$ref", ref);
}
private String normalize(String key) {
return key.replaceAll("[^a-zA-Z0-9]", "x");
}
private void addGlobalObject(JSONObject jParent, JSONObject jElement, String key) throws JSONException {
if (jParent != null) {
jParent.put(normalize(key), jElement);
refs.put(jElement.getString("objKey"), jElement);
}
}
private String toOasType(String baseType) {
String oasType = baseType;
switch(baseType) {
case "NMTOKEN":
case "token":
case "IDREF":
case "ID":
oasType = "string";
break;
case "dateTime":
oasType = "string";
break;
case "decimal":
oasType = "number";
break;
case "byte":
case "int":
case "short":
case "nonNegativeInteger":
case "positiveInteger":
oasType = "integer";
break;
}
return oasType;
}
private String getDefinitionRef(QName rname) {
if (rname != null) {
String local = rname.getLocalPart();
String ns = rname.getNamespaceURI();
return oasDirUrl + nsMap.getPrefix(ns) + ".jsonschema#/definitions/" + normalize(local);
}
return null;
}
private void addChild(JSONObject jParent, JSONObject jElement) throws JSONException {
if (!jParent.has("children")) {
jParent.put("children", new JSONArray());
}
jParent.getJSONArray("children").put(jElement);
}
private void handleElement(JSONObject jsonOb, JSONObject jsonChild, long minItems, long maxItems) throws JSONException {
if (!jsonOb.has("type")) {
jsonOb.put("type", "object");
}
if (!jsonOb.has("required")) {
jsonOb.put("required", new JSONArray());
}
if (!jsonOb.has("properties")) {
jsonOb.put("properties", new JSONObject());
}
JSONArray required = jsonOb.getJSONArray("required");
JSONObject properties = jsonOb.getJSONObject("properties");
String propertyName = jsonChild.getString("name");
long minOccurs = jsonChild.has("minOccurs") ? jsonChild.getLong("minOccurs") : 0;
long maxOccurs = jsonChild.has("maxOccurs") ? jsonChild.getLong("maxOccurs") : minOccurs;
boolean isRequired = Math.min(minOccurs, minItems) > 0;
boolean isArray = maxOccurs > 1 || maxItems > 1;
JSONObject property = new JSONObject();
// fill required
if (isRequired) {
if (required.join(",").indexOf(propertyName) == -1) {
required.put(propertyName);
}
} else if (!isArray) {
property.put("nullable", true);
}
// fill properties
if (isArray) {
property.put("type", "array");
property.put("minItems", Math.min(minOccurs, minItems));
property.put("maxItems", Math.max(maxOccurs, maxItems));
property.put("items", new JSONObject());
}
JSONObject prop = isArray ? property.getJSONObject("items") : property;
if (jsonChild.has("value")) {
JSONObject value = jsonChild.getJSONObject("value");
@SuppressWarnings("unchecked") Iterator<String> it = value.keys();
while (it.hasNext()) {
String pkey = it.next();
if (!pkey.isEmpty()) {
prop.put(pkey, value.get(pkey));
}
}
} else if (jsonChild.has("children")) {
JSONArray children = jsonChild.getJSONArray("children");
for (int i = 0; i < children.length(); i++) {
JSONObject child = children.getJSONObject(i);
if (!child.has("objType")) {
@SuppressWarnings("unchecked") Iterator<String> it = child.keys();
while (it.hasNext()) {
String pkey = it.next();
if (!pkey.isEmpty()) {
prop.put(pkey, child.get(pkey));
}
}
}
}
}
if (!isRequired && prop.has("$ref")) {
JSONArray allOf = new JSONArray().put(new JSONObject().put("$ref", prop.remove("$ref")));
prop.put("nullable", true).put("allOf", allOf);
}
properties.put(propertyName, property);
// handle children
if (jsonChild.has("children")) {
JSONArray children = jsonChild.getJSONArray("children");
for (int i = 0; i < children.length(); i++) {
JSONObject child = children.getJSONObject(i);
if (child.has("objType")) {
handleChild(prop.has("type") || prop.has("$ref") ? jsonOb : prop, child);
}
}
}
}
private void handleAttribute(JSONObject jsonOb, JSONObject jsonChild, long minItems, long maxItems) throws JSONException {
if (!jsonOb.has("required")) {
jsonOb.put("required", new JSONArray());
}
if (!jsonOb.has("properties")) {
jsonOb.put("properties", new JSONObject());
}
JSONObject attrObject = null;
if (jsonOb.getJSONObject("properties").has("attr")) {
attrObject = jsonOb.getJSONObject("properties").getJSONObject("attr");
} else {
attrObject = new JSONObject().put("type", "object").put("required", new JSONArray()).put("properties", new JSONObject());
jsonOb.getJSONObject("properties").put("attr", attrObject);
}
JSONArray required = attrObject.getJSONArray("required");
JSONObject properties = attrObject.getJSONObject("properties");
String attrName = jsonChild.getString("name");
long minOccurs = jsonChild.has("minOccurs") ? jsonChild.getLong("minOccurs") : 0;
long maxOccurs = jsonChild.has("maxOccurs") ? jsonChild.getLong("maxOccurs") : minOccurs;
boolean isRequired = minOccurs > 0;
boolean isArray = maxOccurs > 1;
// fill required
if (isRequired) {
if (required.join(",").indexOf(attrName) == -1) {
required.put(attrName);
// if attribute is required then attr object is required also
if (jsonOb.getJSONArray("required").join(",").indexOf("attr") == -1) {
jsonOb.getJSONArray("required").put("attr");
}
}
}
// fill properties
JSONObject attribute = new JSONObject();
if (isArray) {
attribute.put("type", "array");
attribute.put("minItems", minOccurs);
attribute.put("maxItems", maxOccurs);
attribute.put("items", new JSONObject());
}
JSONObject attr = isArray ? attribute.getJSONObject("items") : attribute;
if (jsonChild.has("value")) {
JSONObject value = jsonChild.getJSONObject("value");
copyOKeys(value, attr);
} else if (jsonChild.has("children")) {
JSONArray children = jsonChild.getJSONArray("children");
for (int i = 0; i < children.length(); i++) {
JSONObject child = children.getJSONObject(i);
if (!child.has("objType")) {
copyOKeys(child, attr);
}
}
}
properties.put(attrName, attribute);
// handle children
if (jsonChild.has("children")) {
JSONArray children = jsonChild.getJSONArray("children");
for (int i = 0; i < children.length(); i++) {
JSONObject child = children.getJSONObject(i);
if (child.has("objType")) {
handleChild(attr, child);
}
}
}
}
private void handleChoice(JSONObject jsonOb, JSONObject jsonChild, long minItems, long maxItems) throws JSONException {
// note: oneOf with complex inline model is baddly supported: minTems forced to 0L
long minOccurs = jsonChild.has("minOccurs") ? jsonChild.getLong("minOccurs") : 0;
long maxOccurs = jsonChild.has("maxOccurs") ? jsonChild.getLong("maxOccurs") : minOccurs;
// handle children
if (jsonChild.has("children")) {
JSONArray children = jsonChild.getJSONArray("children");
for (int i = 0; i < children.length(); i++) {
JSONObject child = children.getJSONObject(i);
// minTems forced to 0L
handleChild(jsonOb, child, 0L, Math.max(maxOccurs, maxItems));
}
}
}
private void handleUnion(JSONObject jsonOb, JSONObject jsonChild, long minItems, long maxItems) throws JSONException {
if (jsonChild.has("children")) {
JSONArray children = jsonChild.getJSONArray("children");
for (int i = 0; i < children.length(); i++) {
JSONObject child = children.getJSONObject(i);
handleChild(jsonOb, child, minItems, maxItems);
}
}
}
private void handleExtension(JSONObject jsonOb, JSONObject jsonChild, long minItems, long maxItems) throws JSONException {
boolean hasAllOf = jsonOb.has("allOf");
if (!hasAllOf) {
jsonOb.put("allOf", new JSONArray());
}
JSONArray allOf = jsonOb.getJSONArray("allOf");
// handle children
if (jsonChild.has("children")) {
JSONArray children = jsonChild.getJSONArray("children");
for (int i = 0; i < children.length(); i++) {
JSONObject child = children.getJSONObject(i);
JSONObject ob = new JSONObject();
handleChild(ob, child);
merge(ob);
allOf.put(ob);
}
}
merge(jsonOb);
}
private void handleSequence(JSONObject jsonOb, JSONObject jsonChild, long minItems, long maxItems) throws JSONException {
long minOccurs = jsonChild.has("minOccurs") ? jsonChild.getLong("minOccurs") : 0;
long maxOccurs = jsonChild.has("maxOccurs") ? jsonChild.getLong("maxOccurs") : minOccurs;
boolean hasAllOf = jsonOb.has("allOf");
if (!hasAllOf) {
jsonOb.put("allOf", new JSONArray());
}
JSONArray allOf = jsonOb.getJSONArray("allOf");
// handle children
if (jsonChild.has("children")) {
JSONArray children = jsonChild.getJSONArray("children");
for (int i = 0; i < children.length(); i++) {
JSONObject child = children.getJSONObject(i);
JSONObject ob = new JSONObject();
handleChild(ob, child, Math.min(minOccurs, minItems), Math.max(maxOccurs, maxItems));
merge(ob);
allOf.put(ob);
}
merge(jsonOb);
}
}
private void merge(JSONObject jsonOb) throws JSONException {
if (jsonOb.has("allOf")) {
JSONArray allOf = jsonOb.getJSONArray("allOf");
int len = allOf.length();
if (len > 0) {
JSONObject merge = new JSONObject().put("type", "object").put("required", new JSONArray()).put("properties", new JSONObject());
List<JSONObject> merged = new ArrayList<JSONObject>();
List<JSONObject> others = new ArrayList<JSONObject>();
for (int i = 0; i < len; i++) {
JSONObject ob = allOf.getJSONObject(i);
if (ob.has("description") && ob.length() == 1) {
merge.put("description", ob.getString("description"));
merged.add(ob);
}
if (ob.has("properties")) {
copyOKeys(ob.getJSONObject("properties"), merge.getJSONObject("properties"));
}
if (ob.has("required")) {
copyAKeys(ob.getJSONArray("required"), merge.getJSONArray("required"));
}
if (!ob.has("properties") && !ob.has("required")) {
others.add(ob);
} else {
merged.add(ob);
}
}
for (JSONObject ob : merged) {
allOf.remove(ob);
}
if (merged.size() > 0) {
allOf.put(merge);
}
// merge in parent
if (allOf.length() == 1) {
JSONObject ob = allOf.getJSONObject(0);
if (jsonOb.has("properties")) {
if (ob.has("description") && ob.length() == 1) {
jsonOb.put("description", ob.getString("description"));
}
if (ob.has("properties")) {
copyOKeys(ob.getJSONObject("properties"), jsonOb.getJSONObject("properties"));
}
if (ob.has("required")) {
copyAKeys(ob.getJSONArray("required"), jsonOb.getJSONArray("required"));
}
jsonOb.remove("allOf");
} else {
copyOKeys(ob, jsonOb);
jsonOb.remove("allOf");
}
} else {
;
}
}
}
}
private void handleChild(JSONObject jsonOb, JSONObject jsonChild) throws JSONException {
handleChild(jsonOb, jsonChild, 1L, 1L);
}
private void handleChild(JSONObject jsonOb, JSONObject jsonChild, long minItems, long maxItems) throws JSONException {
if (jsonChild.has("objType")) {
String objType = jsonChild.getString("objType");
switch(objType) {
case "elementType":
handleElement(jsonOb, jsonChild, minItems, maxItems);
break;
case "attributeType":
handleAttribute(jsonOb, jsonChild, minItems, maxItems);
break;
case "choiceType":
handleChoice(jsonOb, jsonChild, minItems, maxItems);
break;
case "sequenceType":
handleSequence(jsonOb, jsonChild, minItems, maxItems);
break;
case "simpleUnionType":
handleUnion(jsonOb, jsonChild, minItems, maxItems);
break;
case "complexContentExtensionType":
handleExtension(jsonOb, jsonChild, minItems, maxItems);
break;
case "allType":
case "groupType":
case "complexType":
case "simpleRestrictionType":
case "simpleType":
if (jsonChild.has("children")) {
JSONArray children = jsonChild.getJSONArray("children");
for (int i = 0; i < children.length(); i++) {
handleChild(jsonOb, (JSONObject) children.get(i), minItems, maxItems);
}
} else {
System.out.println("ObjType: " + objType + " has no children");
}
break;
default:
System.out.println("Unhandled objType: " + objType);
break;
}
} else {
copyOKeys(jsonChild, jsonOb);
}
// remove pattern for integer (not supported)
if (jsonOb.has("type") && jsonOb.has("pattern")) {
if (!jsonOb.getString("type").equals("string")) {
jsonOb.remove("pattern");
}
}
}
private void copyOKeys(JSONObject from, JSONObject to) throws JSONException {
@SuppressWarnings("unchecked") Iterator<String> it = from.keys();
while (it.hasNext()) {
String pkey = it.next();
if (!pkey.isEmpty()) {
to.put(pkey, from.get(pkey));
}
}
}
private void copyAKeys(JSONArray from, JSONArray to) throws JSONException {
for (int i = 0; i < from.length(); i++) {
to.put(from.get(i));
}
}
private void handleRefs(Object ob) {
try {
if (ob instanceof JSONObject) {
JSONObject jsonOb = (JSONObject) ob;
if (jsonOb.has("$ref")) {
String ref = jsonOb.getString("$ref");
JSONObject refObject = getRefObject(ref);
if (!refObject.has("$ref")) {
jsonOb.remove("$ref");
copyOKeys(refObject, jsonOb);
}
}
@SuppressWarnings("unchecked") Iterator<String> it = jsonOb.keys();
while (it.hasNext()) {
String pkey = it.next();
handleRefs(jsonOb.get(pkey));
}
if (jsonOb.has("allOf")) {
merge(jsonOb);
}
} else if (ob instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) ob;
for (int i = 0; i < jsonArray.length(); i++) {
handleRefs(jsonArray.get(i));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void handle(JSONObject jParent) {
try {
if (jParent != null) {
if (!jParent.has("value")) {
JSONObject value = new JSONObject();
jParent.put("value", value);
JSONObject jsonOb = jParent.getJSONObject("value");
if (jParent.has("children")) {
JSONArray children = jParent.getJSONArray("children");
int len = children.length();
for (int i = 0; i < len; i++) {
handleChild(jsonOb, (JSONObject) children.get(i));
merge(jsonOb);
}
} else {
//
}
}
}
} catch (Exception e) {
e.printStackTrace();
Engine.logEngine.warn("(JSonSchemaUtils) Unexpected exception while generating jsonchema models", e);
}
}
@Override
protected void walkChoice(XmlSchema xmlSchema, XmlSchemaChoice obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
JSONObject jElement = new JSONObject();
try {
jElement.put("objType", "choiceType").put("minOccurs", obj.getMinOccurs()).put("maxOccurs", obj.getMaxOccurs());
addChild(jParent, jElement);
parent = jElement;
} catch (JSONException e) {
e.printStackTrace();
}
super.walkChoice(xmlSchema, obj);
parent = jParent;
root = jRoot;
}
@Override
protected void walkGroup(XmlSchema xmlSchema, XmlSchemaGroup obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
QName qname = obj.getName();
JSONObject jElement = new JSONObject();
try {
if (isGlobal(jParent)) {
if (qname != null) {
jElement.put("objType", "groupType");
String id = jsonSchema.getString("id");
jElement.put("objKey", id + "/definitions/" + normalize(qname.getLocalPart()));
jElement.put("QName", new JSONObject().put("localPart", qname.getLocalPart()).put("namespaceURI", qname.getNamespaceURI()));
addGlobalObject(jParent, jElement, qname.getLocalPart());
parent = jElement;
root = jElement;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
super.walkGroup(xmlSchema, obj);
if (isGlobal(jParent)) {
// handle(jElement);
}
parent = jParent;
root = jRoot;
}
@Override
protected void walkGroupRef(XmlSchema xmlSchema, XmlSchemaGroupRef obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
QName refName = obj.getRefName();
long minOccurs = obj.getMinOccurs();
long maxOccurs = obj.getMaxOccurs();
JSONObject jElement = new JSONObject();
try {
if (refName != null) {
String ref = getDefinitionRef(refName);
if (ref != null) {
JSONObject jRef = null;
if (maxOccurs > 1) {
if (ref.indexOf("xsd.jsonschema") != -1) {
jRef = new JSONObject().put("type", "array").put("items", new JSONObject().put("type", toOasType(refName.getLocalPart()))).put("minItems", minOccurs);
} else {
JSONObject refObject = getRefObject(ref);
jRef = new JSONObject().put("type", "array").put("items", refObject).put("minItems", minOccurs);
}
} else {
if (ref.indexOf("xsd.jsonschema") != -1) {
jRef = new JSONObject().put("type", toOasType(refName.getLocalPart()));
if (minOccurs == 0) {
jRef.put("nullable", "true");
}
} else {
JSONObject refObject = getRefObject(ref);
if (minOccurs == 0) {
jRef = new JSONObject().put("nullable", "true").put("allOf", new JSONArray().put(refObject));
} else {
jRef = refObject;
}
}
}
jElement.put("objType", "groupType").put("minOccurs", minOccurs).put("maxOccurs", maxOccurs).put("name", refName.getLocalPart());
addChild(jElement, jRef);
addChild(jParent, jElement);
parent = jElement;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
super.walkGroupRef(xmlSchema, obj);
parent = jParent;
root = jRoot;
}
@Override
protected void walkAll(XmlSchema xmlSchema, XmlSchemaAll obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
JSONObject jElement = new JSONObject();
try {
jElement.put("objType", "allType").put("minOccurs", obj.getMinOccurs()).put("maxOccurs", obj.getMaxOccurs());
addChild(jParent, jElement);
parent = jElement;
} catch (JSONException e) {
e.printStackTrace();
}
super.walkAll(xmlSchema, obj);
parent = jParent;
root = jRoot;
}
@Override
protected void walkSequence(XmlSchema xmlSchema, XmlSchemaSequence obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
JSONObject jElement = new JSONObject();
try {
jElement.put("objType", "sequenceType").put("minOccurs", obj.getMinOccurs()).put("maxOccurs", obj.getMaxOccurs());
addChild(jParent, jElement);
parent = jElement;
} catch (JSONException e) {
e.printStackTrace();
}
super.walkSequence(xmlSchema, obj);
parent = jParent;
root = jRoot;
}
@Override
protected void walkElement(XmlSchema xmlSchema, XmlSchemaElement obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
String name = obj.getName();
QName qname = obj.getQName();
QName refName = obj.getRefName();
QName typeName = obj.getSchemaTypeName();
XmlSchemaType xmlSchemaType = obj.getSchemaType();
long minOccurs = obj.getMinOccurs();
long maxOccurs = obj.getMaxOccurs();
JSONObject jElement = new JSONObject();
try {
if (isGlobal(jParent)) {
jElement.put("objType", "elementType");
} else {
jElement.put("objType", "elementType").put("minOccurs", minOccurs).put("maxOccurs", maxOccurs);
}
if (refName == null && typeName == null) {
// pass though
} else {
QName rname = refName != null ? refName : (typeName != null ? typeName : (xmlSchemaType != null ? xmlSchemaType.getQName() : new QName("")));
String ref = getDefinitionRef(rname);
if (ref != null) {
if (name.isEmpty()) {
name = rname.getLocalPart();
}
if (ref.indexOf("xsd.jsonschema") != -1) {
addChild(jElement, new JSONObject().put("type", toOasType(rname.getLocalPart())));
} else {
JSONObject refObject = getRefObject(ref);
addChild(jElement, refObject);
}
}
}
jElement.put("name", name);
if (isGlobal(jParent)) {
if (qname != null) {
String id = jsonSchema.getString("id");
jElement.put("objKey", id + "/definitions/" + normalize(qname.getLocalPart()));
jElement.put("QName", new JSONObject().put("localPart", qname.getLocalPart()).put("namespaceURI", qname.getNamespaceURI()));
}
addGlobalObject(jParent, jElement, name);
parent = jElement;
root = jElement;
} else {
addChild(jParent, jElement);
parent = jElement;
}
} catch (JSONException e) {
e.printStackTrace();
}
super.walkElement(xmlSchema, obj);
if (isGlobal(jParent)) {
// handle(jElement);
}
parent = jParent;
root = jRoot;
}
@Override
protected void walkAny(XmlSchema xmlSchema, XmlSchemaAny obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
JSONObject jElement = new JSONObject();
try {
JSONObject value = new JSONObject().put("type", "string").put("description", "any element");
jElement.put("objType", "elementType").put("minOccurs", 1).put("maxOccurs", 1).put("name", "any").put("value", value);
jParent.put("additionalProperties", true);
addChild(jParent, jElement);
parent = jElement;
} catch (JSONException e) {
e.printStackTrace();
}
super.walkAny(xmlSchema, obj);
parent = jParent;
root = jRoot;
}
@Override
protected void walkAnyAttribute(XmlSchema xmlSchema, XmlSchemaAnyAttribute obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
JSONObject jElement = new JSONObject();
try {
JSONObject value = new JSONObject().put("type", "string").put("xml", new JSONObject().put("attribute", true)).put("description", "any attribute");
jElement.put("objType", "attributeType").put("minOccurs", 0).put("name", "any").put("value", value);
addChild(jParent, jElement);
parent = jElement;
} catch (JSONException e) {
e.printStackTrace();
}
super.walkAnyAttribute(xmlSchema, obj);
parent = jParent;
root = jRoot;
}
@Override
protected void walkAppInfo(XmlSchema xmlSchema, XmlSchemaAppInfo item) {
JSONObject jParent = parent;
JSONObject jRoot = root;
try {
String description = "";
NodeList nodeList = item.getMarkup();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.TEXT_NODE) {
description += node.getNodeValue();
}
}
if (!description.isEmpty()) {
addChild(jParent, new JSONObject().put("title", description));
}
} catch (JSONException e) {
e.printStackTrace();
}
super.walkAppInfo(xmlSchema, item);
parent = jParent;
root = jRoot;
}
@Override
protected void walkDocumentation(XmlSchema xmlSchema, XmlSchemaDocumentation item) {
JSONObject jParent = parent;
JSONObject jRoot = root;
try {
String description = "";
NodeList nodeList = item.getMarkup();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.TEXT_NODE) {
description += node.getNodeValue();
}
}
if (!description.isEmpty()) {
addChild(jParent, new JSONObject().put("description", description));
}
} catch (JSONException e) {
e.printStackTrace();
}
super.walkDocumentation(xmlSchema, item);
parent = jParent;
root = jRoot;
}
@Override
protected void walkAttribute(XmlSchema xmlSchema, XmlSchemaAttribute obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
String name = obj.getName();
QName qname = obj.getQName();
QName refName = obj.getRefName();
QName typeName = obj.getSchemaTypeName();
XmlSchemaSimpleType xmlSchemaSimpleType = obj.getSchemaType();
boolean isRequired = obj.getUse().equals(XmlSchemaUtils.attributeUseRequired);
JSONObject jElement = new JSONObject();
try {
jElement.put("objType", "attributeType").put("minOccurs", isRequired ? 1 : 0);
if (refName == null && typeName == null) {
// pass through
} else {
QName rname = refName != null ? refName : (typeName != null ? typeName : (xmlSchemaSimpleType != null ? xmlSchemaSimpleType.getQName() : new QName("")));
String ref = getDefinitionRef(rname);
if (ref != null) {
if (name.isEmpty()) {
name = rname.getLocalPart();
}
JSONObject value = new JSONObject();
if (ref.indexOf("xsd.jsonschema") != -1) {
value.put("type", toOasType(rname.getLocalPart()));
value.put("xml", new JSONObject().put("attribute", true));
} else {
JSONObject refObject = getRefObject(ref);
copyOKeys(refObject, value);
value.put("xml", new JSONObject().put("attribute", true));
}
addChild(jElement, value);
}
}
jElement.put("name", name);
if (isGlobal(jParent)) {
if (qname != null) {
String id = jsonSchema.getString("id");
jElement.put("objKey", id + "/definitions/" + normalize(qname.getLocalPart()));
jElement.put("QName", new JSONObject().put("localPart", qname.getLocalPart()).put("namespaceURI", qname.getNamespaceURI()));
}
addGlobalObject(jParent, jElement, name);
parent = jElement;
root = jElement;
} else {
addChild(jParent, jElement);
parent = jElement;
}
} catch (JSONException e) {
e.printStackTrace();
}
super.walkAttribute(xmlSchema, obj);
if (isGlobal(jParent)) {
// handle(jElement);
}
parent = jParent;
root = jRoot;
}
@Override
protected void walkAttributeGroup(XmlSchema xmlSchema, XmlSchemaAttributeGroup obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
QName qname = obj.getName();
JSONObject jElement = new JSONObject();
try {
if (isGlobal(jParent)) {
if (qname != null) {
jElement.put("objType", "attributeGroupType");
String id = jsonSchema.getString("id");
jElement.put("objKey", id + "/definitions/" + normalize(qname.getLocalPart()));
jElement.put("QName", new JSONObject().put("localPart", qname.getLocalPart()).put("namespaceURI", qname.getNamespaceURI()));
addGlobalObject(jParent, jElement, qname.getLocalPart());
parent = jElement;
root = jElement;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
super.walkAttributeGroup(xmlSchema, obj);
if (isGlobal(jParent)) {
// handle(jElement);
}
parent = jParent;
root = jRoot;
}
@Override
protected void walkAttributeGroupRef(XmlSchema xmlSchema, XmlSchemaAttributeGroupRef obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
QName refName = obj.getRefName();
JSONObject jElement = new JSONObject();
try {
if (refName != null) {
String ref = getDefinitionRef(refName);
if (ref != null) {
jElement.put("objType", "attributeGroupType").put("name", refName.getLocalPart());
addChild(jElement, new JSONObject().put("$ref", ref));
addChild(jParent, jElement);
parent = jElement;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
super.walkAttributeGroupRef(xmlSchema, obj);
parent = jParent;
root = jRoot;
}
@Override
protected void walkSimpleContent(XmlSchema xmlSchema, XmlSchemaSimpleContent obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
QName qname = null;
XmlSchemaContent xmlSchemaContent = obj.getContent();
if (xmlSchemaContent instanceof XmlSchemaSimpleContentRestriction) {
qname = ((XmlSchemaSimpleContentRestriction) xmlSchemaContent).getBaseTypeName();
} else if (xmlSchemaContent instanceof XmlSchemaSimpleContentExtension) {
qname = ((XmlSchemaSimpleContentExtension) xmlSchemaContent).getBaseTypeName();
}
JSONObject jElement = new JSONObject();
try {
if (qname != null) {
String ref = getDefinitionRef(qname);
if (ref != null) {
if (ref.indexOf("xsd.jsonschema") != -1) {
jElement.put("objType", "elementType").put("minOccurs", 1).put("maxOccurs", 1).put("name", "text").put("value", new JSONObject().put("type", toOasType(qname.getLocalPart())));
} else {
JSONObject refObject = getRefObject(ref);
jElement.put("objType", "elementType").put("minOccurs", 1).put("maxOccurs", 1).put("name", "text").put("value", refObject);
}
addChild(jParent, jElement);
parent = jElement;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
super.walkSimpleContent(xmlSchema, obj);
parent = jParent;
root = jRoot;
}
@Override
protected void walkSimpleType(XmlSchema xmlSchema, XmlSchemaSimpleType obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
QName qname = obj.getQName();
QName bname = obj.getBaseSchemaTypeName();
JSONObject jElement = new JSONObject();
try {
jElement.put("objType", "simpleType");
if (bname != null) {
String ref = getDefinitionRef(bname);
if (ref != null) {
if (ref.indexOf("xsd.jsonschema") != -1) {
jElement.put("value", new JSONObject().put("type", toOasType(bname.getLocalPart())));
} else {
JSONObject refObject = getRefObject(ref);
jElement.put("value", refObject);
}
}
}
if (isGlobal(jParent)) {
if (qname != null) {
String id = jsonSchema.getString("id");
jElement.put("objKey", id + "/definitions/" + normalize(qname.getLocalPart()));
jElement.put("QName", new JSONObject().put("localPart", qname.getLocalPart()).put("namespaceURI", qname.getNamespaceURI()));
}
addGlobalObject(jParent, jElement, obj.getName());
parent = jElement;
root = jElement;
} else {
addChild(jParent, jElement);
parent = jElement;
}
} catch (JSONException e1) {
e1.printStackTrace();
}
super.walkSimpleType(xmlSchema, obj);
if (isGlobal(jParent)) {
// handle(jElement);
}
parent = jParent;
root = jRoot;
}
@Override
protected void walkSimpleTypeRestriction(XmlSchema xmlSchema, XmlSchemaSimpleTypeRestriction obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
QName qname = obj.getBaseTypeName();
JSONObject jElement = new JSONObject();
try {
if (qname != null) {
String ref = getDefinitionRef(qname);
if (ref != null) {
jElement.put("objType", "simpleRestrictionType");
if (ref.indexOf("xsd.jsonschema") != -1) {
addChild(jElement, new JSONObject().put("type", toOasType(qname.getLocalPart())));
} else {
JSONObject refObject = getRefObject(ref);
addChild(jElement, refObject);
}
addChild(jParent, jElement);
parent = jElement;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
super.walkSimpleTypeRestriction(xmlSchema, obj);
parent = jParent;
root = jRoot;
}
@Override
protected void walkSimpleTypeUnion(XmlSchema xmlSchema, XmlSchemaSimpleTypeUnion obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
JSONObject jElement = new JSONObject();
try {
jElement.put("objType", "simpleUnionType");
QName[] members = obj.getMemberTypesQNames();
if (members != null) {
for (QName qname : members) {
String ref = getDefinitionRef(qname);
if (ref.indexOf("xsd.jsonschema") != -1) {
addChild(jElement, new JSONObject().put("type", toOasType(qname.getLocalPart())));
} else {
parent = jElement;
walkByTypeName(xmlSchema, qname);
}
}
} else {
parent = jElement;
super.walkSimpleTypeUnion(xmlSchema, obj);
}
addChild(jParent, jElement);
} catch (JSONException e) {
e.printStackTrace();
}
parent = jParent;
root = jRoot;
}
@Override
protected void walkFacets(XmlSchema xmlSchema, XmlSchemaObjectCollection facets) {
JSONObject jParent = parent;
JSONObject jRoot = root;
JSONArray array = new JSONArray();
boolean arrayWithDuplicates = false;
for (int i = 0; i < facets.getCount(); i++) {
XmlSchemaFacet facet = (XmlSchemaFacet) facets.getItem(i);
Object value = facet.getValue();
try {
JSONObject jFacet = new JSONObject();
if (facet instanceof XmlSchemaEnumerationFacet) {
if ("".equals(value)) {
jFacet.put("minLength", 0);
array.put(value);
array.put(JSONObject.NULL);
} else {
if (array.join(",").toLowerCase().indexOf(String.valueOf(value).toLowerCase()) != -1) {
arrayWithDuplicates = true;
}
array.put(value);
}
if (i < facets.getCount() - 1) {
continue;
}
} else if (facet instanceof XmlSchemaPatternFacet) {
jFacet.put("pattern", value);
} else if (facet instanceof XmlSchemaLengthFacet) {
jFacet.put("length", Integer.valueOf(value.toString(), 10));
} else if (facet instanceof XmlSchemaMinLengthFacet) {
jFacet.put("minLength", Integer.valueOf(value.toString(), 10));
} else if (facet instanceof XmlSchemaMaxLengthFacet) {
jFacet.put("maxLength", Integer.valueOf(value.toString(), 10));
} else if (facet instanceof XmlSchemaMaxExclusiveFacet) {
jFacet.put("maximum", Integer.valueOf(value.toString(), 10)).put("exclusiveMaximum", true);
} else if (facet instanceof XmlSchemaMaxInclusiveFacet) {
jFacet.put("maximum", Integer.valueOf(value.toString(), 10)).put("exclusiveMaximum", false);
} else if (facet instanceof XmlSchemaMinExclusiveFacet) {
jFacet.put("minimum", Integer.valueOf(value.toString(), 10)).put("exclusiveMinimum", true);
} else if (facet instanceof XmlSchemaMinInclusiveFacet) {
jFacet.put("minimum", Integer.valueOf(value.toString(), 10)).put("exclusiveMinimum", false);
} else if (facet instanceof XmlSchemaTotalDigitsFacet) {
jFacet.put("maxLength", Integer.valueOf(value.toString(), 10));
}
if (facet instanceof XmlSchemaEnumerationFacet) {
if (array.length() > 0) {
jFacet.put("nullable", true);
if (facets.getCount() > 1) {
if (arrayWithDuplicates) {
jFacet.put("pattern", array.join("|"));
} else {
jFacet.put("enum", array);
}
}
}
}
if (jFacet.length() > 0) {
addChild(jParent, jFacet);
}
parent = jFacet;
} catch (JSONException e) {
e.printStackTrace();
}
}
parent = jParent;
root = jRoot;
}
@Override
protected void walkSimpleTypeList(XmlSchema xmlSchema, XmlSchemaSimpleTypeList obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
QName qname = obj.getItemTypeName();
JSONObject jElement = new JSONObject();
try {
if (qname != null) {
String ref = getDefinitionRef(qname);
if (ref != null) {
JSONObject value = new JSONObject();
if (ref.indexOf("xsd.jsonschema") != -1) {
value.put("type", "array").put("items", new JSONObject().put("type", toOasType(qname.getLocalPart())));
} else {
JSONObject refObject = getRefObject(ref);
value.put("type", "array").put("items", refObject);
}
jElement.put("objType", "simpleListType").put("value", value);
addChild(jParent, jElement);
parent = jElement;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
super.walkSimpleTypeList(xmlSchema, obj);
parent = jParent;
root = jRoot;
}
@Override
protected void walkComplexContentExtension(XmlSchema xmlSchema, XmlSchemaComplexContentExtension obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
QName baseTypeName = obj.getBaseTypeName();
JSONObject jElement = new JSONObject();
try {
if (baseTypeName != null) {
String ref = getDefinitionRef(baseTypeName);
if (ref != null) {
jElement.put("objType", "complexContentExtensionType");
addChild(jElement, new JSONObject().put("$ref", ref));
addChild(jParent, jElement);
parent = jElement;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
super.walkComplexContentExtension(xmlSchema, obj);
parent = jParent;
root = jRoot;
}
@Override
protected void walkComplexContentRestriction(XmlSchema xmlSchema, XmlSchemaComplexContentRestriction obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
QName baseTypeName = obj.getBaseTypeName();
JSONObject jElement = new JSONObject();
try {
if (baseTypeName != null) {
String ref = getDefinitionRef(baseTypeName);
if (ref != null) {
jElement.put("objType", "complexContentRestrictionType");
addChild(jElement, new JSONObject().put("$ref", ref));
addChild(jParent, jElement);
parent = jElement;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
super.walkComplexContentRestriction(xmlSchema, obj);
parent = jParent;
root = jRoot;
}
@Override
protected void walkComplexType(XmlSchema xmlSchema, XmlSchemaComplexType obj) {
JSONObject jParent = parent;
JSONObject jRoot = root;
QName qname = obj.getQName();
JSONObject jElement = new JSONObject();
try {
jElement.put("objType", "complexType");
if (obj.isMixed()) {
JSONObject jText = new JSONObject();
jText.put("objType", "elementType").put("name", "text").put("minOccurs", 0).put("maxOccurs", 1).put("value", new JSONObject().put("description", "the mixed content string").put("type", "string"));
addChild(jElement, jText);
}
if (isGlobal(jParent)) {
if (qname != null) {
String id = jsonSchema.getString("id");
jElement.put("objKey", id + "/definitions/" + normalize(qname.getLocalPart()));
jElement.put("QName", new JSONObject().put("localPart", qname.getLocalPart()).put("namespaceURI", qname.getNamespaceURI()));
}
addGlobalObject(jParent, jElement, obj.getName());
parent = jElement;
root = jElement;
} else {
addChild(jParent, jElement);
parent = jElement;
}
} catch (JSONException e) {
e.printStackTrace();
}
super.walkComplexType(xmlSchema, obj);
if (isGlobal(jParent)) {
// handle(jElement);
}
parent = jParent;
root = jRoot;
}
@Override
protected void walk(XmlSchema xmlSchema) {
XmlSchemaObjectCollection items = xmlSchema.getItems();
for (Iterator<XmlSchemaObject> i = GenericUtils.cast(items.getIterator()); i.hasNext(); ) {
XmlSchemaObject obj = i.next();
if (obj instanceof XmlSchemaInclude) {
walkInclude(xmlSchema, (XmlSchemaInclude) obj);
} else if (obj instanceof XmlSchemaImport) {
walkImport(xmlSchema, (XmlSchemaImport) obj);
}
}
// walk simple types first
for (Iterator<XmlSchemaType> i = GenericUtils.cast(xmlSchema.getSchemaTypes().getValues()); i.hasNext(); ) {
XmlSchemaObject obj = i.next();
if (obj instanceof XmlSchemaSimpleType) {
walk(xmlSchema, obj);
}
}
// walk others
for (Iterator<XmlSchemaType> i = GenericUtils.cast(xmlSchema.getSchemaTypes().getValues()); i.hasNext(); ) {
XmlSchemaObject obj = i.next();
if (!(obj instanceof XmlSchemaSimpleType)) {
walk(xmlSchema, obj);
}
}
for (Iterator<XmlSchemaObject> i = GenericUtils.cast(items.getIterator()); i.hasNext(); ) {
XmlSchemaObject obj = i.next();
if (!(obj instanceof XmlSchemaInclude || obj instanceof XmlSchemaImport || obj instanceof XmlSchemaType)) {
walk(xmlSchema, obj);
}
}
List<String> toRemove = new ArrayList<String>();
@SuppressWarnings("unchecked") Iterator<String> it = definitions.keys();
while (it.hasNext()) {
String pkey = it.next();
try {
JSONObject jsonOb = definitions.getJSONObject(pkey);
handle(jsonOb);
JSONObject value = jsonOb.getJSONObject("value");
if (value.has("properties") || value.has("allOf") || value.has("$ref")) {
jsonOb.put("type", "object");
handleRefs(value);
}
copyOKeys(value, jsonOb);
if (jsonOb.has("type")) {
if (!jsonOb.getString("type").equals("object")) {
toRemove.add(pkey);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
for (String pkey : toRemove) {
definitions.remove(pkey);
}
boolean debug = false;
if (!debug) {
try {
@SuppressWarnings("unchecked") Iterator<String> ita = definitions.keys();
while (ita.hasNext()) {
JSONObject jsonOb = definitions.getJSONObject(ita.next());
jsonOb.remove("objKey");
jsonOb.remove("objType");
jsonOb.remove("QName");
jsonOb.remove("children");
jsonOb.remove("name");
jsonOb.remove("maxOccurs");
jsonOb.remove("minOccurs");
jsonOb.remove("value");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
refs.clear();
}
}.walk(xmlSchema);
} catch (Exception e) {
e.printStackTrace();
}
return jsonSchema;
}
use of org.apache.ws.commons.schema.XmlSchemaComplexType in project ddf by codice.
the class FeatureMetacardType method processComplexType.
private void processComplexType(XmlSchemaElement xmlSchemaElement) {
if (!processGmlType(xmlSchemaElement)) {
XmlSchemaType schemaType = xmlSchemaElement.getSchemaType();
if (schemaType instanceof XmlSchemaComplexType) {
XmlSchemaComplexType complexType = (XmlSchemaComplexType) schemaType;
if (complexType.getParticle() != null) {
processXmlSchemaParticle(complexType.getParticle());
} else if (complexType.getContentModel() != null) {
XmlSchemaContent content = complexType.getContentModel().getContent();
if (content instanceof XmlSchemaComplexContentExtension) {
XmlSchemaComplexContentExtension extension = (XmlSchemaComplexContentExtension) content;
processXmlSchemaParticle(extension.getParticle());
} else if (content instanceof XmlSchemaSimpleContentExtension) {
final XmlSchemaSimpleContentExtension extension = (XmlSchemaSimpleContentExtension) content;
processSimpleContent(xmlSchemaElement, extension.getBaseTypeName());
} else if (content instanceof XmlSchemaSimpleContentRestriction) {
final XmlSchemaSimpleContentRestriction restriction = (XmlSchemaSimpleContentRestriction) content;
processSimpleContent(xmlSchemaElement, restriction.getBaseTypeName());
}
}
}
}
}
Aggregations