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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.util.template;

import java.io.*;
import java.util.*;

import org.litesoft.core.typeutils.*;

public abstract class AbstractTC
{
    private Map<String, TCsub> mSubstitutions = new HashMap<String, TCsub>();

    protected AbstractTC( Collection<TCsub> pSubstitutions )
    {
        if ( pSubstitutions != null )
        {
            for ( TCsub zSubstitution : pSubstitutions )
            {
                if ( zSubstitution != null )
                {
                    mSubstitutions.put( zSubstitution.getReplacementKey(), zSubstitution );
                }
            }
        }
    }

    public void add( String pKey, String pReplacementText )
    {
        if ( null != (pKey = Strings.noEmpty( pKey )) )
        {
            add( new StringTCsub( pKey, pReplacementText ) );
        }
    }

    public void add( TCsub pTCsub )
    {
        if ( pTCsub != null )
        {
            mSubstitutions.put( pTCsub.getReplacementKey(), pTCsub );
        }
    }

    protected String processSubstitutionsFrom( String pTextWithSubstututionRefs, Writer pWriter, String pReplaceNotFoundsWith, Object pReplaceFrom )
            throws IOException
    {
        for ( int zAt; -1 != (zAt = pTextWithSubstututionRefs.indexOf( TCsub.KEY_INIT )); )
        {
            if ( zAt != 0 )
            {
                pWriter.write( pTextWithSubstututionRefs, 0, zAt );
                pTextWithSubstututionRefs = pTextWithSubstututionRefs.substring( zAt );
            }
            if ( -1 == (zAt = pTextWithSubstututionRefs.indexOf( TCsub.KEY_FINI )) )
            {
                return pTextWithSubstututionRefs;
            }
            String zKey = pTextWithSubstututionRefs.substring( TCsub.LEN_INIT, zAt );
            pTextWithSubstututionRefs = pTextWithSubstututionRefs.substring( zAt + TCsub.LEN_FINI );
            TCsub zSub = mSubstitutions.get( zKey );
            if ( zSub != null )
            {
                zSub.replace( pWriter, pReplaceNotFoundsWith, pReplaceFrom );
            }
            else if ( pReplaceNotFoundsWith != null )
            {
                pWriter.write( pReplaceNotFoundsWith );
            }
            else
            {
                pWriter.write( TCsub.NOT_FOUND_INIT + zKey + TCsub.NOT_FOUND_FINI );
            }
        }
        if ( pTextWithSubstututionRefs.length() > TCsub.LEN_INIT )
        {
            int zAt = pTextWithSubstututionRefs.length() - TCsub.LEN_INIT;
            pWriter.write( pTextWithSubstututionRefs.substring( 0, zAt ) );
            return pTextWithSubstututionRefs.substring( zAt );
        }
        pWriter.write( pTextWithSubstututionRefs );
        return "";
    }

    public void dispose()
    {
        if ( !mSubstitutions.isEmpty() )
        {
            for ( TCsub zSub : mSubstitutions.values() )
            {
                zSub.dispose();
            }
            mSubstitutions = Collections.emptyMap();
        }
    }

    protected static Set<TCsub> makeSet( String... pNameValues )
            throws IllegalArgumentException
    {
        Set<TCsub> rv = new HashSet<TCsub>();
        if ( Objects.isNotNullOrEmpty( pNameValues ) )
        {
            if ( (pNameValues.length & 1) != 0 )
            {
                throw new IllegalArgumentException( "Attempt to add an unpaired Name/Value pair" );
            }
            for ( int i = 0; i < pNameValues.length; i += 2 )
            {
                rv.add( new StringTCsub( pNameValues[i], pNameValues[i + 1] ) );
            }
        }
        return rv;
    }

    protected static Set<TCsub> makeSet( TCsub... pTCsubs )
            throws IllegalArgumentException
    {
        return new HashSet<TCsub>( Arrays.asList( pTCsubs ) );
    }
}

Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/util/template/AbstractTC.java

Diff revisions: vs.
Revision Author Commited Message
809 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 04:10:46 +0000
801 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 03:59:02 +0000
50 Diff Diff GeorgeS picture GeorgeS Tue 13 Apr, 2010 11:51:38 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000