Assert { public static void xliffAttrRestricted(Element elem, boolean nonXliffAllowed, boolean xmlSpaceAllowed, boolean xmlLangAllowed, String... xliffAttrNames) throws ConstraintViolationException { HashSet<String> restriction = new HashSet<String>(); for (String name : xliffAttrNames) { restriction.add(name); } String elemNS = elem.getNamespaceURI(); NamedNodeMap nodeMap = elem.getAttributes(); for (int i = 0; i < nodeMap.getLength(); ++i) { Attr attr = (Attr) nodeMap.item(i); String ns = attr.getNamespaceURI(); String name = attr.getLocalName(); if (ns == null) { ns = elemNS; } if (Schemas.XMLNS_NAMESPACE.equals(ns)) { continue; } if ((ns == null) || ns.equals(Schemas.XLIFF_12_NAMESPACE)) { if (!restriction.contains(name)) { String msg = MessageFormat .format( "Element ''{0}'' specifies attribute ''{1}'' that is not allowed here", elem.getLocalName(), name); throw new ConstraintViolationException(msg); } } else if (ns.equals(Schemas.XML_NAMESPACE)) { if (!xmlSpaceAllowed && XmlSpace.NAME.equals(name)) { String msg = MessageFormat .format( "Element ''{0}'' specifies XML-attribute ''{1}'' that is not allowed here", elem.getLocalName(), name); throw new ConstraintViolationException(msg); } else if (!xmlLangAllowed && XmlLang.NAME.equals(name)) { String msg = MessageFormat .format( "Element ''{0}'' specifies XML-attribute ''{1}'' that is not allowed here", elem.getLocalName(), name); throw new ConstraintViolationException(msg); } } else if (!nonXliffAllowed) { String msg = MessageFormat .format( "Element ''{0}'' specifies Non-XLIFF-attribute ''{1}'' that is not allowed here", elem.getLocalName(), name); throw new ConstraintViolationException(msg); } } } private  Assert(); static void notNull(Object param, String paramName); static void validXMLChars(String s, String paramName); static void isNmtoken(String token, String paramName); static void xliffAttrAvailable(Element elem, String... attrNames); static void xliffAttrRestricted(Element elem,
			boolean nonXliffAllowed, boolean xmlSpaceAllowed,
			boolean xmlLangAllowed, String... xliffAttrNames); static void isXliffElement(org.w3c.dom.Node node); static void isXliffElement(org.w3c.dom.Node node, String name); static void isElement(Node node); static void isNonXliffElement(org.w3c.dom.Node node); static void isInstance(Object object, String name,
			Class<?>... classes); static void areInstances(Collection<?> objects, String name,
			Class<?>... classes);  }
@Test public void testXliffAttrRestricted() { Document doc = Utils.createDocument(); Element elem = doc.createElementNS(XLIFF_NS, "test"); elem.setAttributeNS(XLIFF_NS, "a", "abc"); elem.setAttributeNS(XLIFF_NS, "b", "abc"); elem.setAttributeNS(XLIFF_NS, "c", "abc"); elem.setAttributeNS(XML_NS, "xml:lang", "en"); elem.setAttributeNS(XML_NS, "xml:space", "preserve"); elem.setAttributeNS("xyz", "a", "abc"); try { Assert.xliffAttrRestricted(elem, true, true, true, "a", "b", "c"); } catch (ConstraintViolationException e) { fail("Did not expect ConstraintViolationException"); } try { Assert.xliffAttrRestricted(elem, true, true, true, "a", "b", "c", "d"); } catch (ConstraintViolationException e) { fail("Did not expect ConstraintViolationException"); } try { Assert.xliffAttrRestricted(elem, true, true, true, "b", "c", "d"); fail("Expected ConstraintViolationException"); } catch (ConstraintViolationException e) { } try { Assert.xliffAttrRestricted(elem, false, true, true, "a", "b", "c"); fail("Expected ConstraintViolationException"); } catch (ConstraintViolationException e) { } try { Assert.xliffAttrRestricted(elem, true, false, true, "a", "b", "c"); fail("Expected ConstraintViolationException"); } catch (ConstraintViolationException e) { } try { Assert.xliffAttrRestricted(elem, true, true, false, "a", "b", "c"); fail("Expected ConstraintViolationException"); } catch (ConstraintViolationException e) { } }