OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
t_html_generator.cc
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  * Copyright 2006-2017 The Apache Software Foundation.
20  * https://github.com/apache/thrift
21  */
22 
23 #include <string>
24 #include <fstream>
25 #include <iostream>
26 #include <vector>
27 #include <map>
28 
29 #include <stdlib.h>
30 #include <sys/stat.h>
31 #include <sstream>
32 #include "t_generator.h"
33 #include "platform.h"
34 using namespace std;
35 
36 
42 class t_html_generator : public t_generator {
43  public:
45  t_program* program,
46  const std::map<std::string, std::string>& parsed_options,
47  const std::string& option_string)
48  : t_generator(program)
49  {
50  (void) parsed_options;
51  (void) option_string;
52  out_dir_base_ = "gen-html";
53  }
54 
55  void generate_program();
56  void generate_xml();
57 
61  void generate_field_xml(t_field * tfl);
62  void generate_struct_xml(const string& stname);
63  void generate_sandesh_xml(t_sandesh* tsnd);
64  void generate_typedef (t_typedef* ttypedef) {}
65  void generate_enum (t_enum* tenum) {}
66  void generate_const (t_const* tconst) {}
67  void generate_struct (t_struct* tstruct) {}
68 #ifdef SANDESH
69  void generate_sandesh (t_sandesh* tsandesh) {}
70 #endif
71  void generate_service (t_service* tservice) {}
72 
73  std::ofstream f_out_;
74 };
75 
76 void t_html_generator::generate_struct_xml(const string &stname) {
77  vector<t_struct*> objects = program_->get_objects();
78  vector<t_struct*>::iterator o_iter;
79  for (o_iter = objects.begin(); o_iter != objects.end(); ++o_iter) {
80  t_struct* tstruct = *o_iter;
81  const string & sname = tstruct->get_name();
82  if (0 == sname.compare(stname)){
83  f_out_ << "<" << sname << ">";
84  vector<t_field*> members = tstruct->get_members();
85  vector<t_field*>::iterator mem_iter = members.begin();
86  for ( ; mem_iter != members.end(); mem_iter++) {
87  generate_field_xml(*mem_iter);
88  }
89  f_out_ << "</" << sname << ">";
90  }
91  }
92 }
93 
95  string fdname = tfl->get_name();
96  t_type * fdtype = tfl->get_type();
97  string fdtname = fdtype->get_name();
98 
99  if (fdtype->is_base_type()) {
100  f_out_ << "<" << fdname << " type=\"" << fdtype->get_name() <<
101  "\">0</" << fdname << ">";
102  } else if (fdtype->is_list()) {
103 #if 0
104  t_list * tl = (t_list*) fdtype;
105  if (tl->get_elem_type()->is_struct()) {
106  t_struct * ts = (t_struct *)tl->get_elem_type();
107  f_out_ << "<td><table border=\"1\"><tr>" << endl;
108  vector<t_field*> smem = ts->get_members();
109  vector<t_field*>::iterator smem_iter = smem.begin();
110  for ( ; smem_iter != smem.end(); smem_iter++) {
111  f_out_ << "<th>" << (*smem_iter)->get_name() << "</th>" << endl;
112  }
113  f_out_ << "</tr><xsl:apply-templates select=\"" <<
114  fdname << "/list/" << ts->get_name() << "\"/></table></td>" ;
115  } else {
116  f_out_ << "<td style=\"color:blue\"><xsl:value-of select=\"" <<
117  fdname << "\"/></td>" << endl;
118  }
119 #endif
120  } else {
121  f_out_ << "<" << fdname << " type=\"struct\">";
122  generate_struct_xml(fdtype->get_name());
123  f_out_ << "</" << fdname << ">";
124  }
125 }
126 
128  string name = tsnd->get_name();
129  f_out_ << "<" << name << " type=\"sandesh\">" << endl;
130  vector<t_field*> members = tsnd->get_members();
131  vector<t_field*>::iterator mem_iter = members.begin();
132  for ( ; mem_iter != members.end(); mem_iter++) {
133  if ((*mem_iter)->get_auto_generated()) continue;
134  generate_field_xml(*mem_iter);
135  }
136  f_out_ << "</" << name << ">" << endl;
137 }
138 
140  // Make output directory
141  MKDIR(get_out_dir().c_str());
142  string fname = get_out_dir() + program_->get_name() + ".xml";
143  f_out_.open(fname.c_str());
144  f_out_ <<
145  "<?xml-stylesheet type=\"text/xsl\" href=\"/universal_parse.xsl\"?>" <<
146  endl;
147  f_out_ << "<" << program_->get_name() << " type=\"rlist\">" << endl;
148 
149 #ifdef SANDESH
150  if (!program_->get_sandeshs().empty()) {
151  vector<t_sandesh*> sandeshs = program_->get_sandeshs();
152  vector<t_sandesh*>::iterator s_iter;
153  for (s_iter = sandeshs.begin(); s_iter != sandeshs.end(); ++s_iter) {
154  if (static_cast<const t_base_type*>((*s_iter)->get_type())->get_base() ==
155  t_base_type::TYPE_SANDESH_REQUEST)
156  generate_sandesh_xml(*s_iter);
157  }
158  }
159 #endif
160  f_out_ << "</" << program_->get_name() << ">" << endl;
161 
162  f_out_.close();
163 }
164 
170  generate_xml();
171 }
172 
173 THRIFT_REGISTER_GENERATOR(html, "HTML", "")
const std::string & get_name() const
Definition: t_field.h:95
Definition: t_enum.h:30
Definition: t_type.h:48
void generate_field_xml(t_field *tfl)
t_type * get_type() const
Definition: t_field.h:91
virtual bool is_base_type() const
Definition: t_type.h:61
const members_type & get_members()
t_html_generator(t_program *program, const std::map< std::string, std::string > &parsed_options, const std::string &option_string)
#define MKDIR(x)
Definition: platform.h:28
void generate_sandesh_xml(t_sandesh *tsnd)
std::ofstream f_out_
void generate_service(t_service *tservice)
t_type * get_elem_type() const
Definition: t_list.h:34
virtual const std::string & get_name() const
Definition: t_type.h:56
virtual bool is_list() const
Definition: t_type.h:76
void generate_typedef(t_typedef *ttypedef)
virtual bool is_struct() const
Definition: t_type.h:73
void generate_struct(t_struct *tstruct)
Definition: t_list.h:29
void generate_struct_xml(const string &stname)
void generate_const(t_const *tconst)
#define THRIFT_REGISTER_GENERATOR(language, long_name, doc)
void generate_enum(t_enum *tenum)