1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.xgen;
\r
25 import java.io.IOException;
\r
26 import java.io.OutputStream;
\r
27 import java.io.OutputStreamWriter;
\r
28 import java.io.Writer;
\r
29 import java.util.ArrayList;
\r
31 import org.onap.aaf.inno.env.APIException;
\r
32 import org.onap.aaf.inno.env.Env;
\r
33 import org.onap.aaf.inno.env.Trans;
\r
34 import org.onap.aaf.xgen.html.State;
\r
35 import org.onap.aaf.xgen.html.Thematic;
\r
38 public abstract class CacheGen<G extends XGen<G>> {
\r
39 public static final int NO_FLAGS = 0x0;
\r
40 public final static int PRETTY = 0x1;
\r
41 public final static int XML = 0x2;
\r
42 public final static int HTML4 = 0x4;
\r
43 public final static int HTML5 = 0x8;
\r
46 private ArrayList<Section<G>> sections = new ArrayList<Section<G>>();
\r
48 private final Thematic thematic;
\r
50 public CacheGen(int flags, Code<G> code) throws APIException, IOException {
\r
52 final XGenBuff<G> buff = new XGenBuff<G>(flags,this);
\r
53 // Run to gather Strings and Code Class Segments
\r
54 buff.run(new Cache<G>() {
\r
56 public void dynamic(G hgen, Code<G> code) {
\r
57 sections.add(buff.newSection());
\r
58 sections.add(new Dynamic(hgen.getIndent(),code));
\r
61 sections.add(buff.newSection());
\r
63 // If Code implements thematic, set for later
\r
64 thematic = code instanceof Thematic?(Thematic)code:null;
\r
68 public abstract G create(int htmlStyle, Writer w);
\r
70 public void replay(State<Env> state, Trans trans, OutputStream os, String theme) throws IOException, APIException {
\r
71 replay(state, trans, new OutputStreamWriter(os), theme);
\r
74 public void replay(State<Env> state, Trans trans,Writer w, String theme) throws IOException, APIException {
\r
75 if(thematic!=null) {
\r
76 theme = thematic.themeResolve(theme);
\r
79 trans.setTheme(theme);
\r
80 int htmlStyle = state.htmlVer(theme);
\r
83 XGenBuff<G> buff = new XGenBuff<G>(flags,this);
\r
89 @SuppressWarnings("unchecked")
\r
90 Section<G>[] reverse = new Section[sections.size()];
\r
91 for(Section<G> section : sections) {
\r
92 s = section.use(state, trans, buff); // note, doesn't change cached, only dynamic, which is created for thread
\r
93 int tempIndent = s.getIndent();
\r
94 s.setIndent(indent);
\r
96 s.setIndent(tempIndent);
\r
97 indent = tempIndent;
\r
101 for(--i;i>=0;--i) {
\r
102 reverse[i].back(w);
\r
107 private class Dynamic extends Section<G> {
\r
108 private Code<G> code;
\r
110 public Dynamic(int indent, Code<G> code) {
\r
112 this.indent = indent;
\r
115 @SuppressWarnings("unchecked")
\r
116 public Section<G> use(State<Env> state, Trans trans, XGenBuff<G> buff) throws APIException, IOException {
\r
117 // Clone Dynamic to make Thread Safe
\r
118 Dynamic d = new Dynamic(indent,code);
\r
119 buff.setIndent(indent);
\r
120 if(code instanceof DynamicCode) {
\r
121 buff.run(state,trans,Cache.Null.singleton(), (DynamicCode<G,?,? extends Trans>)code);
\r
123 buff.run((Cache<G>)Cache.Null.singleton(), code);
\r
125 Section<G> s = buff.newSection();
\r
126 d.indent = s.indent;
\r
127 d.forward = s.forward;
\r
128 d.backward = s.backward;
\r