Improve code. Improve tests.

This commit is contained in:
Christian P. MOMON 2013-08-04 01:56:56 +02:00
parent 8c2cc87fd7
commit ee3237faaa
2 changed files with 45 additions and 13 deletions

View file

@ -31,7 +31,7 @@ import org.xml.sax.SAXException;
public class XidynUtils
{
static private final Logger logger = LoggerFactory.getLogger(XidynUtils.class);
private static final Pattern BODY_PATTERN = Pattern.compile("^.*<[bB][oO][dD][yY]>\\s*(.*\\S)\\s*</[bB][oO][dD][yY]>.*$");
private static final Pattern BODY_PATTERN = Pattern.compile("(?s)^.*<[bB][oO][dD][yY]>\\s*(\\S.*\\S)\\s*</[bB][oO][dD][yY]>.*$");
/**
* This method adds a tag to a DOM object.
@ -41,18 +41,21 @@ public class XidynUtils
// Find head tag.
Node headNode = findHeadNode(doc);
logger.debug("headNode = [" + headNode + "]");
if (headNode != null)
{
Node metaNode = doc.createElement("meta");
Node metaNode = doc.createElement("meta");
NamedNodeMap attrMap = metaNode.getAttributes();
Node attrNode = doc.createAttribute("name");
attrMap.setNamedItem(attrNode);
attrNode.setNodeValue(name);
NamedNodeMap attrMap = metaNode.getAttributes();
Node attrNode = doc.createAttribute("name");
attrMap.setNamedItem(attrNode);
attrNode.setNodeValue(name);
attrNode = doc.createAttribute("content");
attrMap.setNamedItem(attrNode);
attrNode.setNodeValue(content);
headNode.insertBefore(metaNode, headNode.getFirstChild());
attrNode = doc.createAttribute("content");
attrMap.setNamedItem(attrNode);
attrNode.setNodeValue(content);
headNode.insertBefore(metaNode, headNode.getFirstChild());
}
}
/**

View file

@ -52,7 +52,7 @@ public class XidynUtilsTest
@Test
public void testExtractBodyContent03()
{
String source = "aaaaa<boDY></BodY>zzzzz";
String source = "aaaaa<body></BodY>zzzzz";
String target = XidynUtils.extractBodyContent(source);
Assertions.assertThat(target).isEqualTo("");
@ -64,12 +64,41 @@ public class XidynUtilsTest
@Test
public void testExtractBodyContent04()
{
String source = "aaaaa<boDY> hello </BodY>zzzzz";
String source = "aaaaa<boDY> hello </body>zzzzz";
String target = XidynUtils.extractBodyContent(source);
Assertions.assertThat(target).isEqualTo("hello");
}
/**
*
*/
@Test
public void testExtractBodyContent05()
{
StringBuffer buffer = new StringBuffer(1000);
buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>").append("\n");
buffer.append("<!DOCTYPE html>").append("\n");
buffer.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">").append("\n");
buffer.append("<head>").append("\n");
buffer.append("<title>Kinsources</title>").append("\n");
buffer.append("<meta charset=\"UTF-8\" />").append("\n");
buffer.append("<link rel=\"icon\" type=\"image/x-icon\" href=\"/favicon.ico\" />").append("\n");
buffer.append(" <link rel=\"stylesheet\" type=\"text/css\" href=\"kiwa.css\" />").append("\n");
buffer.append("<meta content=\"kinsources, puck, devinsy, gedcom\" name=\"keywords\" />").append("\n");
buffer.append("</head>").append("\n");
buffer.append("<body>");
buffer.append("WELCOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOME");
buffer.append("</body>").append("\n");
buffer.append("</html>").append("\n");
String source = buffer.toString();
// System.out.println("[" + source + "]");
String target = XidynUtils.extractBodyContent(source);
Assertions.assertThat(target).isEqualTo("WELCOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOME");
}
/**
*
*/