summaryrefslogtreecommitdiffstats
path: root/xgen/src/main/java/org/onap/aaf/xgen/CacheGen.java
blob: 3a8957a67af314b449d59cfc3aca4b89f806b5eb (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package org.onap.aaf.xgen;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;

import org.onap.aaf.inno.env.APIException;
import org.onap.aaf.inno.env.Env;
import org.onap.aaf.inno.env.Trans;
import org.onap.aaf.xgen.html.State;
import org.onap.aaf.xgen.html.Thematic;


public abstract class CacheGen<G extends XGen<G>> {
	public static final int NO_FLAGS = 0x0;
	public final static int PRETTY	= 0x1;
	public final static int XML		= 0x2;
	public final static int HTML4 	= 0x4;
	public final static int HTML5 	= 0x8;

	
	private ArrayList<Section<G>> sections = new ArrayList<Section<G>>();
	private int flags;
	private final Thematic thematic;

	public CacheGen(int flags, Code<G> code) throws APIException, IOException {
		this.flags = flags;
		final XGenBuff<G> buff = new XGenBuff<G>(flags,this);
		// Run to gather Strings and Code Class Segments
		buff.run(new Cache<G>() {
				@Override
				public void dynamic(G hgen, Code<G> code) {
					sections.add(buff.newSection());
					sections.add(new Dynamic(hgen.getIndent(),code));
				}
			},code);
		sections.add(buff.newSection());
	
		// If Code implements thematic, set for later
		thematic = code instanceof Thematic?(Thematic)code:null;

	}
	
	public abstract G create(int htmlStyle, Writer w);

	public void replay(State<Env> state, Trans trans, OutputStream os, String theme) throws IOException, APIException {
		replay(state, trans, new OutputStreamWriter(os), theme);
	}
	
	public void replay(State<Env> state, Trans trans,Writer w, String theme) throws IOException, APIException {
		if(thematic!=null) {
			theme = thematic.themeResolve(theme);
		}
		/* Theme
		trans.setTheme(theme);
		int htmlStyle = state.htmlVer(theme);
		*/
		
		XGenBuff<G> buff = new XGenBuff<G>(flags,this);
		
		// forward
		int indent = 0;
		Section<G> s;
		int i=0;
		@SuppressWarnings("unchecked")
		Section<G>[] reverse = new Section[sections.size()];
		for(Section<G> section : sections) {
			s = section.use(state, trans, buff); // note, doesn't change cached, only dynamic, which is created for thread
			int tempIndent = s.getIndent();
			s.setIndent(indent);
			s.forward(w);
			s.setIndent(tempIndent);
			indent = tempIndent;
			reverse[i++]=s;
		}

		for(--i;i>=0;--i) {
			reverse[i].back(w);
		}
		w.flush();
	}
	
	private class Dynamic extends Section<G> {
		private Code<G> code;
		
		public Dynamic(int indent, Code<G> code) {
			this.code = code;
			this.indent = indent;
		}

		@SuppressWarnings("unchecked")
		public Section<G> use(State<Env> state, Trans trans, XGenBuff<G> buff) throws APIException, IOException {
			// Clone Dynamic to make Thread Safe
			Dynamic d = new Dynamic(indent,code);
			buff.setIndent(indent);
			if(code instanceof DynamicCode) {
				buff.run(state,trans,Cache.Null.singleton(), (DynamicCode<G,?,? extends Trans>)code);
			} else {
				buff.run((Cache<G>)Cache.Null.singleton(), code);
			}
			Section<G> s = buff.newSection();
			d.indent = s.indent;
			d.forward = s.forward;
			d.backward = s.backward;
			return d;
		}
	}
}