TIPS

loadHtmlTempate() or checkAndLoadHtmlTemplate()

There seems to be two method to load xhtml template. You may choice them to the requirements.

loadHtmlTemplate()

loadHtmlTemplate() loads xhtml template and returns instance of org.mixer2.jaxb.xhtml.Html . If fail to parse xhtml, return null.

checkAndLoadHtmlTemplate()

checkAndLoadHtmlTemplate() loads xhtml template and returns instance of org.mixer2.jaxb.xhtml.Html . If fail to parse xhtml, throws exception. You can use checkAndLoadHtmlTempate() on version 1.2.8 or later.

commonly-used method

When you code view by mixer2, you use commonly methods that is implemented on all the html tags.

All common method is implemented on org.mixer.xhtml.AbstractJaxb class. See javaDoc for details.

attribute manipulation method

get*, set* and isSet* methods.

// on template, <div id="foo" onClick="bar();">xxxxx</div>
Div div = html.getById("foo", Div.class);
boolean b = div.isSetOnClick(); // you get true
String s = div.getOnClick(); // you get "bar();"
div.setId("foo2");
div.setOnMouseover("bar2();")
// you get <div id="foo2" onClick="bar();" onMouseover="bar2();">xxxxx</div>

unset* methods.

// on template, <div id="foo" onClick="bar();">xxxxx</div>
Div div = html.getById("foo", Div.class);
div.unsetId();
div.unsetOnClick();
div.unsetContent();
div.getContent().add("hello");
// you get <div>hello</div>

get tag

Example belows is not enough. See javadoc for details.

NOTICE! series method for "GET" returns a object by reference, not copy.

using id property

// scan inside of html for specified tag and get div tag that has "foo" id property.
// get null if missing.
Div div = html.getById("foo", Div.class);

// go on to scan the div tag and get span tag that has "bar" id property.
// get null if missing.
Span span = div.getById("bar", Span.class);

// one liner
Span span = html.getById("foo",Div.class).getById("bar", Span.class);

// In html specification, the value of id property is unique in one web page.
// So, actually, you need only the code like below.
Span span = html.getById("bar", Span.class);

// add string to div tag like <div id="foo">aaa</div>
html.getById("foo", Div.class).getContent().add("bbb");
// you get <div id="foo">aaa bbb</div>

// delete all content of div tag and add new content.
html.getById("foo", Div.class).unsetContent();
html.getById("foo", Div.class).getContent().add("bbb");
// you get<div id="foo">bbb</div>

// The same effect by replaceInner()
html.getById("foo", Div.class).replaceInner("bbb");
// you get<div id="foo">bbb</div>

using class property

// scan inside of html for specified div tag that has "foo" id property.
Div div = html.getById("foo", Div.class);

// scan inside of div tag for span tag that has "bar" class property and make list of span tag
List<Span> spanList = div.getDescendants("bar", Span.class);

// one liner
List<Span> spanList = html.getById("foo", Div.class).getDescendants("bar", Span.class);

// add class property "zzz" to all the span tag in List.
// you get <span class="bar zzz">...</span>
for (Span span : spanList) {
    span.addCssclass("zzz"); //
}

delete tag

see javadoc for details

// scan inside of html for div tag that has "foo" id property and delete it.
// if deleted successfully, return true.
boolean result = html.removeById("foo", Div.class);

// delete all span tags in div tag that has "foo" id property.
html.getById("foo", Div.class).removeDescendants(Span.class);

// delete every tags that has "bar" class property in div tag having "foo" id property.
html.getById("foo", Div.class).removeDescendants("bar");

// delete span tags that has "bar" class property in div tag having "foo" id property.
html.getById("foo", Div.class).removeDescendants("bar", Span.class);

create tag newly

// create anchor link likes <a id="foo" href="example.html">example</a>
A a = new A();
a.setid("foo");
a.setHref("example.html");
a.getContent().add("example");

// create div tag and install anchor link above.
Div div = new Div();
div.getContent.add(a);
// you get <div><a id="foo" href="example.html">example</a></div>

