[AAF-23] Updated Copyright Headers for AAF Inno
[aaf/inno.git] / xgen / src / main / java / com / att / xgen / CacheGen.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\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
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.xgen;\r
24 \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
30 \r
31 import com.att.inno.env.APIException;\r
32 import com.att.inno.env.Env;\r
33 import com.att.inno.env.Trans;\r
34 import com.att.xgen.html.State;\r
35 import com.att.xgen.html.Thematic;\r
36 \r
37 \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
44 \r
45         \r
46         private ArrayList<Section<G>> sections = new ArrayList<Section<G>>();\r
47         private int flags;\r
48         private final Thematic thematic;\r
49 \r
50         public CacheGen(int flags, Code<G> code) throws APIException, IOException {\r
51                 this.flags = flags;\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
55                                 @Override\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
59                                 }\r
60                         },code);\r
61                 sections.add(buff.newSection());\r
62         \r
63                 // If Code implements thematic, set for later\r
64                 thematic = code instanceof Thematic?(Thematic)code:null;\r
65 \r
66         }\r
67         \r
68         public abstract G create(int htmlStyle, Writer w);\r
69 \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
72         }\r
73         \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
77                 }\r
78                 /* Theme\r
79                 trans.setTheme(theme);\r
80                 int htmlStyle = state.htmlVer(theme);\r
81                 */\r
82                 \r
83                 XGenBuff<G> buff = new XGenBuff<G>(flags,this);\r
84                 \r
85                 // forward\r
86                 int indent = 0;\r
87                 Section<G> s;\r
88                 int i=0;\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
95                         s.forward(w);\r
96                         s.setIndent(tempIndent);\r
97                         indent = tempIndent;\r
98                         reverse[i++]=s;\r
99                 }\r
100 \r
101                 for(--i;i>=0;--i) {\r
102                         reverse[i].back(w);\r
103                 }\r
104                 w.flush();\r
105         }\r
106         \r
107         private class Dynamic extends Section<G> {\r
108                 private Code<G> code;\r
109                 \r
110                 public Dynamic(int indent, Code<G> code) {\r
111                         this.code = code;\r
112                         this.indent = indent;\r
113                 }\r
114 \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
122                         } else {\r
123                                 buff.run((Cache<G>)Cache.Null.singleton(), code);\r
124                         }\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
129                         return d;\r
130                 }\r
131         }\r
132 }\r