All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.phloc.schematron.pure.model.PSRule Maven / Gradle / Ivy

There is a newer version: 2.7.1
Show newest version
/**
 * Copyright (C) 2014 phloc systems
 * http://www.phloc.com
 * office[at]phloc[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phloc.schematron.pure.model;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;

import com.phloc.commons.ValueEnforcer;
import com.phloc.commons.annotations.ReturnsMutableCopy;
import com.phloc.commons.collections.ContainerHelper;
import com.phloc.commons.log.InMemoryLogger;
import com.phloc.commons.microdom.IMicroElement;
import com.phloc.commons.microdom.impl.MicroElement;
import com.phloc.commons.string.StringHelper;
import com.phloc.commons.string.ToStringGenerator;
import com.phloc.schematron.CSchematron;
import com.phloc.schematron.CSchematronXML;

/**
 * A single Schematron rule-element.
* A list of assertions tested within the context specified by the required * context attribute. The context attribute specifies the rule context * expression.
* NOTE: It is not an error if a rule never fires in a document. In order to * test that a document always has some context, a new pattern should be created * from the context of the document, with an assertion requiring the element or * attribute.
* The icon, see and fpi attributes allow rich interfaces and documentation.
* The flag attribute allows more detailed outcomes.
* The role and subject attributes allow explicit identification of some part of * a pattern as part of the validation outcome.
* When the rule element has the attribute abstract with a value true, then the * rule is an abstract rule. An abstract rule shall not have a context * attribute. An abstract rule is a list of assertions that will be invoked by * other rules belonging to the same pattern using the extends element. Abstract * rules provide a mechanism for reducing schema size. * * @author Philip Helger */ @NotThreadSafe public class PSRule implements IPSElement, IPSHasID, IPSHasFlag, IPSHasForeignElements, IPSHasIncludes, IPSHasLets, IPSHasRichGroup, IPSHasLinkableGroup { public static final boolean DEFAULT_ABSTRACT = false; private String m_sFlag; private PSRichGroup m_aRich; private PSLinkableGroup m_aLinkable; private boolean m_bAbstract = DEFAULT_ABSTRACT; private String m_sContext; private String m_sID; private final List m_aIncludes = new ArrayList (); private final List m_aLets = new ArrayList (); private final List m_aContent = new ArrayList (); private Map m_aForeignAttrs; private List m_aForeignElements; public PSRule () {} public boolean isValid (@Nonnull final InMemoryLogger aLogger) { // abstract rules need an ID if (m_bAbstract && StringHelper.hasNoText (m_sID)) { aLogger.error ("abstract has no 'id'"); return false; } // abstract rules may not have a context if (m_bAbstract && StringHelper.hasText (m_sContext)) { aLogger.error ("abstract may not have a 'context'"); return false; } // Non-abstract rules need a context if (!m_bAbstract && StringHelper.hasNoText (m_sContext)) { aLogger.error (" must have a 'context'"); return false; } // At least one assert, report or extends must be present if (m_aContent.isEmpty ()) { aLogger.error (" has no content"); return false; } for (final PSInclude aInclude : m_aIncludes) if (!aInclude.isValid (aLogger)) return false; for (final PSLet aLet : m_aLets) if (!aLet.isValid (aLogger)) return false; for (final IPSElement aContent : m_aContent) if (!aContent.isValid (aLogger)) return false; return true; } public boolean isMinimal () { for (final PSInclude aInclude : m_aIncludes) if (!aInclude.isMinimal ()) return false; for (final PSLet aLet : m_aLets) if (!aLet.isMinimal ()) return false; for (final IPSElement aContent : m_aContent) if (!aContent.isMinimal ()) return false; return true; } 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 ArrayList (); m_aForeignElements.add (aForeignElement); } public void addForeignElements (@Nonnull final List aForeignElements) { ValueEnforcer.notNull (aForeignElements, "ForeignElements"); for (final IMicroElement aForeignElement : aForeignElements) addForeignElement (aForeignElement); } public boolean hasForeignElements () { return m_aForeignElements != null && !m_aForeignElements.isEmpty (); } @Nonnull @ReturnsMutableCopy public List getAllForeignElements () { return ContainerHelper.newList (m_aForeignElements); } public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue) { ValueEnforcer.notNull (sAttrName, "AttrName"); ValueEnforcer.notNull (sAttrValue, "AttrValue"); if (m_aForeignAttrs == null) m_aForeignAttrs = new LinkedHashMap (); m_aForeignAttrs.put (sAttrName, sAttrValue); } public void addForeignAttributes (@Nonnull final Map aForeignAttrs) { ValueEnforcer.notNull (aForeignAttrs, "ForeignAttrs"); for (final Map.Entry aEntry : aForeignAttrs.entrySet ()) addForeignAttribute (aEntry.getKey (), aEntry.getValue ()); } public boolean hasForeignAttributes () { return m_aForeignAttrs != null && !m_aForeignAttrs.isEmpty (); } @Nonnull @ReturnsMutableCopy public Map getAllForeignAttributes () { return ContainerHelper.newOrderedMap (m_aForeignAttrs); } public void setFlag (@Nullable final String sFlag) { m_sFlag = sFlag; } @Nullable public String getFlag () { return m_sFlag; } public void setRich (@Nullable final PSRichGroup aRich) { m_aRich = aRich; } public boolean hasRich () { return m_aRich != null; } @Nullable public PSRichGroup getRich () { return m_aRich; } @Nullable public PSRichGroup getRichClone () { return m_aRich == null ? null : m_aRich.getClone (); } public void setLinkable (@Nullable final PSLinkableGroup aLinkable) { m_aLinkable = aLinkable; } public boolean hasLinkable () { return m_aLinkable != null; } @Nullable public PSLinkableGroup getLinkable () { return m_aLinkable; } @Nullable public PSLinkableGroup getLinkableClone () { return m_aLinkable == null ? null : m_aLinkable.getClone (); } /** * @param bAbstract * The abstract state of this rule. */ public void setAbstract (final boolean bAbstract) { m_bAbstract = bAbstract; } /** * @return true if this rule is abstract, false * otherwise. Default is {@value #DEFAULT_ABSTRACT}. */ public boolean isAbstract () { return m_bAbstract; } public void setContext (@Nullable final String sContext) { m_sContext = sContext; } @Nullable public String getContext () { return m_sContext; } public void setID (@Nullable final String sID) { m_sID = sID; } public boolean hasID () { return m_sID != null; } @Nullable public String getID () { return m_sID; } public void addInclude (@Nonnull final PSInclude aInclude) { ValueEnforcer.notNull (aInclude, "Include"); m_aIncludes.add (aInclude); } public boolean hasAnyInclude () { return !m_aIncludes.isEmpty (); } @Nonnull @ReturnsMutableCopy public List getAllIncludes () { return ContainerHelper.newList (m_aIncludes); } public void addLet (@Nonnull final PSLet aLet) { ValueEnforcer.notNull (aLet, "Let"); m_aLets.add (aLet); } public boolean hasAnyLet () { return !m_aLets.isEmpty (); } @Nonnull @ReturnsMutableCopy public List getAllLets () { return ContainerHelper.newList (m_aLets); } @Nonnull @ReturnsMutableCopy public Map getAllLetsAsMap () { final Map ret = new LinkedHashMap (); for (final PSLet aLet : m_aLets) ret.put (aLet.getName (), aLet.getValue ()); return ret; } public void addAssertReport (@Nonnull final PSAssertReport aAssertReport) { ValueEnforcer.notNull (aAssertReport, "AssertReport"); m_aContent.add (aAssertReport); } @Nonnull @ReturnsMutableCopy public List getAllAssertReports () { final List ret = new ArrayList (); for (final IPSElement aElement : m_aContent) if (aElement instanceof PSAssertReport) ret.add ((PSAssertReport) aElement); return ret; } public void addExtends (@Nonnull final PSExtends aExtends) { ValueEnforcer.notNull (aExtends, "Extends"); m_aContent.add (aExtends); } @Nonnull @ReturnsMutableCopy public List getAllExtends () { final List ret = new ArrayList (); for (final IPSElement aElement : m_aContent) if (aElement instanceof PSExtends) ret.add ((PSExtends) aElement); return ret; } @Nonnegative public int getExtendsCount () { int ret = 0; for (final IPSElement aElement : m_aContent) if (aElement instanceof PSExtends) ++ret; return ret; } public boolean hasAnyExtends () { for (final IPSElement aElement : m_aContent) if (aElement instanceof PSExtends) return true; return false; } /** * @return A list consisting of {@link PSAssertReport} and {@link PSExtends} * parameters */ @Nonnull @ReturnsMutableCopy public List getAllContentElements () { return ContainerHelper.newList (m_aContent); } @Nonnull public IMicroElement getAsMicroElement () { final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON, CSchematronXML.ELEMENT_RULE); ret.setAttribute (CSchematronXML.ATTR_FLAG, m_sFlag); if (m_bAbstract) ret.setAttribute (CSchematronXML.ATTR_ABSTRACT, "true"); ret.setAttribute (CSchematronXML.ATTR_CONTEXT, m_sContext); ret.setAttribute (CSchematronXML.ATTR_ID, m_sID); if (m_aRich != null) 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 PSInclude aInclude : m_aIncludes) ret.appendChild (aInclude.getAsMicroElement ()); for (final PSLet aLet : m_aLets) ret.appendChild (aLet.getAsMicroElement ()); for (final IPSElement aContent : m_aContent) ret.appendChild (aContent.getAsMicroElement ()); if (m_aForeignAttrs != null) for (final Map.Entry aEntry : m_aForeignAttrs.entrySet ()) ret.setAttribute (aEntry.getKey (), aEntry.getValue ()); return ret; } @Override public String toString () { return new ToStringGenerator (this).appendIfNotNull ("flag", m_sFlag) .appendIfNotNull ("rich", m_aRich) .appendIfNotNull ("linkable", m_aLinkable) .append ("abstract", m_bAbstract) .appendIfNotNull ("context", m_sContext) .appendIfNotNull ("id", m_sID) .append ("includes", m_aIncludes) .append ("lets", m_aLets) .append ("content", m_aContent) .appendIfNotNull ("foreignAttrs", m_aForeignAttrs) .appendIfNotNull ("foreignElements", m_aForeignElements) .toString (); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy