use of org.apache.ws.commons.schema.XmlSchema in project ddf by codice.
the class TestXmlSchemaMessageBodyReader method testReadFromServiceExceptionReport.
@Test
public void testReadFromServiceExceptionReport() throws WebApplicationException, IOException {
XmlSchemaMessageBodyReaderWfs10 reader = new XmlSchemaMessageBodyReaderWfs10();
XmlSchema schema = reader.readFrom(null, null, null, null, null, ser);
assertNull(schema);
}
use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.
the class AbstractDataBinding method addSchemaDocument.
public XmlSchema addSchemaDocument(ServiceInfo serviceInfo, SchemaCollection col, Document d, String systemId, Collection<String> ids) {
/*
* Sanity check. The document has to remotely resemble a schema.
*/
if (!XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(d.getDocumentElement().getNamespaceURI())) {
QName qn = DOMUtils.getElementQName(d.getDocumentElement());
throw new RuntimeException("Invalid schema document passed to " + "AbstractDataBinding.addSchemaDocument, " + "not in W3C schema namespace: " + qn);
}
if (!"schema".equals(d.getDocumentElement().getLocalName())) {
QName qn = DOMUtils.getElementQName(d.getDocumentElement());
throw new RuntimeException("Invalid schema document passed to " + "AbstractDataBinding.addSchemaDocument, " + "document element isn't 'schema': " + qn);
}
String ns = d.getDocumentElement().getAttribute("targetNamespace");
boolean copied = false;
if (StringUtils.isEmpty(ns)) {
if (DOMUtils.getFirstElement(d.getDocumentElement()) == null) {
hackAroundEmptyNamespaceIssue = true;
return null;
}
// create a copy of the dom so we
// can modify it.
d = copy(d);
copied = true;
ns = serviceInfo.getInterface().getName().getNamespaceURI();
d.getDocumentElement().setAttribute("targetNamespace", ns);
}
SchemaInfo schemaInfo = serviceInfo.getSchema(ns);
if (schemaInfo != null && (systemId == null && schemaInfo.getSystemId() == null || systemId != null && systemId.equalsIgnoreCase(schemaInfo.getSystemId()))) {
return schemaInfo.getSchema();
}
if (hackAroundEmptyNamespaceIssue) {
d = doEmptyNamespaceHack(d, copied);
}
Node n = d.getDocumentElement().getFirstChild();
boolean patchRequired = false;
while (n != null) {
if (n instanceof Element) {
Element e = (Element) n;
if (e.getLocalName().equals("import")) {
patchRequired = true;
break;
}
}
n = n.getNextSibling();
}
if (patchRequired) {
if (!copied) {
d = copy(d);
}
n = d.getDocumentElement().getFirstChild();
while (n != null) {
if (n instanceof Element) {
Element e = (Element) n;
if (e.getLocalName().equals("import")) {
e = (Element) n;
String loc = e.getAttribute("schemaLocation");
if (ids == null || ids.contains(loc)) {
e.removeAttribute("schemaLocation");
}
updateSchemaLocation(e);
if (StringUtils.isEmpty(e.getAttribute("namespace"))) {
e.setAttribute("namespace", serviceInfo.getInterface().getName().getNamespaceURI());
}
}
}
n = n.getNextSibling();
}
}
SchemaInfo schema = new SchemaInfo(ns);
schema.setSystemId(systemId);
XmlSchema xmlSchema;
synchronized (d) {
xmlSchema = col.read(d, systemId);
schema.setSchema(xmlSchema);
schema.setElement(d.getDocumentElement());
}
serviceInfo.addSchema(schema);
return xmlSchema;
}
use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.
the class ImportRepairTest method tryToParseSchemas.
private void tryToParseSchemas() throws Exception {
// Get DOM Implementation using DOM Registry
final List<DOMLSInput> inputs = new ArrayList<>();
final Map<String, LSInput> resolverMap = new HashMap<>();
for (XmlSchema schema : collection.getXmlSchemas()) {
if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schema.getTargetNamespace())) {
continue;
}
Document document = new XmlSchemaSerializer().serializeSchema(schema, false)[0];
DOMLSInput input = new DOMLSInput(document, schema.getTargetNamespace());
dumpSchema(document);
resolverMap.put(schema.getTargetNamespace(), input);
inputs.add(input);
}
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementation impl = registry.getDOMImplementation("XS-Loader");
try {
Object schemaLoader = findMethod(impl, "createXSLoader").invoke(impl, new Object[1]);
DOMConfiguration config = (DOMConfiguration) findMethod(schemaLoader, "getConfig").invoke(schemaLoader);
config.setParameter("validate", Boolean.TRUE);
try {
// bug in the JDK doesn't set this, but accesses it
config.setParameter("http://www.oracle.com/xml/jaxp/properties/xmlSecurityPropertyManager", Class.forName("com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager").newInstance());
config.setParameter("http://apache.org/xml/properties/security-manager", Class.forName("com.sun.org.apache.xerces.internal.utils.XMLSecurityManager").newInstance());
} catch (Throwable t) {
// ignore
}
config.setParameter("error-handler", new DOMErrorHandler() {
public boolean handleError(DOMError error) {
LOG.info("Schema parsing error: " + error.getMessage() + " " + error.getType() + " " + error.getLocation().getUri() + " " + error.getLocation().getLineNumber() + ":" + error.getLocation().getColumnNumber());
throw new DOMErrorException(error);
}
});
config.setParameter("resource-resolver", new LSResourceResolver() {
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
return resolverMap.get(namespaceURI);
}
});
Method m = findMethod(schemaLoader, "loadInputList");
String name = m.getParameterTypes()[0].getName() + "Impl";
name = name.replace("xs.LS", "impl.xs.util.LS");
Class<?> c = Class.forName(name);
Object inputList = c.getConstructor(LSInput[].class, Integer.TYPE).newInstance(inputs.toArray(new LSInput[inputs.size()]), inputs.size());
findMethod(schemaLoader, "loadInputList").invoke(schemaLoader, inputList);
} catch (InvocationTargetException ite) {
throw (Exception) ite.getTargetException();
}
}
use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.
the class Stax2ValidationUtils method getValidator.
/**
* Create woodstox validator for a schema set.
*
* @throws XMLStreamException
*/
private XMLValidationSchema getValidator(Endpoint endpoint, ServiceInfo serviceInfo) throws XMLStreamException {
synchronized (endpoint) {
XMLValidationSchema ret = (XMLValidationSchema) endpoint.get(KEY);
if (ret == null) {
if (endpoint.containsKey(KEY)) {
return null;
}
Map<String, EmbeddedSchema> sources = new TreeMap<>();
for (SchemaInfo schemaInfo : serviceInfo.getSchemas()) {
XmlSchema sch = schemaInfo.getSchema();
String uri = sch.getTargetNamespace();
if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri)) {
continue;
}
if (sch.getTargetNamespace() == null && sch.getExternals().size() > 0) {
for (XmlSchemaExternal xmlSchemaExternal : sch.getExternals()) {
addSchema(sources, xmlSchemaExternal.getSchema(), getElement(xmlSchemaExternal.getSchema().getSourceURI()));
}
continue;
} else if (sch.getTargetNamespace() == null) {
throw new IllegalStateException("An Schema without imports must have a targetNamespace");
}
addSchema(sources, sch, schemaInfo.getElement());
}
W3CMultiSchemaFactory factory = new W3CMultiSchemaFactory();
// I don't think that we need the baseURI.
try {
ret = factory.loadSchemas(null, sources);
endpoint.put(KEY, ret);
} catch (XMLStreamException ex) {
LOG.log(Level.INFO, "Problem loading schemas. Falling back to slower method.", ret);
endpoint.put(KEY, null);
}
}
return ret;
}
}
use of org.apache.ws.commons.schema.XmlSchema in project cxf by apache.
the class SchemaInfo method getElement.
/**
* Build and return a DOM tree for this schema.
* @return a DOM Element representation of the schema
*/
public synchronized Element getElement() {
// if someone recently used this DOM tree, take advantage.
Element element = cachedElement == null ? null : cachedElement.get();
if (element != null) {
return element;
}
if (getSchema() == null) {
throw new RuntimeException("No XmlSchema in SchemaInfo");
}
XmlSchema sch = getSchema();
synchronized (sch) {
XmlSchema schAgain = getSchema();
// Some unit tests really want to see 'tns:'.
if (schAgain.getNamespaceContext() == null) {
NamespaceMap nsMap = new NamespaceMap();
nsMap.add("xsd", Constants.URI_2001_SCHEMA_XSD);
nsMap.add("tns", schAgain.getTargetNamespace());
schAgain.setNamespaceContext(nsMap);
}
Document serializedSchema;
try {
serializedSchema = schAgain.getSchemaDocument();
} catch (XmlSchemaSerializerException e) {
throw new RuntimeException("Error serializing Xml Schema", e);
}
element = serializedSchema.getDocumentElement();
cachedElement = new SoftReference<Element>(element);
}
// The aegis databinding tests demonstrate this particularly.
if (element.getPrefix() == null && !Constants.URI_2001_SCHEMA_XSD.equals(element.getAttributeNS(Constants.XMLNS_ATTRIBUTE_NS_URI, Constants.XMLNS_ATTRIBUTE))) {
Attr attr = element.getOwnerDocument().createAttributeNS(Constants.XMLNS_ATTRIBUTE_NS_URI, Constants.XMLNS_ATTRIBUTE);
attr.setValue(Constants.URI_2001_SCHEMA_XSD);
element.setAttributeNodeNS(attr);
}
return element;
}
Aggregations