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