Skip to content

Commit

Permalink
Improved handling of XML elements from other namespaces; #51
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Apr 30, 2019
1 parent e365878 commit 7475956
Show file tree
Hide file tree
Showing 16 changed files with 414 additions and 340 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Continue reading the **full documentation** at http://phax.github.io/ph-schematr

## News and noteworthy

* v5.0.10 - work in progress
* Improved handling of XML elements from other namespaces ([issue #51](https://github.com/phax/ph-schematron/issues/51))
* v5.0.9 - 2019-04-25
* Updated to Saxon-HE 9.9.1-1
* Updated to ant 1.9.14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import com.helger.commons.annotation.ReturnsMutableCopy;
import com.helger.commons.lang.ICloneable;
import com.helger.commons.string.ToStringGenerator;
import com.helger.schematron.CSchematron;
import com.helger.schematron.pure.model.PSNS;
import com.helger.schematron.pure.model.PSSchema;
import com.helger.xml.namespace.MapBasedNamespaceContext;
import com.helger.xml.serialize.write.EXMLSerializeIndent;
import com.helger.xml.serialize.write.IXMLWriterSettings;
import com.helger.xml.serialize.write.XMLWriterSettings;
Expand Down Expand Up @@ -66,6 +70,26 @@ public XMLWriterSettings getXMLWriterSettings ()
return new XMLWriterSettings (m_aXMLWriterSettings);
}

/**
* Helper method to extract the namespace mapping from the provided
* Schematron.
*
* @param aSchema
* The schema to extract the namespace context from. May not be
* <code>null</code>.
* @return A non-<code>null</code> but maybe empty namespace context
*/
@Nonnull
@ReturnsMutableCopy
public static MapBasedNamespaceContext createNamespaceMapping (@Nonnull final PSSchema aSchema)
{
final MapBasedNamespaceContext ret = new MapBasedNamespaceContext ();
ret.addDefaultNamespaceURI (CSchematron.NAMESPACE_SCHEMATRON);
for (final PSNS aItem : aSchema.getAllNSs ())
ret.addMapping (aItem.getPrefix (), aItem.getUri ());
return ret;
}

@Nonnull
public PSWriterSettings getClone ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public class PSActive implements IPSClonableElement <PSActive>, IPSHasForeignEle
private String m_sPattern;
private final ICommonsList <Object> m_aContent = new CommonsArrayList <> ();
private ICommonsOrderedMap <String, String> m_aForeignAttrs;
private ICommonsList <IMicroElement> m_aForeignElements;

public PSActive ()
{}
Expand Down Expand Up @@ -94,21 +93,19 @@ public void addForeignElement (@Nonnull final IMicroElement aForeignElement)
ValueEnforcer.notNull (aForeignElement, "ForeignElement");
if (aForeignElement.hasParent ())
throw new IllegalArgumentException ("ForeignElement already has a parent!");
if (m_aForeignElements == null)
m_aForeignElements = new CommonsArrayList <> ();
m_aForeignElements.add (aForeignElement);
m_aContent.add (aForeignElement);
}

public boolean hasForeignElements ()
{
return m_aForeignElements != null && m_aForeignElements.isNotEmpty ();
return m_aContent.containsAny (x -> x instanceof IMicroElement);
}

@Nonnull
@ReturnsMutableCopy
public ICommonsList <IMicroElement> getAllForeignElements ()
{
return new CommonsArrayList <> (m_aForeignElements);
return m_aContent.getAllInstanceOf (IMicroElement.class);
}

