ome-xml  5.6.0
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 <sstream>
43 
44 #include <ome/compat/regex.h>
45 #include <ome/common/xml/dom/Document.h>
46 #include <ome/common/xsl/Transformer.h>
47 #include <ome/xml/OMETransformResolver.h>
48 #include <ome/xml/OMEEntityResolver.h>
49 
50 namespace ome
51 {
52  namespace xml
53  {
54 
63  inline
64  std::string
66  {
68 
69  std::string ns = common::xml::String(docroot->getNamespaceURI());
70 
71  ome::compat::smatch found;
72  static const ome::compat::regex schema_match("^http://www.openmicroscopy.org/Schemas/OME/(.*)$");
73 
74  if (ome::compat::regex_match(ns, found, schema_match))
75  {
76  return found[1];
77  }
78  else if (ns == "http://www.openmicroscopy.org/XMLschemas/OME/FC/ome.xsd")
79  {
80  return "2003-FC";
81  }
82 
83  return "";
84  }
85 
99  template<typename Input, typename Output>
100  void
101  transform(const std::string& target_schema,
102  const Input& input,
103  Output& output,
104  ome::common::xml::EntityResolver& entity_resolver,
105  OMETransformResolver& transform_resolver)
106  {
108  try
109  {
110  inputdoc = ome::common::xml::dom::createDocument(input, entity_resolver);
111  }
112  catch (const std::runtime_error&) // retry without strict validation
113  {
115  params.doSchema = false;
116  params.validationSchemaFullChecking = false;
117  inputdoc = ome::common::xml::dom::createDocument(input, entity_resolver, params);
118  }
119 
120  const std::string source_schema(getModelVersion(inputdoc));
121 
122  if (source_schema.empty())
123  throw std::runtime_error("Impossible to determine model schema version");
124 
125  typedef std::pair<std::vector<OMETransformResolver::Transform>, OMETransformResolver::Quality> transform_list;
126  transform_list transforms(transform_resolver.transform_order(source_schema, target_schema));
127 
129 
130  if (transforms.first.empty()) // No transformation required
131  {
132  ome::common::xml::dom::writeDocument(inputdoc, output);
133  }
134  else
135  {
136  ome::common::xsl::Transformer transformer;
137  transformer.setEntityResolver(&entity_resolver);
138 
139  // For loading and storing intermediate schema content;
140  std::istringstream is;
141  std::ostringstream os;
142 
144  is.str(os.str());
145  is.clear();
146  os.str("");
147  os.clear();
148 
149  for (transform_list::first_type::const_iterator i = transforms.first.begin();
150  i != transforms.first.end();
151  ++i)
152  {
153  // Note that validation can trigger asserts inside the
154  // Xalan library, so it's safest to disable it and
155  // validate the end result.
156  if (i + 1 == transforms.first.end()) // Use output type
157  {
158  transformer.setUseValidation(false);
159  transformer.transform(i->file, is, output);
160  }
161  else // Use output stringstream
162  {
163  transformer.setUseValidation(false);
164  transformer.transform(i->file, is, os);
165  is.str(os.str());
166  is.clear();
167  os.str("");
168  os.clear();
169  }
170  }
171  }
172  }
173 
192  template<typename Input, typename Output>
193  void
194  transform(const std::string& target_schema,
195  const Input& input,
196  Output& output,
197  ome::common::xml::EntityResolver& entity_resolver)
198  {
199  OMETransformResolver transform_resolver;
200  transform(target_schema, input, output,
201  entity_resolver, transform_resolver);
202  }
203 
204  }
205 }
206 
207 #endif // OME_XML_MODEL_OMETRANSFORM_H
208 
209 /*
210  * Local Variables:
211  * mode:C++
212  * End:
213  */
Quality
Transformation quality.
Definition: OMETransformResolver.h:124
void setUseValidation(bool validate)
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)
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
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:71
std::string getModelVersion(ome::common::xml::dom::Document &document)
Determine the model schema version used in an XML document.
Definition: OMETransform.h:65
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:101