Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package org.litesoft.template;

import java.util.*;

public class Template implements Chunk
{
    public static final String KEY_PREFIX = ".{";
    public static final String KEY_POSTFIX = "}.";

    private final Chunk[] mChunks;

    public Template( Chunk[] pChunks )
    {
        mChunks = pChunks;
    }

    @Override
    public void dump( Sink pWriter, ValueProvider pValueProvider )
    {
        for ( Chunk zChunk : mChunks )
        {
            zChunk.dump( pWriter, pValueProvider );
        }
    }

    public static Template from( String pText )
    {
        List<Chunk> zChunks = new ArrayList<>();

        int zFrom = 0;
        for ( int zAt; -1 != (zAt = pText.indexOf( KEY_POSTFIX, zFrom )); zFrom = zAt + KEY_POSTFIX.length() )
        {
            int zPrefixAt = pText.lastIndexOf( KEY_PREFIX, zAt );
            if ( zPrefixAt != -1 )
            {
                int zKeyAt = zPrefixAt + KEY_PREFIX.length();
                if ( zKeyAt != zAt )
                {
                    addTo( zChunks, pText, zFrom, zPrefixAt ); // Before the Key
                    zChunks.add( new ChunkWithKey( pText.substring( zKeyAt, zAt ) ) ); // the Key
                    continue;
                }
            }
            addTo( zChunks, pText, zFrom, zAt + KEY_POSTFIX.length() ); // Malformed
        }
        addTo( zChunks, pText, zFrom, pText.length() ); // The Rest...

        return new Template( zChunks.toArray( new Chunk[zChunks.size()] ) );
    }

    private static void addTo( List<Chunk> pChunks, String pText, int pFrom, int pUpTo )
    {
        if ( pFrom != pUpTo )
        {
            pChunks.add( new ChunkForText( pText.substring( pFrom, pUpTo ) ) );
        }
    }
}

Commits for litesoft/trunk/DeviceDesktopTest/src/org/litesoft/template/Template.java

Diff revisions: vs.
Revision Author Commited Message
943 Diff Diff GeorgeS picture GeorgeS Tue 03 Jun, 2014 04:25:50 +0000

Extracting commonfoundation

939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

927 GeorgeS picture GeorgeS Mon 17 Mar, 2014 03:58:40 +0000

Template system and index.html as a resource...