Skip to content

Commit

Permalink
Merge pull request #47 from cebe/schemalocation-by-namespace2
Browse files Browse the repository at this point in the history
backport KnownNamespaceSchemaLocation
  • Loading branch information
goetas authored Apr 2, 2019
2 parents a472ab0 + d56a89c commit 0a2fbab
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class SchemaReader

private $knownLocationSchemas = array();

private $knownNamespaceSchemaLocations = array();

private static $globalSchemaInfo = array(
self::XML_NS => 'http://www.w3.org/2001/xml.xsd',
self::XSD_NS => 'http://www.w3.org/2001/XMLSchema.xsd'
Expand All @@ -55,11 +57,29 @@ public function __construct()
$this->addKnownSchemaLocation('http://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd', __DIR__ . '/Resources/xmldsig-core-schema.xsd');
}

/**
* Override remote location with a local file.
*
* @param string $remote remote schema URL
* @param string $local local file path
*/
public function addKnownSchemaLocation($remote, $local)
{
$this->knownLocationSchemas[$remote] = $local;
}

/**
* Specify schema location by namespace.
* This can be used for schemas which import namespaces but do not specify schemaLocation attributes.
*
* @param string $namespace namespace
* @param string $location schema URL
*/
public function addKnownNamespaceSchemaLocation($namespace, $location)
{
$this->knownNamespaceSchemaLocations[$namespace] = $location;
}

private function loadAttributeGroup(Schema $schema, DOMElement $node)
{
$attGroup = new AttributeGroup($schema, $node->getAttribute("name"));
Expand Down Expand Up @@ -647,7 +667,11 @@ private function fillItem(Item $element, DOMElement $node)
private function loadImport(Schema $schema, DOMElement $node)
{
$base = urldecode($node->ownerDocument->documentURI);
$file = UrlUtils::resolveRelativeUrl($base, $node->getAttribute("schemaLocation"));
$schemaLocation = $node->getAttribute("schemaLocation");
if (!$schemaLocation && $node->hasAttribute("namespace") && isset($this->knownNamespaceSchemaLocations[$node->getAttribute("namespace")])) {
$schemaLocation = $this->knownNamespaceSchemaLocations[$node->getAttribute("namespace")];
}
$file = UrlUtils::resolveRelativeUrl($base, $schemaLocation);
if ($node->hasAttribute("namespace")
&& isset(self::$globalSchemaInfo[$node->getAttribute("namespace")])
&& isset($this->loadedFiles[self::$globalSchemaInfo[$node->getAttribute("namespace")]])
Expand Down
10 changes: 9 additions & 1 deletion tests/ImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ public function testBase()

$this->assertSame($remoteAttr, $localAttrs[0]);
}
}

public function testKnownNamespaceLocationImport()
{
$this->reader->addKnownNamespaceSchemaLocation('urn:example:profile-1.1', __DIR__.'/schema/profile-1.1.xsd');

$schema = $this->reader->readFile(__DIR__.'/schema/transaction-1.0.xsd');
$this->assertInstanceOf('GoetasWebservices\XML\XSDReader\Schema\Schema', $schema);
}
}
39 changes: 39 additions & 0 deletions tests/schema/profile-1.1.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="iso-8859-1" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:example:profile-1.1"
xmlns:vct="urn:example:transaction-1.0"
targetNamespace="urn:example:profile-1.1">

<xsd:import namespace="urn:example:transaction-1.0"
schemaLocation="transaction-1.0.xsd" />

<xsd:element name="GetProfileRequest" type="vct:RequestType">
<xsd:annotation>
<xsd:documentation>Example</xsd:documentation>
</xsd:annotation>
</xsd:element>

<xsd:simpleType name="TransactionType">
<xsd:annotation>
<xsd:documentation>Liste der Transaktionen.</xsd:documentation>
</xsd:annotation>
<xsd:restriction base="xsd:normalizedString">
<xsd:enumeration value="Order">
<xsd:annotation>
<xsd:documentation>Order</xsd:documentation>
</xsd:annotation>
</xsd:enumeration>
<xsd:enumeration value="OrderInOnlineShop">
<xsd:annotation>
<xsd:documentation>OrderInOnlineShop</xsd:documentation>
</xsd:annotation>
</xsd:enumeration>
<xsd:enumeration value="TextSearch">
<xsd:annotation>
<xsd:documentation>Volltextsuche</xsd:documentation>
</xsd:annotation>
</xsd:enumeration>
</xsd:restriction>
</xsd:simpleType>

</xsd:schema>
23 changes: 23 additions & 0 deletions tests/schema/transaction-1.0.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="iso-8859-1" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:example:transaction-1.0"
xmlns:vcp="urn:example:profile-1.1"
targetNamespace="urn:example:transaction-1.0">

<xsd:import namespace="urn:example:profile-1.1"/><!-- this line does no specify schemaLocation -->

<xsd:complexType name="TransactionStatusType">
<xsd:sequence>
<xsd:element name="TransactionId" type="xsd:normalizedString" />
<xsd:element name="Transaction" type="vcp:TransactionType" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>


<xsd:complexType name="RequestType">
<xsd:sequence>
<xsd:element name="TransactionID" type="xsd:normalizedString" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>

</xsd:schema>

0 comments on commit 0a2fbab

Please sign in to comment.