public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
Expand Down Expand Up @@ -224,14 +221,14 @@ public IMicroElement getAsMicroElement ()
{
final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON, CSchematronXML.ELEMENT_ACTIVE);
ret.setAttribute (CSchematronXML.ATTR_PATTERN, m_sPattern);
if (m_aForeignElements != null)
for (final IMicroElement aForeignElement : m_aForeignElements)
ret.appendChild (aForeignElement.getClone ());
for (final Object aContent : m_aContent)
if (aContent instanceof String)
ret.appendText ((String) aContent);
if (aContent instanceof IMicroElement)
ret.appendChild (((IMicroElement) aContent).getClone ());
else
ret.appendChild (((IPSElement) aContent).getAsMicroElement ());
if (aContent instanceof String)
ret.appendText ((String) aContent);
else
ret.appendChild (((IPSElement) aContent).getAsMicroElement ());
if (m_aForeignAttrs != null)
for (final Map.Entry <String, String> aEntry : m_aForeignAttrs.entrySet ())
ret.setAttribute (aEntry.getKey (), aEntry.getValue ());
Expand All @@ -256,9 +253,10 @@ public PSActive getClone ()
else
if (aContent instanceof PSSpan)
ret.addSpan (((PSSpan) aContent).getClone ());
else
if (aContent instanceof IMicroElement)
ret.addForeignElement (((IMicroElement) aContent).getClone ());
}
if (hasForeignElements ())
ret.addForeignElements (m_aForeignElements);
if (hasForeignAttributes ())
ret.addForeignAttributes (m_aForeignAttrs);
return ret;
Expand All @@ -270,7 +268,6 @@ public String toString ()
return new ToStringGenerator (this).appendIfNotNull ("pattern", m_sPattern)
.appendIf ("content", m_aContent, CollectionHelper::isNotEmpty)
.appendIf ("foreignAttrs", m_aForeignAttrs, CollectionHelper::isNotEmpty)
.appendIf ("foreignElements", m_aForeignElements, CollectionHelper::isNotEmpty)
.getToString ();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public class PSAssertReport implements
private PSLinkableGroup m_aLinkable;
private final ICommonsList <Object> m_aContent = new CommonsArrayList <> ();
private ICommonsOrderedMap <String, String> m_aForeignAttrs;
private ICommonsList <IMicroElement> m_aForeignElements;

public PSAssertReport (final boolean bIsAssert)
{
Expand Down Expand Up @@ -124,21 +123,19 @@ public void addForeignElement (@Nonnull final IMicroElement aForeignElement)
ValueEnforcer.notNull (aForeignElement, "ForeignElement");
if (aForeignElement.hasParent ())
throw new IllegalArgumentException ("ForeignElement already has a parent!");
if (m_aForeignElements == null)
m_aForeignElements = new CommonsArrayList <> ();
m_aForeignElements.add (aForeignElement);
m_aContent.add (aForeignElement);
}

public boolean hasForeignElements ()
{
return m_aForeignElements != null && m_aForeignElements.isNotEmpty ();
return m_aContent.containsAny (x -> x instanceof IMicroElement);
}

@Nonnull
@ReturnsMutableCopy
public ICommonsList <IMicroElement> getAllForeignElements ()
{
return new CommonsArrayList <> (m_aForeignElements);
return m_aContent.getAllInstanceOf (IMicroElement.class);
}

public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
Expand Down Expand Up @@ -374,14 +371,14 @@ public IMicroElement getAsMicroElement ()
m_aRich.fillMicroElement (ret);
if (m_aLinkable != null)
m_aLinkable.fillMicroElement (ret);
if (m_aForeignElements != null)
for (final IMicroElement aForeignElement : m_aForeignElements)
ret.appendChild (aForeignElement.getClone ());
for (final Object aContent : m_aContent)
if (aContent instanceof String)
ret.appendText ((String) aContent);
if (aContent instanceof IMicroElement)
ret.appendChild (((IMicroElement) aContent).getClone ());
else
ret.appendChild (((IPSElement) aContent).getAsMicroElement ());
if (aContent instanceof String)
ret.appendText ((String) aContent);
else
ret.appendChild (((IPSElement) aContent).getAsMicroElement ());
if (m_aForeignAttrs != null)
for (final Map.Entry <String, String> aEntry : m_aForeignAttrs.entrySet ())
ret.setAttribute (aEntry.getKey (), aEntry.getValue ());
Expand All @@ -400,7 +397,6 @@ public String toString ()
.appendIfNotNull ("linkable", m_aLinkable)
.appendIf ("content", m_aContent, CollectionHelper::isNotEmpty)
.appendIf ("foreignAttrs", m_aForeignAttrs, CollectionHelper::isNotEmpty)
.appendIf ("foreignElements", m_aForeignElements, CollectionHelper::isNotEmpty)
.getToString ();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public class PSDiagnostic implements
private PSRichGroup m_aRich;
private final ICommonsList <Object> m_aContent = new CommonsArrayList <> ();
private ICommonsOrderedMap <String, String> m_aForeignAttrs;
private ICommonsList <IMicroElement> m_aForeignElements;

