Home of: [Atelier "FUJIGURUMA"] >> [PageMixer hosted by SourceForge.net]

SEE "For Readers of English Version",
or Japanese version of this page

Inset data into token

This section explains how to inset your data into HTML tag element in PageMixer framework. And so, you can learn how to get "your data" to inset. Explanation uses the filter which inset user-name string as value of "value" attribute of "input" tag in HTML page.


<input name="Auth.UserName" value="">

Before processing

<input name="Auth.UserName" value="foozy">

After processing

Overview

Class diagram

Class diagram in this section is shown below:

Class diagram
Class diagram (click for large figure)

Classes which you must define are colored, and other are already defined.

Object diagram

Object diagram in this section is shown below:

Object diagram
Object diagram (click for large figure)

Sequence diagram

Sequence diagram in this section is shown below:

Sequence diagram
Sequence diagram (click for large figure)

Class names

In this tutorial, abbreviated class names are used. Complete names are shown below.

Classes of PageMixer framework

NotationFull name
ConsumerContext jp.ne.dti.lares.foozy.pagemixer.mixer.ConsumerContext
HashMapConsumerContext jp.ne.dti.lares.foozy.pagemixer.mixer.HashMapConsumerContext
HTMLSymbolSet jp.ne.dti.lares.foozy.pagemixer.HTMLSymbolSet
Symbol jp.ne.dti.lares.foozy.pagemixer.Symbol
Token jp.ne.dti.lares.foozy.pagemixer.Token
TokenDataInsetFilter jp.ne.dti.lares.foozy.pagemixer.mixer.TokenDataInsetFilter
TokenWatcher jp.ne.dti.lares.foozy.pagemixer.mixer.TokenWatcher

Tutorial specific classes

NotationFull name
Bootstrap.Default pagemixer.filter.Bootstrap.Default
UsernameAttrInsetFilter pagemixer.filter.UsernameAttrInsetFilter

Create TokenWatcher

As described above, this section let the filter inset user-name string as value of "value" attribute of "input" tag in HTML page. But, it is not good idea to inset it into all "input" tag in given HTML page, please image real service scenes.

Then, conditions of target Token are:

In this case, "TokenWatcher.NameAttr" is used as TokenWatcher. It is "Token Watcher watching at Name and Attribute of it".


new TokenWatcher.NameAttr(HTMLSymbolSet.SET.INPUT,
                          HTMLSymbolSet.SET.NAME,
                          "Auth.UserName")

TokenWatcher to inset into "INPUT"

Define identifier of value

This tutorial once described that "Consumer Context" provides functions to set/unset/get values.

But, to use those functions, you must define the identifier object to identify value. For example, the identifier object(= key) to set/get user-name string to inset into Token is defined as below.


Object key = "Auth.UserName";

Identifier to found use-name

It is useful to define key(s) in the class(or interface) which is visible from filter classes and data provider(or setting up) classes in real project.

Concretize TokenDataInsetFilter

PageMixer framework provides the utility classes which provide the template to inset data into the Token found by TokenWatcher given on construction, and these are TokenDataInsetFilter and its derived classes.

In the situation of this section, you must inset data as "HTML-Safe" value of attribute, because:

and, "TokenDataInsetFilter.HTMLSafeAttr" is good for this purpose.


public class UsernameAttrInsetFilter
    extends TokenDataInsetFilter.HTMLSafeAttr
{
    final static
    private HTMLSymbolSet SET = HTMLSymbolSet.SET;

    final static
    private String ATTR_VALUE = "Auth.UserName";

    ////////////////////////////////////////

    public UsernameAttrInsetFilter(Object keyUsername){
        super(new TokenWatcher.NameAttr(SET.INPUT, 
                                        SET.NAME,
                                        ATTR_VALUE),
              keyUsername,
              SET.VALUE);
    }

    ////////////////////////////////////////
    // Concretization of class HTMLSafeAttr

    protected String getValue(Object data){
        return (null != data ? data.toString() : "");
    }

}

Concretize TokenDataInsetFilter.HTMLSafeAttr

TokenDataInsetFilter.HTMLSafeAttr needs three arguments.

You do not need the explanation about the first of them, TokenWatcher, do you ? (or please see previous section again) The next argument, Object, is used as identifier(key) to identify value in ConsumerContext. And the last, Symbol, is used as name of "target attribute" into which the filter insets value.

NOTE: Since PageMixer 3.0, classes depending on Servlet API are separated from another (into 'servlet' package). This separation divides demonstration source files into one to explain general concept/principle of PageMixer, and another to explain how to build services up in Servlet environment.

Some sample classes seem to be not efficient because of this separation: the way to specify key, for example.

Connect and mix

Now, everything needed are ready to use. Execution code is as below (see UsernameAttrInsetFilter for detail).

try{
    // key to set/get user name
    final Object key = "Auth.UserName";
    // user name
    final String name = "foozy";

    Bootstrap bootstrap =
    new Bootstrap.Default(filename)
    {
        protected void prepare(ConsumerContext context)
        {
            // put user name into context
            context.setValue(key, name);
        }
    };

    // apply the filter
    bootstrap.execute(new UsernameAttrInsetFilter(key));
}
catch(Exception e){
    e.printStackTrace(System.err);
}
Connect and Mix

Sample HTML file as input is "auth.en.html" (or "index.en.html") under "src/demo/servlet/war/WEB-INF/page/demosite" in distribution.

Overriding "prepare(ConsumerContext)" of class Bootstrap.Default makes "bootstrap" object to invoke above processing before mixing with given filter at execution.


To next section "Trim sequence"