org.hl7.fhir.utilities.xml.SchematronWriter Maven / Gradle / Ivy
package org.hl7.fhir.utilities.xml;
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.hl7.fhir.utilities.TextStreamWriter;
import org.hl7.fhir.utilities.Utilities;
public class SchematronWriter extends TextStreamWriter {
public enum SchematronType {
ALL_RESOURCES,
RESOURCE,
PROFILE
}
public class Assert {
private String test;
private String message;
}
public class Rule {
private String name;
private List asserts = new ArrayList();
public void assrt(String test, String message) {
Assert a = new Assert();
a.test = test;
a.message = message;
asserts.add(a);
}
public boolean isSpecial() {
return name.contains("*") || name.contains("[");
}
}
public class Section {
private String title;
private List rules = new ArrayList();
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Rule rule(String name) {
for (Rule r : rules) {
if (r.name.equals(name))
return r;
}
Rule r = new Rule();
r.name = name;
rules.add(r);
return r;
}
public boolean hasRegularContent() {
for (Rule r : rules)
if (!r.asserts.isEmpty() && !r.isSpecial())
return true;
return false;
}
public boolean hasSpecialContent() {
for (Rule r : rules)
if (!r.asserts.isEmpty() && r.isSpecial())
return true;
return false;
}
public List getRegularRules() {
List regular = new ArrayList();
for (Rule r : rules)
if (!r.asserts.isEmpty() && !r.isSpecial())
regular.add(r);
return regular;
}
public List getSpecialRules() {
List regular = new ArrayList();
for (Rule r : rules)
if (!r.asserts.isEmpty() && r.isSpecial())
regular.add(r);
return regular;
}
}
private SchematronType type;
private String description;
private List sections = new ArrayList();
public SchematronWriter(OutputStream out, SchematronType type, String description) throws UnsupportedEncodingException {
super(out);
this.type = type;
this.description = description;
}
public Section section(String title) {
for (Section s : sections) {
if (s.title.equals(title))
return s;
}
Section s = new Section();
s.title = title;
sections.add(s);
return s;
}
public void dump() throws IOException {
ln("");
ln_i("");
ln("");
ln("");
addNote();
for (Section s : sections) {
if (s.hasRegularContent()) {
ln_i("");
ln(""+Utilities.escapeXml(s.title)+" ");
for (Rule r : s.getRegularRules()) {
if (!r.asserts.isEmpty()) {
ln_i("");
for (Assert a : r.asserts)
ln(""+Utilities.escapeXml(a.message)+" ");
ln_o(" ");
}
}
ln_o(" ");
}
if (s.hasSpecialContent()) {
int i = 1;
for (Rule r : s.getSpecialRules()) {
ln_i("");
ln(""+Utilities.escapeXml(s.title)+" "+i+" ");
i++;
if (!r.asserts.isEmpty()) {
ln_i("");
for (Assert a : r.asserts)
ln(""+Utilities.escapeXml(a.message)+" ");
ln_o(" ");
}
ln_o(" ");
}
}
}
ln_o(" ");
flush();
close();
}
private void addNote() throws IOException {
switch (type) {
case ALL_RESOURCES : addAllResourcesNote(); break;
case RESOURCE : addResourceNote(); break;
case PROFILE : addProfileNote(); break;
}
}
private void addAllResourcesNote() throws IOException {
ln("");
}
private void addResourceNote() throws IOException {
ln("");
}
private void addProfileNote() throws IOException {
ln("");
}
}