Refactored code (TagAttribute, DisplayMode for attribute, tests…)

This commit is contained in:
Christian P. MOMON 2020-01-18 17:09:08 +01:00
parent fb8cd7beef
commit f360eb89f4
8 changed files with 522 additions and 91 deletions

View file

@ -0,0 +1,29 @@
/*
* Copyright (C) 2006-2017,2020 Christian Pierre MOMON
*
* This file is part of Xidyn.
*
* Xidyn is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xidyn is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Xidyn. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.xidyn.data;
/**
* The Enum DisplayMode.
*/
public enum DisplayMode
{
REPLACE,
APPEND,
IGNORE
};

View file

@ -20,6 +20,8 @@ package fr.devinsy.xidyn.data;
import java.io.Serializable; import java.io.Serializable;
import fr.devinsy.strings.StringsUtils;
/** /**
* The Class SimpleTagData. * The Class SimpleTagData.
* *
@ -47,17 +49,10 @@ public class SimpleTagData implements Serializable, TagData
ALL_ROWS ALL_ROWS
} }
public enum MODE IterationStrategy iterationStrategy;
{
REPLACE,
APPEND,
IGNORE
};
private IterationStrategy iterationStrategy;
private TagAttributes attributes; private TagAttributes attributes;
private boolean excludeSection; private boolean excludeSection;
private MODE displayMode = MODE.REPLACE; private DisplayMode displayMode = DisplayMode.REPLACE;
private String content; private String content;
/** /**
@ -65,11 +60,7 @@ public class SimpleTagData implements Serializable, TagData
*/ */
public SimpleTagData() public SimpleTagData()
{ {
this.attributes = null; this(null);
this.excludeSection = false;
this.displayMode = MODE.REPLACE;
this.content = null;
this.iterationStrategy = IterationStrategy.ALL_ROWS;
} }
/** /**
@ -80,9 +71,9 @@ public class SimpleTagData implements Serializable, TagData
*/ */
public SimpleTagData(final String text) public SimpleTagData(final String text)
{ {
this.attributes = null; this.attributes = new TagAttributes();
this.excludeSection = false; this.excludeSection = false;
this.displayMode = MODE.REPLACE; this.displayMode = DisplayMode.REPLACE;
this.content = text; this.content = text;
this.iterationStrategy = IterationStrategy.ALL_ROWS; this.iterationStrategy = IterationStrategy.ALL_ROWS;
} }
@ -127,40 +118,20 @@ public class SimpleTagData implements Serializable, TagData
{ {
TagAttributes result; TagAttributes result;
if (this.attributes == null)
{
this.attributes = new TagAttributes();
}
result = this.attributes; result = this.attributes;
// //
return result; return result;
} }
/**
* Display.
*
* @return the string
*/
public String display()
{
String result;
result = this.content;
//
return result;
}
/** /**
* Display mode. * Display mode.
* *
* @return the mode * @return the mode
*/ */
public MODE displayMode() public DisplayMode displayMode()
{ {
MODE result; DisplayMode result;
result = this.displayMode; result = this.displayMode;
@ -189,11 +160,76 @@ public class SimpleTagData implements Serializable, TagData
* the label * the label
* @return the attribute * @return the attribute
*/ */
public String getAttribute(final String label) public TagAttribute getAttribute(final String label)
{
TagAttribute result;
result = this.attributes.getAttribute(label);
//
return result;
}
/**
* Gets the attribute mode.
*
* @param label
* the label
* @return the attribute mode
*/
public DisplayMode getAttributeMode(final String label)
{
DisplayMode result;
TagAttribute attribute = this.attributes.get(label);
if (attribute == null)
{
result = null;
}
else
{
result = attribute.getMode();
}
//
return result;
}
/**
* Gets the attribute value.
*
* @param label
* the label
* @return the attribute value
*/
public String getAttributeValue(final String label)
{ {
String result; String result;
result = this.attributes.getAttribute(label); TagAttribute attribute = this.attributes.get(label);
if (attribute == null)
{
result = null;
}
else
{
result = attribute.getValue();
}
//
return result;
}
/**
* Display.
*
* @return the string
*/
public String getContent()
{
String result;
result = this.content;
// //
return result; return result;
@ -224,7 +260,66 @@ public class SimpleTagData implements Serializable, TagData
*/ */
public void setAttribute(final String label, final String value) public void setAttribute(final String label, final String value)
{ {
this.attributes.put(label, value); if (this.attributes.containsKey(label))
{
this.attributes.get(label).setValue(value);
}
else
{
if (StringsUtils.containsAnyIgnoreCase(label, "style", "class"))
{
this.attributes.put(label, new TagAttribute(label, value, DisplayMode.APPEND));
}
else
{
this.attributes.put(label, new TagAttribute(label, value));
}
}
}
/**
* Sets the attribute.
*
* @param label
* the label
* @param value
* the value
* @param mode
* the mode
*/
public void setAttribute(final String label, final String value, final DisplayMode mode)
{
if (this.attributes.containsKey(label))
{
TagAttribute attribute = this.attributes.get(label);
attribute.setValue(value);
attribute.setMode(mode);
}
else
{
this.attributes.put(label, new TagAttribute(label, value, mode));
}
}
/**
* Sets the attribute mode.
*
* @param label
* the label
* @param mode
* the mode
*/
public void setAttributeMode(final String label, final DisplayMode mode)
{
if (this.attributes.containsKey(label))
{
this.attributes.get(label).setMode(mode);
}
else
{
TagAttribute tag = new TagAttribute(label, null, mode);
this.attributes.put(label, tag);
}
} }
/** /**
@ -244,7 +339,7 @@ public class SimpleTagData implements Serializable, TagData
* @param displayMode * @param displayMode
* the new display mode * the new display mode
*/ */
public void setDisplayMode(final MODE displayMode) public void setDisplayMode(final DisplayMode displayMode)
{ {
this.displayMode = displayMode; this.displayMode = displayMode;
} }

View file

@ -0,0 +1,113 @@
/*
* Copyright (C) 2020 Christian Pierre MOMON
*
* This file is part of Xidyn.
*
* Xidyn is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xidyn is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Xidyn. If not, see <http://www.gnu.org/licenses/>
*/
package fr.devinsy.xidyn.data;
/**
* The Class TagAttribute.
*/
public class TagAttribute
{
private DisplayMode mode;
private String label;
private String value;
/**
* Instantiates a new tag attribute.
*
* @param value
* the value
*/
public TagAttribute(final String label, final String value)
{
this(label, value, DisplayMode.REPLACE);
}
/**
* Instantiates a new tag attribute.
*
* @param label
* the label
* @param value
* the value
* @param mode
* the mode
*/
public TagAttribute(final String label, final String value, final DisplayMode mode)
{
this.mode = mode;
this.label = label;
this.value = value;
}
/**
* Append value.
*
* @param value
* the value
*/
public void appendValue(final String value)
{
if (this.value == null)
{
this.value = value;
}
else
{
this.value = this.value + " " + value;
}
}
public String getLabel()
{
return this.label;
}
public DisplayMode getMode()
{
return this.mode;
}
public String getValue()
{
return this.value;
}
/**
* Sets the mode.
*
* @param mode
* the new mode
*/
public void setMode(final DisplayMode mode)
{
if (mode == null)
{
throw new IllegalArgumentException("Null parameter.");
}
else
{
this.mode = mode;
}
}
public void setValue(final String value)
{
this.value = value;
}
}

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2006-2017 Christian Pierre MOMON * Copyright (C) 2006-2017,2020 Christian Pierre MOMON
* *
* This file is part of Xidyn. * This file is part of Xidyn.
* *
@ -19,6 +19,7 @@
package fr.devinsy.xidyn.data; package fr.devinsy.xidyn.data;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
/** /**
* The Class TagAttributes. * The Class TagAttributes.
@ -26,8 +27,7 @@ import java.util.HashMap;
* Note: no more AttrValue as in Brill, because the exception of style is * Note: no more AttrValue as in Brill, because the exception of style is
* managed in the attribute merging on the "style" string detection. * managed in the attribute merging on the "style" string detection.
*/ */
public class TagAttributes extends HashMap<String, TagAttribute> implements Iterable<TagAttribute>
public class TagAttributes extends HashMap<String, String>
{ {
private static final long serialVersionUID = 2802739066295665336L; private static final long serialVersionUID = 2802739066295665336L;
@ -62,11 +62,11 @@ public class TagAttributes extends HashMap<String, String>
{ {
if (this.containsKey(label)) if (this.containsKey(label))
{ {
this.put(label, this.get(label) + " " + value); this.get(label).appendValue(value);
} }
else else
{ {
this.put(label, value); this.put(label, new TagAttribute(label, value));
} }
} }
@ -77,9 +77,9 @@ public class TagAttributes extends HashMap<String, String>
* the label * the label
* @return the attribute * @return the attribute
*/ */
public String getAttribute(final String label) public TagAttribute getAttribute(final String label)
{ {
String result; TagAttribute result;
if (this.containsKey(label)) if (this.containsKey(label))
{ {
@ -95,15 +95,18 @@ public class TagAttributes extends HashMap<String, String>
} }
/** /**
* Sets the attribute. * Iterator.
* *
* @param label * @return the iterator
* the label
* @param value
* the value
*/ */
public void setAttribute(final String label, final String value) @Override
public Iterator<TagAttribute> iterator()
{ {
this.put(label, value); Iterator<TagAttribute> result;
result = this.values().iterator();
//
return result;
} }
} }

View file

@ -428,7 +428,7 @@ public class TagDataManager
{ {
SimpleTagData tag = this.getIdData(id); SimpleTagData tag = this.getIdData(id);
tag.attributes().setAttribute("class", DomPresenterCore.NODISPLAY_CLASS); tag.setAttribute("class", DomPresenterCore.NODISPLAY_CLASS);
} }
/** /**
@ -464,7 +464,7 @@ public class TagDataManager
{ {
SimpleTagData tag = this.getIdData(id, line); SimpleTagData tag = this.getIdData(id, line);
tag.attributes().setAttribute(label, value); tag.setAttribute(label, value);
} }
/** /**
@ -504,7 +504,7 @@ public class TagDataManager
{ {
SimpleTagData tag = this.getIdData(id, line, column); SimpleTagData tag = this.getIdData(id, line, column);
tag.attributes().setAttribute(label, value); tag.setAttribute(label, value);
} }
/** /**
@ -536,7 +536,7 @@ public class TagDataManager
{ {
SimpleTagData tag = this.getIdData(id); SimpleTagData tag = this.getIdData(id);
tag.attributes().setAttribute(label, value); tag.setAttribute(label, value);
} }
/** /**

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2006-2018 Christian Pierre MOMON * Copyright (C) 2006-2018,2020 Christian Pierre MOMON
* *
* This file is part of Xidyn. * This file is part of Xidyn.
* *
@ -73,7 +73,7 @@ class XidynDemo
} }
catch (Exception exception) catch (Exception exception)
{ {
System.out.println(exception.getMessage()); System.out.println("Demo failed: " + exception.getMessage());
exception.printStackTrace(); exception.printStackTrace();
} }
} }
@ -107,7 +107,8 @@ class XidynDemo
} }
catch (Exception exception) catch (Exception exception)
{ {
System.out.println(exception.getMessage()); System.out.println("Demo failed: " + exception.getMessage());
exception.printStackTrace();
} }
} }
@ -148,7 +149,8 @@ class XidynDemo
} }
catch (Exception exception) catch (Exception exception)
{ {
System.out.println(exception.getMessage()); System.out.println("Demo failed: " + exception.getMessage());
exception.printStackTrace();
} }
} }
@ -190,7 +192,8 @@ class XidynDemo
} }
catch (Exception exception) catch (Exception exception)
{ {
System.out.println(exception.getMessage()); System.out.println("Demo failed: " + exception.getMessage());
exception.printStackTrace();
} }
} }

View file

@ -34,7 +34,9 @@ import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import fr.devinsy.xidyn.XidynException; import fr.devinsy.xidyn.XidynException;
import fr.devinsy.xidyn.data.DisplayMode;
import fr.devinsy.xidyn.data.SimpleTagData; import fr.devinsy.xidyn.data.SimpleTagData;
import fr.devinsy.xidyn.data.TagAttribute;
import fr.devinsy.xidyn.data.TagAttributes; import fr.devinsy.xidyn.data.TagAttributes;
import fr.devinsy.xidyn.data.TagData; import fr.devinsy.xidyn.data.TagData;
import fr.devinsy.xidyn.data.TagDataListById; import fr.devinsy.xidyn.data.TagDataListById;
@ -349,26 +351,26 @@ public class DomPresenterCore
// Put model attributes in the merged attributes list. // Put model attributes in the merged attributes list.
if (dataAttributes != null) if (dataAttributes != null)
{ {
Iterator<Map.Entry<String, String>> iterator = dataAttributes.entrySet().iterator(); for (TagAttribute attribute : dataAttributes)
while (iterator.hasNext())
{ {
Map.Entry<String, String> attribute = iterator.next(); if (mergedAttributes.containsKey(attribute.getLabel()))
if (mergedAttributes.containsKey(attribute.getKey()))
{ {
if (attribute.getKey().equalsIgnoreCase("style")) if (attribute.getMode() == DisplayMode.APPEND)
{ {
mergedAttributes.put(attribute.getKey(), mergedAttributes.get(attribute.getKey()) + attribute.getValue()); mergedAttributes.put(attribute.getLabel(), mergedAttributes.get(attribute.getLabel()) + " " + attribute.getValue());
}
else if (attribute.getMode() == DisplayMode.REPLACE)
{
mergedAttributes.put(attribute.getLabel(), attribute.getValue());
} }
else else
{ {
mergedAttributes.put(attribute.getKey(), attribute.getValue()); // Keep the already merged value.
} }
} }
else else
{ {
mergedAttributes.put(attribute.getKey(), attribute.getValue()); mergedAttributes.put(attribute.getLabel(), attribute.getValue());
} }
} }
} }
@ -619,7 +621,7 @@ public class DomPresenterCore
{ {
SimpleTagData data = (SimpleTagData) dataCore; SimpleTagData data = (SimpleTagData) dataCore;
String theClass = data.attributes().getAttribute("class"); String theClass = data.getAttributeValue("class");
if ((theClass == null) || (!theClass.equals(NODISPLAY_CLASS))) if ((theClass == null) || (!theClass.equals(NODISPLAY_CLASS)))
{ {
@ -630,7 +632,7 @@ public class DomPresenterCore
// Build attributes. // Build attributes.
result.append(processAttributes(attrs, data.attributes(), suffix)); result.append(processAttributes(attrs, data.attributes(), suffix));
if (((node.getChildNodes() == null) || (node.getChildNodes().getLength() == 0)) && ((data == null) || (data.display() == null))) if (((node.getChildNodes() == null) || (node.getChildNodes().getLength() == 0)) && ((data == null) || (data.getContent() == null)))
{ {
// Close the tag. // Close the tag.
result.append(" />"); result.append(" />");
@ -639,16 +641,14 @@ public class DomPresenterCore
{ {
result.append('>'); result.append('>');
// CHANGED, cpm:
// Insert data. // Insert data.
if ((data == null) || (data.display() == null)) if ((data == null) || (data.getContent() == null))
{ {
processChildren(result, node, datas, suffix); processChildren(result, node, datas, suffix);
} }
else else
{ {
result.append(data.display()); result.append(data.getContent());
} }
// Close the tag. // Close the tag.
@ -662,20 +662,20 @@ public class DomPresenterCore
{ {
TagDataListByIndex tags = (TagDataListByIndex) dataCore; TagDataListByIndex tags = (TagDataListByIndex) dataCore;
int nbLines = tags.size(); int lineCount = tags.size();
for (int nLine = 0; nLine < nbLines; nLine++) for (int lineIndex = 0; lineIndex < lineCount; lineIndex++)
{ {
if (tags.elementAt(nLine) instanceof SimpleTagData) if (tags.elementAt(lineIndex) instanceof SimpleTagData)
{ {
SimpleTagData data = (SimpleTagData) tags.elementAt(nLine); SimpleTagData data = (SimpleTagData) tags.elementAt(lineIndex);
// Open the tag. // Open the tag.
result.append("<"); result.append("<");
result.append(node.getNodeName()); result.append(node.getNodeName());
result.append(processAttributes(attrs, data.attributes(), Integer.toString(nLine))); result.append(processAttributes(attrs, data.attributes(), Integer.toString(lineIndex)));
if (((node.getChildNodes() == null) || (node.getChildNodes().getLength() == 0)) && ((data == null) || (data.display() == null))) if (((node.getChildNodes() == null) || (node.getChildNodes().getLength() == 0)) && ((data == null) || (data.getContent() == null)))
{ {
// Close the tag. // Close the tag.
result.append(" />\n"); result.append(" />\n");
@ -687,13 +687,13 @@ public class DomPresenterCore
// CHANGED, cpm // CHANGED, cpm
// Insert data. // Insert data.
if ((data == null) || (data.display() == null)) if ((data == null) || (data.getContent() == null))
{ {
processChildren(result, node, datas, suffix); processChildren(result, node, datas, suffix);
} }
else else
{ {
result.append(data.display()); result.append(data.getContent());
} }
// Close the tag. // Close the tag.
@ -705,9 +705,9 @@ public class DomPresenterCore
else else
{ {
// Manage a Hashmap. // Manage a Hashmap.
TagDataListById data = (TagDataListById) tags.elementAt(nLine); TagDataListById data = (TagDataListById) tags.elementAt(lineIndex);
processElementWithId(result, node, attrs, idAttr, data, Integer.toString(nLine)); processElementWithId(result, node, attrs, idAttr, data, Integer.toString(lineIndex));
result.append('\n'); result.append('\n');
} }
} }

View file

@ -67,4 +67,192 @@ public class TagDataManagerTest
Assertions.assertThat(target).isEqualTo(source); Assertions.assertThat(target).isEqualTo(source);
} }
/**
* Test 03.
*
* @throws Exception
* the exception
*/
@Test
public void test03a() throws Exception
{
String source = "<span id='foo' class='bare'>FOOO</span>";
TagDataManager data = new TagDataManager();
data.setAttribute("foo", "class", "goodClass");
String target = PresenterUtils.dynamize(source, data).toString();
String goal = "<span id=\"foo\" class=\"bare goodClass\">FOOO</span>";
Assertions.assertThat(target).isEqualTo(goal);
}
/**
* Test 04.
*
* @throws Exception
* the exception
*/
@Test
public void test03b() throws Exception
{
String source = "<span id='foo' class='bare'>FOOO</span>";
TagDataManager data = new TagDataManager();
data.setAttribute("foo", "class", "goodClass");
data.getIdData("foo").setAttributeMode("class", DisplayMode.APPEND);
String target = PresenterUtils.dynamize(source, data).toString();
String goal = "<span id=\"foo\" class=\"bare goodClass\">FOOO</span>";
Assertions.assertThat(target).isEqualTo(goal);
}
/**
* Test 05.
*
* @throws Exception
* the exception
*/
@Test
public void test03c() throws Exception
{
String source = "<span id='foo' class='bare'>FOOO</span>";
TagDataManager data = new TagDataManager();
data.setAttribute("foo", "class", "goodClass");
data.getIdData("foo").setAttributeMode("class", DisplayMode.REPLACE);
String target = PresenterUtils.dynamize(source, data).toString();
String goal = "<span id=\"foo\" class=\"goodClass\">FOOO</span>";
Assertions.assertThat(target).isEqualTo(goal);
}
/**
* Test 06.
*
* @throws Exception
* the exception
*/
@Test
public void test06a() throws Exception
{
String source = "<span id='foo' style='width: 100px;'>FOOO</span>";
TagDataManager data = new TagDataManager();
data.setAttribute("foo", "style", "goodStyle;");
String target = PresenterUtils.dynamize(source, data).toString();
String goal = "<span style=\"width: 100px; goodStyle;\" id=\"foo\">FOOO</span>";
Assertions.assertThat(target).isEqualTo(goal);
}
@Test
public void test06b() throws Exception
{
String source = "<span id='foo' style='width: 100px;'>FOOO</span>";
TagDataManager data = new TagDataManager();
data.setAttribute("foo", "style", "goodStyle;");
data.getIdData("foo").setAttributeMode("style", DisplayMode.APPEND);
String target = PresenterUtils.dynamize(source, data).toString();
String goal = "<span style=\"width: 100px; goodStyle;\" id=\"foo\">FOOO</span>";
Assertions.assertThat(target).isEqualTo(goal);
}
/**
* Test 06 c.
*
* @throws Exception
* the exception
*/
@Test
public void test06c() throws Exception
{
String source = "<span id='foo' style='width: 100px;'>FOOO</span>";
TagDataManager data = new TagDataManager();
data.setAttribute("foo", "style", "goodStyle;");
data.getIdData("foo").setAttributeMode("style", DisplayMode.REPLACE);
String target = PresenterUtils.dynamize(source, data).toString();
String goal = "<span style=\"goodStyle;\" id=\"foo\">FOOO</span>";
Assertions.assertThat(target).isEqualTo(goal);
}
/**
* Test 9a.
*
* @throws Exception
* the exception
*/
@Test
public void test09a() throws Exception
{
String source = "<span id='foo' name='bare'>FOOO</span>";
TagDataManager data = new TagDataManager();
data.setAttribute("foo", "name", "good;");
String target = PresenterUtils.dynamize(source, data).toString();
String goal = "<span name=\"good;\" id=\"foo\">FOOO</span>";
Assertions.assertThat(target).isEqualTo(goal);
}
/**
* Test 9 b.
*
* @throws Exception
* the exception
*/
@Test
public void test09b() throws Exception
{
String source = "<span id='foo' name='bare'>FOOO</span>";
TagDataManager data = new TagDataManager();
data.setAttribute("foo", "name", "good;");
data.getIdData("foo").setAttributeMode("name", DisplayMode.REPLACE);
String target = PresenterUtils.dynamize(source, data).toString();
String goal = "<span name=\"good;\" id=\"foo\">FOOO</span>";
Assertions.assertThat(target).isEqualTo(goal);
}
/**
* Test 9 c.
*
* @throws Exception
* the exception
*/
@Test
public void test09c() throws Exception
{
String source = "<span id='foo' name='bare'>FOOO</span>";
TagDataManager data = new TagDataManager();
data.setAttribute("foo", "name", "good;");
data.getIdData("foo").setAttributeMode("name", DisplayMode.APPEND);
String target = PresenterUtils.dynamize(source, data).toString();
String goal = "<span name=\"bare good;\" id=\"foo\">FOOO</span>";
Assertions.assertThat(target).isEqualTo(goal);
}
} }