2 * ============LICENSE_START====================================================
\r
4 * ===========================================================================
\r
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
\r
6 * ===========================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END====================================================
\r
22 package org.onap.aaf.misc.xgen;
\r
24 import java.io.IOException;
\r
25 import java.io.OutputStream;
\r
26 import java.io.OutputStreamWriter;
\r
27 import java.io.Writer;
\r
28 import java.util.ArrayList;
\r
30 import org.onap.aaf.misc.env.APIException;
\r
31 import org.onap.aaf.misc.env.Env;
\r
32 import org.onap.aaf.misc.env.Trans;
\r
33 import org.onap.aaf.misc.xgen.html.State;
\r
34 import org.onap.aaf.misc.xgen.html.Thematic;
\r
37 public abstract class CacheGen<G extends XGen<G>> {
\r
38 public static final int NO_FLAGS = 0x0;
\r
39 public final static int PRETTY = 0x1;
\r
40 public final static int XML = 0x2;
\r
41 public final static int HTML4 = 0x4;
\r
42 public final static int HTML5 = 0x8;
\r
45 private ArrayList<Section<G>> sections = new ArrayList<Section<G>>();
\r
47 private final Thematic thematic;
\r
49 public CacheGen(int flags, Code<G> code) throws APIException, IOException {
\r
51 final XGenBuff<G> buff = new XGenBuff<G>(flags,this);
\r
52 // Run to gather Strings and Code Class Segments
\r
53 buff.run(new Cache<G>() {
\r
55 public void dynamic(G hgen, Code<G> code) {
\r
56 sections.add(buff.newSection());
\r
57 sections.add(new Dynamic(hgen.getIndent(),code));
\r
60 sections.add(buff.newSection());
\r
62 // If Code implements thematic, set for later
\r
63 thematic = code instanceof Thematic?(Thematic)code:null;
\r
67 public abstract G create(int htmlStyle, Writer w);
\r
69 public void replay(State<Env> state, Trans trans, OutputStream os, String theme) throws IOException, APIException {
\r
70 replay(state, trans, new OutputStreamWriter(os), theme);
\r
73 public void replay(State<Env> state, Trans trans,Writer w, String theme) throws IOException, APIException {
\r
74 if(thematic!=null) {
\r
75 theme = thematic.themeResolve(theme);
\r
78 trans.setTheme(theme);
\r
79 int htmlStyle = state.htmlVer(theme);
\r
82 XGenBuff<G> buff = new XGenBuff<G>(flags,this);
\r
88 @SuppressWarnings("unchecked")
\r
89 Section<G>[] reverse = new Section[sections.size()];
\r
90 for(Section<G> section : sections) {
\r
91 s = section.use(state, trans, buff); // note, doesn't change cached, only dynamic, which is created for thread
\r
92 int tempIndent = s.getIndent();
\r
93 s.setIndent(indent);
\r
95 s.setIndent(tempIndent);
\r
96 indent = tempIndent;
\r
100 for(--i;i>=0;--i) {
\r
101 reverse[i].back(w);
\r
106 private class Dynamic extends Section<G> {
\r
107 private Code<G> code;
\r
109 public Dynamic(int indent, Code<G> code) {
\r
111 this.indent = indent;
\r
114 @SuppressWarnings("unchecked")
\r
115 public Section<G> use(State<Env> state, Trans trans, XGenBuff<G> buff) throws APIException, IOException {
\r
116 // Clone Dynamic to make Thread Safe
\r
117 Dynamic d = new Dynamic(indent,code);
\r
118 buff.setIndent(indent);
\r
119 if(code instanceof DynamicCode) {
\r
120 buff.run(state,trans,Cache.Null.singleton(), (DynamicCode<G,?,? extends Trans>)code);
\r
122 buff.run((Cache<G>)Cache.Null.singleton(), code);
\r
124 Section<G> s = buff.newSection();
\r
125 d.indent = s.indent;
\r
126 d.forward = s.forward;
\r
127 d.backward = s.backward;
\r