short cut method for create tag.

import static org.mixer2.xhtml.TagCreator.*;

Div div = div();
// same as
// Div div = new Div();

Div div = divWithId("foo");
// same as
// Div div = new Div(); div.setId("foo");

copy tag

copy() method create deep copy of the tag instance.

template:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="hellomsg">Hello World !</div>
</body>
</html>

code:

Html html = mixer2Engine.loadHtmlTemplate(new File("HelloWorld.html"));
Div helloWorldDiv = html.getById("hellomsg",Div.class)
Div newDiv = helloWorldDiv.copy(Div.class);
newDiv.setId("bar");
newDiv.replaceInner("Life is beautiful");
html.getBody().add(newDiv);
System.out.println(html.saveToString());

output:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
</head>
<body>
<div id="hellomsg">Hello World !</div>
<div id="bar">Life is beautiful.</div>
</body>
</html>

replace

replace() method use deep copy of replacer object. see javadoc for details

// tag (no matter of variation) will be replace by p tag.
// if missing, return false;
P p = new P();
p.getContent.add("abc");
Div target = html.getDescendants(Div.class).get(0);
boolean result = html.replace(target, p);

// tag (no matter of variation) that has "id" property will be replace by span tag.
// if missing, return false;
Span span = new Span();
span.getContent.add("abc");
boolean result = html.replaceById("foo", span);

replaceInner

replaceInner() method does NOT use deep copy of replacer object. see javadoc for details

insert

template:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="div_main">
    <div id="div_a">aaaa</div>
</div>
</body>
</html>

insert new span tag.

import static org.mixer2.xhtml.TagCreator.*;

Span span = span();
span.getContent.add("insert!");
html.insertAfterId("div_a");

output

<div id="div_main">
    <div id="div_a">aaaa</div>
    <span>insert!</span>
</div>

type cast

According to tag variation, you need cast. For example, tr tag can have th or td. You do not know what you will get td or th until get it.

Flow flow = table.getTr().get(0).getThOrTd().get(0);

Flow type is abstract. You need cast to Td or Th type. If you have pre-knowledge about the type that will be return by getThOrTd() method, you can use cast() method.

// you get first td of first tr of a table.
Td td = table.getTr().get(0).getThOrTd().get(0).cast(Td.class);

// the code above is same below.
Td td = (Td) table.getTr().get(0).getThOrTd().get(0);

loop

You can use loop syntax of java.

template:

<ul id="namelist">
    <li class="foo">here comes name.</li>
    <li class="foo">here comes name.</li>
</ul>

In this example, get first li and copy it.

ArrayList<String> nameArray = new ArrayList<String>();
nameArray.add("Michel Jackson");
nameArray.add("Lady GAGA");

Ul ul = html.getById("namelist", Ul.class);

// get first li and copy. remove content of it.
// property of li tag will remain.
Li baseLi = ul.getLi().get(0).copy(Li.class);
baseLi.unsetContent();

// delete all li
ul.getLi().clear();

// loop and fill li
for (String name : nameArray) {
    Li li = baseLi.copy();
    li.getContent().add(name);
    ul.getLi().add(li);
}

output

<ul id="namelist">
    <li class="foo">Michel Jackson</li>
    <li class="foo">Lady GAGA</li>
</ul>

partially marshal

You can get specified tag but template must have from <html> to </html> .

template

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<div id="foo">
abc
</div>
</body>
</html>

code

html = m2e.loadHtmlTemplate(new File("test.html"));
Div div = html.getById("foo", Div.class);
Span span = new Span();
span.getContent().add("def");
div.getContent().add(span);
StringWriter sw = new StringWriter();
m2e.saveToStringWriter(div, sw);
System.out.println(sw.toString());

output

<div id="foo">
abc
<span>def</span>
</div>

path adjuster

The static file path may change if you open template directly in browser and the run-time on server.

You can use PathAdjuster class. See javadoc for detail.

XMLStringUtil

The characters for XML(XHTML) is strictly determined by w3c. XMLStringUtil class provide some function. javadoc for detail.