------------------------------------------------------------------------------
| 120 | |
| 121 | //------------------------------------------------------------------------------ |
| 122 | void vtkJavaScriptDataWriter::WriteTable(vtkTable* table, ostream* stream_ptr) |
| 123 | { |
| 124 | if (stream_ptr == nullptr) |
| 125 | { |
| 126 | if (this->FileName && this->OpenFile()) |
| 127 | { |
| 128 | stream_ptr = this->OutputFile; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (stream_ptr != nullptr) |
| 133 | { |
| 134 | vtkIdType numRows = table->GetNumberOfRows(); |
| 135 | vtkIdType numCols = table->GetNumberOfColumns(); |
| 136 | vtkDataSetAttributes* dsa = table->GetRowData(); |
| 137 | |
| 138 | std::string rowHeader = "["; |
| 139 | std::string rowFooter = "],"; |
| 140 | if (this->IncludeFieldNames) |
| 141 | { |
| 142 | rowHeader = "{"; |
| 143 | rowFooter = "},"; |
| 144 | } |
| 145 | |
| 146 | // Header stuff |
| 147 | if (this->VariableName) |
| 148 | { |
| 149 | (*stream_ptr) << "var " << this->VariableName << " = [\n"; |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | (*stream_ptr) << "["; |
| 154 | } |
| 155 | |
| 156 | for (vtkIdType r = 0; r < numRows; ++r) |
| 157 | { |
| 158 | // row header |
| 159 | (*stream_ptr) << rowHeader; |
| 160 | |
| 161 | // Now for each column put out in the form |
| 162 | // colname1: data1, colname2: data2, etc |
| 163 | for (int c = 0; c < numCols; ++c) |
| 164 | { |
| 165 | if (this->IncludeFieldNames) |
| 166 | { |
| 167 | (*stream_ptr) << dsa->GetAbstractArray(c)->GetName() << ":"; |
| 168 | } |
| 169 | |
| 170 | // If the array is a string array put "" around it |
| 171 | if (vtkArrayDownCast<vtkStringArray>(dsa->GetAbstractArray(c))) |
| 172 | { |
| 173 | (*stream_ptr) << "\"" << table->GetValue(r, c).ToString() << "\","; |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | (*stream_ptr) << table->GetValue(r, c).ToString() << ","; |
| 178 | } |
| 179 | } |
no test coverage detected