Polish XsdDocumentedTests

- NicerNoce->XmlNode
- NicerXmlSupport->XmlSupport
- NicerXmlParser->XmlParser

Issue: gh-4939
This commit is contained in:
Rob Winch 2018-03-29 16:33:25 -05:00
parent 0c0abea3ad
commit 234c20eb30
5 changed files with 38 additions and 38 deletions

View File

@ -27,12 +27,12 @@ import java.util.stream.Stream;
* @author Josh Cummings * @author Josh Cummings
*/ */
public class SpringSecurityXsdParser { public class SpringSecurityXsdParser {
private NicerNode rootElement; private XmlNode rootElement;
private Set<String> attrElmts = new LinkedHashSet<>(); private Set<String> attrElmts = new LinkedHashSet<>();
private Map<String, Element> elementNameToElement = new HashMap<>(); private Map<String, Element> elementNameToElement = new HashMap<>();
public SpringSecurityXsdParser(NicerNode rootElement) { public SpringSecurityXsdParser(XmlNode rootElement) {
this.rootElement = rootElement; this.rootElement = rootElement;
} }
@ -52,7 +52,7 @@ public class SpringSecurityXsdParser {
* @param node * @param node
* @return * @return
*/ */
private Map<String, Element> elements(NicerNode node) { private Map<String, Element> elements(XmlNode node) {
Map<String, Element> elementNameToElement = new HashMap<>(); Map<String, Element> elementNameToElement = new HashMap<>();
node.children().forEach(child -> { node.children().forEach(child -> {
@ -73,7 +73,7 @@ public class SpringSecurityXsdParser {
* @param element * @param element
* @return a collection of Attribute objects that are children of element. * @return a collection of Attribute objects that are children of element.
*/ */
private Collection<Attribute> attrs(NicerNode element) { private Collection<Attribute> attrs(XmlNode element) {
Collection<Attribute> attrs = new ArrayList<>(); Collection<Attribute> attrs = new ArrayList<>();
element.children().forEach(c -> { element.children().forEach(c -> {
String name = c.simpleName(); String name = c.simpleName();
@ -94,7 +94,7 @@ public class SpringSecurityXsdParser {
* @param element * @param element
* @return * @return
*/ */
private Collection<Attribute> attrgrps(NicerNode element) { private Collection<Attribute> attrgrps(XmlNode element) {
Collection<Attribute> attrgrp = new ArrayList<>(); Collection<Attribute> attrgrp = new ArrayList<>();
element.children().forEach(c -> { element.children().forEach(c -> {
@ -105,7 +105,7 @@ public class SpringSecurityXsdParser {
attrgrp.addAll(attrgrp(c)); attrgrp.addAll(attrgrp(c));
} else { } else {
String name = c.attribute("ref").split(":")[1]; String name = c.attribute("ref").split(":")[1];
NicerNode attrGrp = findNode(element, name); XmlNode attrGrp = findNode(element, name);
attrgrp.addAll(attrgrp(attrGrp)); attrgrp.addAll(attrgrp(attrGrp));
} }
} else { } else {
@ -116,8 +116,8 @@ public class SpringSecurityXsdParser {
return attrgrp; return attrgrp;
} }
private NicerNode findNode(NicerNode c, String name) { private XmlNode findNode(XmlNode c, String name) {
NicerNode root = c; XmlNode root = c;
while (!"schema".equals(root.simpleName())) { while (!"schema".equals(root.simpleName())) {
root = root.parent().get(); root = root.parent().get();
} }
@ -127,7 +127,7 @@ public class SpringSecurityXsdParser {
.findFirst().orElseThrow(IllegalArgumentException::new); .findFirst().orElseThrow(IllegalArgumentException::new);
} }
private Stream<NicerNode> expand(NicerNode root) { private Stream<XmlNode> expand(XmlNode root) {
return Stream.concat( return Stream.concat(
Stream.of(root), Stream.of(root),
root.children().flatMap(this::expand)); root.children().flatMap(this::expand));
@ -139,7 +139,7 @@ public class SpringSecurityXsdParser {
* @param e * @param e
* @return all the attributes for a specific attributeGroup and any child attributeGroups * @return all the attributes for a specific attributeGroup and any child attributeGroups
*/ */
private Collection<Attribute> attrgrp(NicerNode e) { private Collection<Attribute> attrgrp(XmlNode e) {
Collection<Attribute> attrs = attrs(e); Collection<Attribute> attrs = attrs(e);
attrs.addAll(attrgrps(e)); attrs.addAll(attrgrps(e));
return attrs; return attrs;
@ -151,7 +151,7 @@ public class SpringSecurityXsdParser {
* @param element * @param element
* @return * @return
*/ */
private String desc(NicerNode element) { private String desc(XmlNode element) {
return element.child("annotation") return element.child("annotation")
.flatMap(annotation -> annotation.child("documentation")) .flatMap(annotation -> annotation.child("documentation"))
.map(documentation -> documentation.text()) .map(documentation -> documentation.text())
@ -164,7 +164,7 @@ public class SpringSecurityXsdParser {
* @param n * @param n
* @return * @return
*/ */
private Attribute attr(NicerNode n) { private Attribute attr(XmlNode n) {
return new Attribute(desc(n), n.attribute("name")); return new Attribute(desc(n), n.attribute("name"));
} }
@ -174,7 +174,7 @@ public class SpringSecurityXsdParser {
* @param n * @param n
* @return * @return
*/ */
private Element elmt(NicerNode n) { private Element elmt(XmlNode n) {
String name = n.attribute("ref"); String name = n.attribute("ref");
if (StringUtils.isEmpty(name)) { if (StringUtils.isEmpty(name)) {
name = n.attribute("name"); name = n.attribute("name");

View File

@ -25,10 +25,10 @@ import java.util.stream.Stream;
/** /**
* @author Josh Cummings * @author Josh Cummings
*/ */
public class NicerNode { public class XmlNode {
private final Node node; private final Node node;
public NicerNode(Node node) { public XmlNode(Node node) {
this.node = node; this.node = node;
} }
@ -41,23 +41,23 @@ public class NicerNode {
return this.node.getTextContent(); return this.node.getTextContent();
} }
public Stream<NicerNode> children() { public Stream<XmlNode> children() {
NodeList children = this.node.getChildNodes(); NodeList children = this.node.getChildNodes();
return IntStream.range(0, children.getLength()) return IntStream.range(0, children.getLength())
.mapToObj(children::item) .mapToObj(children::item)
.map(NicerNode::new); .map(XmlNode::new);
} }
public Optional<NicerNode> child(String name) { public Optional<XmlNode> child(String name) {
return this.children() return this.children()
.filter(child -> name.equals(child.simpleName())) .filter(child -> name.equals(child.simpleName()))
.findFirst(); .findFirst();
} }
public Optional<NicerNode> parent() { public Optional<XmlNode> parent() {
return Optional.ofNullable(this.node.getParentNode()) return Optional.ofNullable(this.node.getParentNode())
.map(parent -> new NicerNode(parent)); .map(parent -> new XmlNode(parent));
} }
public String attribute(String name) { public String attribute(String name) {

View File

@ -26,19 +26,19 @@ import java.io.InputStream;
/** /**
* @author Josh Cummings * @author Josh Cummings
*/ */
public class NicerXmlParser implements AutoCloseable { public class XmlParser implements AutoCloseable {
private InputStream xml; private InputStream xml;
public NicerXmlParser(InputStream xml) { public XmlParser(InputStream xml) {
this.xml = xml; this.xml = xml;
} }
public NicerNode parse() { public XmlNode parse() {
try { try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
return new NicerNode(dBuilder.parse(this.xml)); return new XmlNode(dBuilder.parse(this.xml));
} catch ( IOException | ParserConfigurationException | SAXException e ) { } catch ( IOException | ParserConfigurationException | SAXException e ) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }

View File

@ -25,18 +25,18 @@ import java.util.Map;
* *
* @author Josh Cummings * @author Josh Cummings
*/ */
public class NicerXmlSupport { public class XmlSupport {
private NicerXmlParser parser; private XmlParser parser;
public NicerNode parse(String location) throws IOException { public XmlNode parse(String location) throws IOException {
ClassPathResource resource = new ClassPathResource(location); ClassPathResource resource = new ClassPathResource(location);
this.parser = new NicerXmlParser(resource.getInputStream()); this.parser = new XmlParser(resource.getInputStream());
return this.parser.parse(); return this.parser.parse();
} }
public Map<String, Element> elementsByElementName(String location) throws IOException { public Map<String, Element> elementsByElementName(String location) throws IOException {
NicerNode node = parse(location); XmlNode node = parse(location);
return new SpringSecurityXsdParser(node).parse(); return new SpringSecurityXsdParser(node).parse();
} }

View File

@ -52,7 +52,7 @@ public class XsdDocumentedTests {
String schema31xDocumentLocation = "org/springframework/security/config/spring-security-3.1.xsd"; String schema31xDocumentLocation = "org/springframework/security/config/spring-security-3.1.xsd";
String schemaDocumentLocation = "org/springframework/security/config/spring-security-5.0.xsd"; String schemaDocumentLocation = "org/springframework/security/config/spring-security-5.0.xsd";
NicerXmlSupport xml = new NicerXmlSupport(); XmlSupport xml = new XmlSupport();
@After @After
public void close() throws IOException { public void close() throws IOException {
@ -62,17 +62,17 @@ public class XsdDocumentedTests {
@Test @Test
public void parseWhenLatestXsdThenAllNamedSecurityFiltersAreDefinedAndOrderedProperly() public void parseWhenLatestXsdThenAllNamedSecurityFiltersAreDefinedAndOrderedProperly()
throws IOException { throws IOException {
NicerNode root = this.xml.parse(this.schemaDocumentLocation); XmlNode root = this.xml.parse(this.schemaDocumentLocation);
List<String> nodes = List<String> nodes =
root.child("schema") root.child("schema")
.map(NicerNode::children) .map(XmlNode::children)
.orElse(Stream.empty()) .orElse(Stream.empty())
.filter(node -> .filter(node ->
"simpleType".equals(node.simpleName()) && "simpleType".equals(node.simpleName()) &&
"named-security-filter".equals(node.attribute("name"))) "named-security-filter".equals(node.attribute("name")))
.flatMap(NicerNode::children) .flatMap(XmlNode::children)
.flatMap(NicerNode::children) .flatMap(XmlNode::children)
.map(node -> node.attribute("value")) .map(node -> node.attribute("value"))
.filter(StringUtils::isNotEmpty) .filter(StringUtils::isNotEmpty)
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -110,17 +110,17 @@ public class XsdDocumentedTests {
"LAST" "LAST"
); );
NicerNode root = this.xml.parse(this.schema31xDocumentLocation); XmlNode root = this.xml.parse(this.schema31xDocumentLocation);
List<String> nodes = List<String> nodes =
root.child("schema") root.child("schema")
.map(NicerNode::children) .map(XmlNode::children)
.orElse(Stream.empty()) .orElse(Stream.empty())
.filter(node -> .filter(node ->
"simpleType".equals(node.simpleName()) && "simpleType".equals(node.simpleName()) &&
"named-security-filter".equals(node.attribute("name"))) "named-security-filter".equals(node.attribute("name")))
.flatMap(NicerNode::children) .flatMap(XmlNode::children)
.flatMap(NicerNode::children) .flatMap(XmlNode::children)
.map(node -> node.attribute("value")) .map(node -> node.attribute("value"))
.filter(StringUtils::isNotEmpty) .filter(StringUtils::isNotEmpty)
.collect(Collectors.toList()); .collect(Collectors.toList());