ome-xml  5.2.2
OMETransform.h
1 /*
2  * #%L
3  * OME-XML C++ library for working with OME-XML metadata structures.
4  * %%
5  * Copyright © 2016 Open Microscopy Environment:
6  * - Massachusetts Institute of Technology
7  * - National Institutes of Health
8  * - University of Dundee
9  * - Board of Regents of the University of Wisconsin-Madison
10  * - Glencoe Software, Inc.
11  * %%
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * The views and conclusions contained in the software and documentation are
34  * those of the authors and should not be interpreted as representing official
35  * policies, either expressed or implied, of any organization.
36  * #L%
37  */
38 
39 #ifndef OME_XML_MODEL_OMETRANSFORM_H
40 #define OME_XML_MODEL_OMETRANSFORM_H
41 
42 #include <ome/compat/regex.h>
43 #include <ome/common/xml/dom/Document.h>
44 #include <ome/common/xsl/Transformer.h>
45 #include <ome/xml/OMETransformResolver.h>
46 #include <ome/xml/OMEEntityResolver.h>
47 
48 namespace ome
49 {
50  namespace xml
51  {
52 
61  inline
62  std::string
64  {
66 
67  std::string ns = common::xml::String(docroot->getNamespaceURI());
68 
69  ome::compat::smatch found;
70  static const ome::compat::regex schema_match("^http://www.openmicroscopy.org/Schemas/OME/(.*)$");
71 
72  if (ome::compat::regex_match(ns, found, schema_match))
73  {
74  return found[1];
75  }
76  else if (ns == "http://www.openmicroscopy.org/XMLschemas/OME/FC/ome.xsd")
77  {
78  return "2003-FC";
79  }
80 
81  return "";
82  }
83 
97  template<typename Input, typename Output>
98  void
99  transform(const std::string& target_schema,
100  const Input& input,
101  Output& output,
102  ome::common::xml::EntityResolver& entity_resolver,
103  OMETransformResolver& transform_resolver)
104  {
106  try
107  {
108  inputdoc = ome::common::xml::dom::createDocument(input, entity_resolver);
109  }
110  catch (const std::runtime_error&) // retry without strict validation
111  {
113  params.doSchema = false;
114  params.validationSchemaFullChecking = false;
115  inputdoc = ome::common::xml::dom::createDocument(input, entity_resolver, params);
116  }
117 
118  const std::string source_schema(getModelVersion(inputdoc));
119 
120  if (source_schema.empty())
121  throw std::runtime_error("Impossible to determine model schema version");
122 
123  typedef std::pair<std::vector<OMETransformResolver::Transform>, OMETransformResolver::Quality> transform_list;
124  transform_list transforms(transform_resolver.transform_order(source_schema, target_schema));
125 
127 
128  if (transforms.first.empty()) // No transformation required
129  {
130  ome::common::xml::dom::writeDocument(inputdoc, output);
131  }
132  else
133  {
134  ome::common::xsl::Transformer transformer;
135  transformer.setEntityResolver(&entity_resolver);
136 
137  // For loading and storing intermediate schema content;
138  std::istringstream is;
139  std::ostringstream os;
140 
142  is.str(os.str());
143  is.clear();
144  os.str("");
145  os.clear();
146 
147  for (transform_list::first_type::const_iterator i = transforms.first.begin();
148  i != transforms.first.end();
149  ++i)
150  {
151  // Note that validation can trigger asserts inside the
152  // Xalan library, so it's safest to disable it and
153  // validate the end result.
154  if (i + 1 == transforms.first.end()) // Use output type
155  {
156  transformer.setUseValidation(false);
157  transformer.transform(i->file, is, output);
158  }
159  else // Use output stringstream
160  {
161  transformer.setUseValidation(false);
162  transformer.transform(i->file, is, os);
163  is.str(os.str());
164  is.clear();
165  os.str("");
166  os.clear();
167  }
168  }
169  }
170  }
171 
190  template<typename Input, typename Output>
191  void
192  transform(const std::string& target_schema,
193  const Input& input,
194  Output& output,
195  ome::common::xml::EntityResolver& entity_resolver)
196  {
197  OMETransformResolver transform_resolver;
198  transform(target_schema, input, output,
199  entity_resolver, transform_resolver);
200  }
201 
202  }
203 }
204 
205 #endif // OME_XML_MODEL_OMETRANSFORM_H
206 
207 /*
208  * Local Variables:
209  * mode:C++
210  * End:
211  */
Quality
Transformation quality.
Definition: OMETransformResolver.h:125
void setUseValidation(bool validate)
std::pair< std::vector< Transform >, Quality > transform_order(const std::string &source, const std::string &target) const
Determine the optimal transform order between schema versions.
Definition: OMETransformResolver.cpp:317
Document createDocument(const boost::filesystem::path &file, EntityResolver &resolver, const ParseParameters &params=ParseParameters())
void writeDocument(Document &document, const boost::filesystem::path &file, const WriteParameters &params=WriteParameters())
void setEntityResolver(xml::EntityResolver *resolver)
void transform(xalanc::XSLTInputSource &xsl, xalanc::XSLTInputSource &input, xalanc::XSLTResultTarget &output)
Open Microscopy Environment C++ implementation.
Discover and query available OME-XML transforms.
Definition: OMETransformResolver.h:72
std::string getModelVersion(ome::common::xml::dom::Document &document)
Determine the model schema version used in an XML document.
Definition: OMETransform.h:63
void transform(const std::string &target_schema, const Input &input, Output &output, ome::common::xml::EntityResolver &entity_resolver, OMETransformResolver &transform_resolver)
Transform OME-XML to a different model schema version.
Definition: OMETransform.h:99