ome-xml 5.2.0

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List

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
63 getModelVersion(ome::common::xml::dom::Document& document)
64 {
65 ome::common::xml::dom::Element docroot(document.getDocumentElement());
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 {
105 ome::common::xml::dom::Document inputdoc(ome::common::xml::dom::createDocument(input, entity_resolver));
106
107 const std::string source_schema(getModelVersion(inputdoc));
108
109 if (source_schema.empty())
110 throw std::runtime_error("Impossible to determine model schema version");
111
112 typedef std::pair<std::vector<OMETransformResolver::Transform>, OMETransformResolver::Quality> transform_list;
113 transform_list transforms(transform_resolver.transform_order(source_schema, target_schema));
114
116
117 if (transforms.first.empty()) // No transformation required
118 {
119 ome::common::xml::dom::writeDocument(inputdoc, output);
120 }
121 else
122 {
123 ome::common::xsl::Transformer transformer;
124 transformer.setEntityResolver(&entity_resolver);
125
126 // For loading and storing intermediate schema content;
127 std::istringstream is;
128 std::ostringstream os;
129
130 ome::common::xml::dom::writeDocument(inputdoc, os);
131 is.str(os.str());
132 is.clear();
133 os.str("");
134 os.clear();
135
136 for (transform_list::first_type::const_iterator i = transforms.first.begin();
137 i != transforms.first.end();
138 ++i)
139 {
140 // Note that validation can trigger asserts inside the
141 // Xalan library, so it's safest to disable it and
142 // validate the end result.
143 if (i + 1 == transforms.first.end()) // Use output type
144 {
145 transformer.setUseValidation(false);
146 transformer.transform(i->file, is, output);
147 }
148 else // Use output stringstream
149 {
150 transformer.setUseValidation(false);
151 transformer.transform(i->file, is, os);
152 is.str(os.str());
153 is.clear();
154 os.str("");
155 os.clear();
156 }
157 }
158 }
159 }
160
179 template<typename Input, typename Output>
180 void
181 transform(const std::string& target_schema,
182 const Input& input,
183 Output& output,
184 ome::common::xml::EntityResolver& entity_resolver)
185 {
186 OMETransformResolver transform_resolver;
187 transform(target_schema, input, output,
188 entity_resolver, transform_resolver);
189 }
190
191 }
192 }
193
194 #endif // OME_XML_MODEL_OMETRANSFORM_H
195
196 /*
197 * Local Variables:
198 * mode:C++
199 * End:
200 */
ome::xml::OMETransformResolver::Quality
Quality
Transformation quality.
Definition: OMETransformResolver.h:125
ome::common::xsl::Transformer::setUseValidation
void setUseValidation(bool validate)
ome::xml::OMETransformResolver::transform_order
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
ome::common::xml::dom::Document
ome::common::xml::dom::Element
ome::common::xml::dom::createDocument
Document createDocument(const boost::filesystem::path &file, EntityResolver &resolver, const ParseParameters &params=ParseParameters())
ome::common::xml::dom::writeDocument
void writeDocument(Document &document, const boost::filesystem::path &file, const WriteParameters &params=WriteParameters())
ome::common::xsl::Transformer::setEntityResolver
void setEntityResolver(xml::EntityResolver *resolver)
ome::common::xml::EntityResolver
ome::common::xsl::Transformer::transform
void transform(xalanc::XSLTInputSource &xsl, xalanc::XSLTInputSource &input, xalanc::XSLTResultTarget &output)
ome
Open Microscopy Environment C++ implementation.
regex.h
ome::xml::OMETransformResolver
Discover and query available OME-XML transforms.
Definition: OMETransformResolver.h:72
ome::common::xsl::Transformer
ome::xml::getModelVersion
std::string getModelVersion(ome::common::xml::dom::Document &document)
Determine the model schema version used in an XML document.
Definition: OMETransform.h:63
ome::common::xml::dom::Document::getDocumentElement
Element getDocumentElement()
ome::xml::transform
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
ome::common::xml::String