Improve code. Improve tests.
This commit is contained in:
parent
8c2cc87fd7
commit
ee3237faaa
2 changed files with 45 additions and 13 deletions
|
@ -31,7 +31,7 @@ import org.xml.sax.SAXException;
|
||||||
public class XidynUtils
|
public class XidynUtils
|
||||||
{
|
{
|
||||||
static private final Logger logger = LoggerFactory.getLogger(XidynUtils.class);
|
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.
|
* This method adds a tag to a DOM object.
|
||||||
|
@ -41,18 +41,21 @@ public class XidynUtils
|
||||||
// Find head tag.
|
// Find head tag.
|
||||||
|
|
||||||
Node headNode = findHeadNode(doc);
|
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();
|
attrNode = doc.createAttribute("content");
|
||||||
Node attrNode = doc.createAttribute("name");
|
attrMap.setNamedItem(attrNode);
|
||||||
attrMap.setNamedItem(attrNode);
|
attrNode.setNodeValue(content);
|
||||||
attrNode.setNodeValue(name);
|
headNode.insertBefore(metaNode, headNode.getFirstChild());
|
||||||
|
}
|
||||||
attrNode = doc.createAttribute("content");
|
|
||||||
attrMap.setNamedItem(attrNode);
|
|
||||||
attrNode.setNodeValue(content);
|
|
||||||
headNode.insertBefore(metaNode, headNode.getFirstChild());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -52,7 +52,7 @@ public class XidynUtilsTest
|
||||||
@Test
|
@Test
|
||||||
public void testExtractBodyContent03()
|
public void testExtractBodyContent03()
|
||||||
{
|
{
|
||||||
String source = "aaaaa<boDY></BodY>zzzzz";
|
String source = "aaaaa<body></BodY>zzzzz";
|
||||||
|
|
||||||
String target = XidynUtils.extractBodyContent(source);
|
String target = XidynUtils.extractBodyContent(source);
|
||||||
Assertions.assertThat(target).isEqualTo("");
|
Assertions.assertThat(target).isEqualTo("");
|
||||||
|
@ -64,12 +64,41 @@ public class XidynUtilsTest
|
||||||
@Test
|
@Test
|
||||||
public void testExtractBodyContent04()
|
public void testExtractBodyContent04()
|
||||||
{
|
{
|
||||||
String source = "aaaaa<boDY> hello </BodY>zzzzz";
|
String source = "aaaaa<boDY> hello </body>zzzzz";
|
||||||
|
|
||||||
String target = XidynUtils.extractBodyContent(source);
|
String target = XidynUtils.extractBodyContent(source);
|
||||||
Assertions.assertThat(target).isEqualTo("hello");
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue