Search in sources :

Example 16 with XMLWriter

use of org.hl7.fhir.utilities.xml.XMLWriter in project kindling by HL7.

the class SvgGenerator method generate.

public String generate(String filename, String id) throws Exception {
    this.id = id;
    ini = new IniFile(filename);
    String[] classNames = ini.getStringProperty("diagram", "classes") == null ? null : ini.getStringProperty("diagram", "classes").split("\\,");
    if ("false".equals(ini.getStringProperty("diagram", "attributes")))
        attributes = false;
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    XMLWriter xml = new XMLWriter(bytes, "UTF-8");
    minx = 0;
    miny = 0;
    Point size = determineMetrics(classNames);
    adjustAllForMin(size);
    xml.setPretty(false);
    xml.start();
    xml.setDefaultNamespace(NS_SVG);
    xml.namespace(NS_XLINK, "xlink");
    xml.attribute("id", "n" + (++nc));
    xml.attribute("version", "1.1");
    xml.attribute("width", Utilities.noString(ini.getStringProperty("size", "width")) ? Double.toString(size.x) : ini.getStringProperty("size", "width"));
    xml.attribute("height", Utilities.noString(ini.getStringProperty("size", "height")) ? Double.toString(size.y) : ini.getStringProperty("size", "height"));
    xml.enter("svg");
    shadowFilter(xml);
    drawElement(xml, classNames);
    countDuplicateLinks();
    for (Link l : links) {
        drawLink(xml, l);
    }
    xml.exit("svg");
    xml.end();
    String s = new String(bytes.toByteArray());
    return s.substring(s.indexOf(">") + 1);
}
Also used : IniFile(org.hl7.fhir.utilities.IniFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMLWriter(org.hl7.fhir.utilities.xml.XMLWriter)

Example 17 with XMLWriter

use of org.hl7.fhir.utilities.xml.XMLWriter in project kindling by HL7.

the class SvgGenerator method addAttribute.

private void addAttribute(XMLWriter xml, double left, double top, ElementDefn e, String path, LineStatus ls, double height, double width) throws Exception {
    if (e.getStandardsStatus() != null) {
        xml.attribute("x", Double.toString(left + 1));
        xml.attribute("y", Double.toString(top - height + GAP_HEIGHT));
        xml.attribute("id", "n" + (++nc));
        xml.attribute("width", Double.toString(width - 2));
        xml.attribute("height", Double.toString(height));
        xml.attribute("style", "fill:" + e.getStandardsStatus().getColorSvg() + ";stroke:black;stroke-width:0");
        xml.element("rect", null);
    }
    xml.attribute("x", Double.toString(left + LEFT_MARGIN + (ls.line == 0 ? 0 : WRAP_INDENT)));
    xml.attribute("y", Double.toString(top + LINE_HEIGHT * ls.line));
    xml.attribute("fill", "black");
    xml.attribute("class", "diagram-class-detail");
    xml.attribute("id", "n" + (++nc));
    xml.enter("text");
    xml.attribute("xlink:href", baseUrl(path) + path + "." + e.getName().replace("[", "_").replace("]", "_"));
    xml.attribute("id", "n" + (++nc));
    xml.enter("a");
    xml.element("title", e.getEnhancedDefinition());
    xml.text(ls.see(e.getName()));
    xml.exit("a");
    xml.text(ls.see(" : "));
    encodeType(xml, ls, getTypeCodeForElement(e.getTypes()));
    xml.text(ls.see(" [" + e.describeCardinality() + "]"));
    // now, the stereotypes
    boolean hasTS = !((e.getTypes().isEmpty()) || (e.getTypes().size() == 1 && !isReference(e.getTypes().get(0).getName())));
    boolean hasBinding = (e.hasBinding() && e.getBinding().getBinding() != BindingMethod.Unbound);
    if (hasTS || hasBinding) {
        xml.text(ls.see(" \u00AB "));
        if (hasTS) {
            if (isReference(e.getTypes().get(0).getName()) && e.getTypes().size() == 1) {
                boolean first = true;
                for (String p : e.getTypes().get(0).getParams()) {
                    if (first)
                        first = false;
                    else
                        xml.text(ls.see("|"));
                    ls.check(xml, left, top, p.length(), null);
                    encodeType(xml, ls, p);
                }
            } else {
                boolean firstOuter = true;
                for (TypeRef t : e.getTypes()) {
                    if (firstOuter)
                        firstOuter = false;
                    else
                        xml.text(ls.see("|"));
                    ls.check(xml, left, top, t.getName().length(), null);
                    encodeType(xml, ls, t.getName());
                    if (t.getParams().size() > 0) {
                        xml.text(ls.see("("));
                        boolean first = true;
                        for (String p : t.getParams()) {
                            if (first)
                                first = false;
                            else
                                xml.text(ls.see("|"));
                            ls.check(xml, left, top, p.length(), null);
                            encodeType(xml, ls, p);
                        }
                        xml.text(ls.see(")"));
                    }
                }
            }
        }
        if (hasTS && hasBinding) {
            xml.text(ls.see("; "));
        }
        if (hasBinding) {
            BindingSpecification b = e.getBinding();
            String name = e.getBinding().getValueSet() != null ? e.getBinding().getValueSet().getName() : e.getBinding().getName();
            if (name.toLowerCase().endsWith(" codes"))
                name = name.substring(0, name.length() - 5);
            if (name.length() > 30)
                name = name.substring(0, 29) + "...";
            String link = getBindingLink(prefix, e);
            if (b.getStrength() == BindingStrength.EXAMPLE) {
                if (link != null) {
                    xml.attribute("xlink:href", link);
                    xml.attribute("id", "n" + (++nc));
                    xml.enter("a");
                    xml.attribute("id", "n" + (++nc));
                }
                xml.element("title", b.getDefinition() + " (Strength=Example)");
                for (String p : parts(name)) {
                    ls.check(xml, left, top, p.length(), link);
                    xml.text(ls.see(p));
                }
                if (link != null) {
                    xml.exit("a");
                }
                xml.text("??");
            } else if (b.getStrength() == BindingStrength.PREFERRED) {
                xml.attribute("xlink:href", link);
                xml.attribute("id", "n" + (++nc));
                xml.enter("a");
                xml.element("title", b.getDefinition() + " (Strength=Preferred)");
                for (String p : parts(name)) {
                    ls.check(xml, left, top, p.length(), link);
                    xml.text(ls.see(p));
                }
                xml.exit("a");
                xml.text("?");
            } else if (b.getStrength() == BindingStrength.EXTENSIBLE) {
                // if (b.hasMax())
                // what do in this case...
                xml.attribute("xlink:href", link);
                xml.attribute("id", "n" + (++nc));
                xml.enter("a");
                xml.attribute("id", "n" + (++nc));
                xml.element("title", b.getDefinition() + " (Strength=Extensible)");
                for (String p : parts(name)) {
                    ls.check(xml, left, top, p.length(), link);
                    xml.text(ls.see(p));
                }
                xml.exit("a");
                xml.text("+");
            } else if (b.getStrength() == BindingStrength.REQUIRED) {
                xml.attribute("xlink:href", link);
                xml.attribute("id", "n" + (++nc));
                xml.enter("a");
                xml.attribute("id", "n" + (++nc));
                xml.element("title", b.getDefinition() + " (Strength=Required)");
                // xml.open("b");
                for (String p : parts(name)) {
                    ls.check(xml, left, top, p.length(), link);
                    xml.text(ls.see(p));
                }
                // xml.close("b");
                xml.exit("a");
                xml.text("!");
            } else {
                xml.attribute("id", "n" + (++nc));
                xml.attribute("xlink:href", link);
                xml.enter("a");
                xml.attribute("id", "n" + (++nc));
                xml.element("title", b.getDefinition() + " (??)");
                for (String p : parts(name)) {
                    ls.check(xml, left, top, p.length(), link);
                    xml.text(ls.see(p));
                }
                xml.exit("a");
            }
        }
        xml.text(ls.see(" \u00BB"));
    }
    xml.exit("text");
}
Also used : TypeRef(org.hl7.fhir.definitions.model.TypeRef) BindingSpecification(org.hl7.fhir.definitions.model.BindingSpecification)

Example 18 with XMLWriter

use of org.hl7.fhir.utilities.xml.XMLWriter in project org.hl7.fhir.core by hapifhir.

the class XmlParser method composeElement.

private void composeElement(XMLWriter xml, Element element, String elementName) throws IOException {
    for (String s : element.getComments()) {
        xml.comment(s, true);
    }
    if (isText(element.getProperty())) {
        xml.enter(elementName);
        xml.text(element.getValue());
        xml.exit(elementName);
    } else if (element.isPrimitive() || (element.hasType() && ParserBase.isPrimitive(element.getType()))) {
        if (element.getType().equals("xhtml")) {
            xml.escapedText(element.getValue());
        } else if (isText(element.getProperty())) {
            xml.text(element.getValue());
        } else {
            if (element.hasValue())
                xml.attribute("value", element.getValue());
            if (element.hasChildren()) {
                xml.enter(elementName);
                for (Element child : element.getChildren()) composeElement(xml, child, child.getName());
                xml.exit(elementName);
            } else
                xml.element(elementName);
        }
    } else {
        for (Element child : element.getChildren()) {
            if (isAttr(child.getProperty()))
                xml.attribute(child.getName(), child.getValue());
        }
        xml.enter(elementName);
        if (element.getSpecial() != null)
            xml.enter(element.getType());
        for (Element child : element.getChildren()) {
            if (isText(child.getProperty()))
                xml.text(child.getValue());
            else if (!isAttr(child.getProperty()))
                composeElement(xml, child, child.getName());
        }
        if (element.getSpecial() != null)
            xml.exit(element.getType());
        xml.exit(elementName);
    }
}
Also used : SpecialElement(org.hl7.fhir.dstu2016may.metamodel.Element.SpecialElement)

Example 19 with XMLWriter

use of org.hl7.fhir.utilities.xml.XMLWriter in project org.hl7.fhir.core by hapifhir.

the class XmlParser method compose.

@Override
public void compose(Element e, OutputStream stream, OutputStyle style, String base) throws Exception {
    XMLWriter xml = new XMLWriter(stream, "UTF-8");
    xml.setPretty(style == OutputStyle.PRETTY);
    xml.start();
    xml.setDefaultNamespace(e.getProperty().getNamespace());
    composeElement(xml, e, e.getType());
    xml.end();
}
Also used : XMLWriter(org.hl7.fhir.utilities.xml.XMLWriter)

Example 20 with XMLWriter

use of org.hl7.fhir.utilities.xml.XMLWriter in project org.hl7.fhir.core by hapifhir.

the class XmlParserBase method compose.

/**
 * Compose a type to a stream (used in the spec, for example, but not normally in production)
 * @
 */
public void compose(OutputStream stream, String rootName, Type type) throws IOException {
    xml = new XMLWriter(stream, "UTF-8");
    xml.setPretty(style == OutputStyle.PRETTY);
    xml.start();
    xml.setDefaultNamespace(FHIR_NS);
    composeType(Utilities.noString(rootName) ? "value" : rootName, type);
    xml.end();
}
Also used : IXMLWriter(org.hl7.fhir.utilities.xml.IXMLWriter) XMLWriter(org.hl7.fhir.utilities.xml.XMLWriter)

Aggregations

XMLWriter (org.hl7.fhir.utilities.xml.XMLWriter)28 IXMLWriter (org.hl7.fhir.utilities.xml.IXMLWriter)22 FileOutputStream (java.io.FileOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)2 BindingSpecification (org.hl7.fhir.definitions.model.BindingSpecification)1 DefinedCode (org.hl7.fhir.definitions.model.DefinedCode)1 DefinedStringPattern (org.hl7.fhir.definitions.model.DefinedStringPattern)1 PrimitiveType (org.hl7.fhir.definitions.model.PrimitiveType)1 ProfiledType (org.hl7.fhir.definitions.model.ProfiledType)1 ResourceDefn (org.hl7.fhir.definitions.model.ResourceDefn)1 TypeRef (org.hl7.fhir.definitions.model.TypeRef)1 SpecialElement (org.hl7.fhir.dstu2016may.metamodel.Element.SpecialElement)1 IniFile (org.hl7.fhir.utilities.IniFile)1