public PSDiagnostic ()
{}
Expand Down Expand Up @@ -102,21 +101,19 @@ public void addForeignElement (@Nonnull final IMicroElement aForeignElement)
ValueEnforcer.notNull (aForeignElement, "ForeignElement");
if (aForeignElement.hasParent ())
throw new IllegalArgumentException ("ForeignElement already has a parent!");
if (m_aForeignElements == null)
m_aForeignElements = new CommonsArrayList <> ();
m_aForeignElements.add (aForeignElement);
m_aContent.add (aForeignElement);
}

public boolean hasForeignElements ()
{
return m_aForeignElements != null && m_aForeignElements.isNotEmpty ();
return m_aContent.containsAny (x -> x instanceof IMicroElement);
}

@Nonnull
@ReturnsMutableCopy
public ICommonsList <IMicroElement> getAllForeignElements ()
{
return new CommonsArrayList <> (m_aForeignElements);
return m_aContent.getAllInstanceOf (IMicroElement.class);
}

public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
Expand Down Expand Up @@ -250,14 +247,14 @@ public IMicroElement getAsMicroElement ()
ret.setAttribute (CSchematronXML.ATTR_ID, m_sID);
if (m_aRich != null)
m_aRich.fillMicroElement (ret);
if (m_aForeignElements != null)
for (final IMicroElement aForeignElement : m_aForeignElements)
ret.appendChild (aForeignElement.getClone ());
for (final Object aContent : m_aContent)
if (aContent instanceof String)
ret.appendText ((String) aContent);
if (aContent instanceof IMicroElement)
ret.appendChild (((IMicroElement) aContent).getClone ());
else
ret.appendChild (((IPSElement) aContent).getAsMicroElement ());
if (aContent instanceof String)
ret.appendText ((String) aContent);
else
ret.appendChild (((IPSElement) aContent).getAsMicroElement ());
if (m_aForeignAttrs != null)
for (final Map.Entry <String, String> aEntry : m_aForeignAttrs.entrySet ())
ret.setAttribute (aEntry.getKey (), aEntry.getValue ());
Expand All @@ -272,25 +269,26 @@ public PSDiagnostic getClone ()
ret.setRich (getRichClone ());
for (final Object aContent : m_aContent)
{
if (aContent instanceof String)
ret.addText ((String) aContent);
if (aContent instanceof IMicroElement)
ret.addForeignElement (((IMicroElement) aContent).getClone ());
else
if (aContent instanceof PSValueOf)
ret.addValueOf (((PSValueOf) aContent).getClone ());
if (aContent instanceof String)
ret.addText ((String) aContent);
else
if (aContent instanceof PSEmph)
ret.addEmph (((PSEmph) aContent).getClone ());
if (aContent instanceof PSValueOf)
ret.addValueOf (((PSValueOf) aContent).getClone ());
else
if (aContent instanceof PSDir)
ret.addDir (((PSDir) aContent).getClone ());
if (aContent instanceof PSEmph)
ret.addEmph (((PSEmph) aContent).getClone ());
else
if (aContent instanceof PSSpan)
ret.addSpan (((PSSpan) aContent).getClone ());
if (aContent instanceof PSDir)
ret.addDir (((PSDir) aContent).getClone ());
else
throw new IllegalStateException ("Unexpected content element: " + aContent);
if (aContent instanceof PSSpan)
ret.addSpan (((PSSpan) aContent).getClone ());
else
throw new IllegalStateException ("Unexpected content element: " + aContent);
}
if (hasForeignElements ())
ret.addForeignElements (m_aForeignElements);
if (hasForeignAttributes ())
ret.addForeignAttributes (m_aForeignAttrs);
return ret;
Expand All @@ -303,7 +301,6 @@ public String toString ()
.appendIfNotNull ("rich", m_aRich)
.appendIf ("content", m_aContent, CollectionHelper::isNotEmpty)
.appendIf ("foreignAttrs", m_aForeignAttrs, CollectionHelper::isNotEmpty)
.appendIf ("foreignElements", m_aForeignElements, CollectionHelper::isNotEmpty)
.getToString ();
}
}
Loading

0 comments on commit 7475956

Please sign in to comment.