Added helping methods and test. Improved root tree name.

This commit is contained in:
Christian P. MOMON 2020-01-18 10:42:35 +01:00
parent bdf194db05
commit 5b62d85bf0
4 changed files with 214 additions and 12 deletions

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.
*
@ -36,6 +36,8 @@ import java.io.Serializable;
*/
public class SimpleTagData implements Serializable, TagData
{
private static final long serialVersionUID = 8976245034682639923L;
public enum IterationStrategy
{
ONLY_FIRST_ROW,
@ -50,9 +52,7 @@ public class SimpleTagData implements Serializable, TagData
REPLACE,
APPEND,
IGNORE
}
private static final long serialVersionUID = 8976245034682639923L;;
};
private IterationStrategy iterationStrategy;
private TagAttributes attributes;
@ -87,6 +87,19 @@ public class SimpleTagData implements Serializable, TagData
this.iterationStrategy = IterationStrategy.ALL_ROWS;
}
/**
* Append attribute.
*
* @param label
* the label
* @param value
* the value
*/
public void appendAttribute(final String label, final String value)
{
this.attributes.appendAttribute(label, value);
}
/**
* Append content.
*
@ -169,6 +182,23 @@ public class SimpleTagData implements Serializable, TagData
return result;
}
/**
* Gets the attribute.
*
* @param label
* the label
* @return the attribute
*/
public String getAttribute(final String label)
{
String result;
result = this.attributes.getAttribute(label);
//
return result;
}
/**
* Iteration strategy.
*
@ -184,6 +214,19 @@ public class SimpleTagData implements Serializable, TagData
return result;
}
/**
* Sets the attribute.
*
* @param label
* the label
* @param value
* the value
*/
public void setAttribute(final String label, final String value)
{
this.attributes.put(label, value);
}
/**
* Sets the content.
*

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.
*
@ -74,6 +74,82 @@ public class TagDataManager
tags.add(tag);
}
/**
* Append attribute.
*
* @param id
* the id
* @param line
* the line
* @param label
* the label
* @param value
* the value
*/
public void appendAttribute(final String id, final int line, final String label, final long value)
{
appendAttribute(id, line, label, String.valueOf(value));
}
/**
* Append attribute.
*
* @param id
* the id
* @param line
* the line
* @param label
* the label
* @param value
* the value
*/
public void appendAttribute(final String id, final int line, final String label, final String value)
{
SimpleTagData tag = this.getIdData(id, line);
tag.attributes().appendAttribute(label, value);
}
/**
* Append attribute.
*
* @param id
* the id
* @param line
* the line
* @param column
* the column
* @param label
* the label
* @param value
* the value
*/
public void appendAttribute(final String id, final int line, final String column, final String label, final long value)
{
appendAttribute(id, line, column, label, String.valueOf(value));
}
/**
* Append attribute.
*
* @param id
* the id
* @param line
* the line
* @param column
* the column
* @param label
* the label
* @param value
* the value
*/
public void appendAttribute(final String id, final int line, final String column, final String label, final String value)
{
SimpleTagData tag = this.getIdData(id, line, column);
tag.attributes().appendAttribute(label, value);
}
/**
* Append attribute.
*
@ -174,6 +250,19 @@ public class TagDataManager
tag.appendContent(value);
}
/**
* Append content.
*
* @param id
* the id
* @param value
* the value
*/
public void appendContent(final String id, final long value)
{
appendContent(id, String.valueOf(value));
}
/**
* Append content.
*
@ -297,11 +386,11 @@ public class TagDataManager
}
/**
* Gets the ids data by id.
*
* @return the ids data by id
* Gets the root ids data.
*
* @return the root
*/
public TagDataListById getIdsDataById()
public TagDataListById getRoot()
{
TagDataListById result;

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.
*
@ -90,7 +90,7 @@ public class DomPresenterCore
*/
public static void dynamize(final Appendable result, final Document doc, final TagDataManager data) throws XidynException
{
dynamize(result, doc, data.getIdsDataById());
dynamize(result, doc, data.getRoot());
}
/**
@ -131,7 +131,7 @@ public class DomPresenterCore
{
StringBuffer result;
result = dynamize(doc, data.getIdsDataById());
result = dynamize(doc, data.getRoot());
//
return result;

View file

@ -0,0 +1,70 @@
/*
* 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;
import org.fest.assertions.Assertions;
import org.junit.Test;
import fr.devinsy.xidyn.presenters.PresenterUtils;
/**
* The Class TagDataManagerTest.
*/
public class TagDataManagerTest
{
/**
* Test 01.
*
* @throws Exception
* the exception
*/
@Test
public void test01() throws Exception
{
String source = "<ul><li id='line'>line 1</li></ul>";
TagDataManager data = new TagDataManager();
data.setContent("line", 0, "Alpha");
data.appendAttribute("line", 0, "style", "background: red;");
data.setContent("line", 1, "Bravo");
data.appendAttribute("line", 1, "style", "background: blue;");
String target = PresenterUtils.dynamize(source, data).toString();
String goal = "<ul><li style=\"background: red;\" id=\"line_0\">Alpha</li>\n<li style=\"background: blue;\" id=\"line_1\">Bravo</li>\n</ul>";
Assertions.assertThat(target).isEqualTo(goal);
}
@Test
public void test02() throws Exception
{
String source = "<table><tr id='line'><td id='col1'>id</td><td id='name'>name</td></tr></table>";
TagDataManager data = new TagDataManager();
data.setContent("line", 0, "col1", "Alpha");
data.appendAttribute("line", 0, "col1", "style", "background: red;");
data.setContent("line", 1, "Bravo");
data.appendAttribute("line", 1, "style", "background: blue;");
String target = PresenterUtils.dynamize(source, null).toString();
Assertions.assertThat(target).isEqualTo(source);
}
}