Search in sources :

Example 1 with XhtmlGenerator

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

the class Publisher method produceProfile.

private void produceProfile(ResourceDefn resource, Profile pack, ConstraintStructure profile, SectionTracker st, String intro, String notes, String prefix, ImplementationGuideDefn ig) throws Exception {
    File tmp = Utilities.createTempFile("tmp", ".tmp");
    String title = profile.getId();
    int level = (ig == null || ig.isCore()) ? 0 : 1;
    // you have to validate a profile, because it has to be merged with it's
    // base resource to fill out all the missing bits
    // validateProfile(profile);
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    XmlSpecGenerator gen = new XmlSpecGenerator(bs, title + "-definitions.html", "", page, ig.isCore() ? "" : "../");
    gen.generate(profile.getResource());
    gen.close();
    String xml = new String(bs.toByteArray());
    bs = new ByteArrayOutputStream();
    JsonSpecGenerator genJ = new JsonSpecGenerator(bs, title + "-definitions.html", "", page, ig.isCore() ? "" : "../", page.getVersion().toCode());
    genJ.generate(profile.getResource());
    genJ.close();
    String json = new String(bs.toByteArray());
    XmlParser comp = new XmlParser();
    FileOutputStream s = new FileOutputStream(page.getFolders().dstDir + prefix + title + ".profile.xml");
    comp.setOutputStyle(OutputStyle.PRETTY).compose(s, profile.getResource());
    s.close();
    Utilities.copyFile(new CSFile(page.getFolders().dstDir + prefix + title + ".profile.xml"), new CSFile(page.getFolders().dstDir + "examples" + File.separator + title + ".profile.xml"));
    JsonParser jcomp = new JsonParser();
    s = new FileOutputStream(page.getFolders().dstDir + prefix + title + ".profile.json");
    jcomp.setOutputStyle(OutputStyle.PRETTY).compose(s, profile.getResource());
    s.close();
    // String shex = new ShExGenerator(page.getWorkerContext()).generate(HTMLLinkPolicy.NONE, profile.getResource());
    // TextFile.stringToFile(shex, Utilities.changeFileExt(page.getFolders().dstDir + prefix +title + ".profile.shex", ".shex"));
    // shexToXhtml(prefix +title + ".profile", "ShEx statement for " + prefix +title, shex, "profile-instance:type:" + title, "Type");
    TerminologyNotesGenerator tgen = new TerminologyNotesGenerator(new FileOutputStream(tmp), page);
    tgen.generate(level == 0 ? "" : "../", profile);
    tgen.close();
    String tx = TextFile.fileToString(tmp.getAbsolutePath());
    String src = TextFile.fileToString(page.getFolders().templateDir + "template-profile.html");
    src = page.processProfileIncludes(profile.getId(), profile.getId(), pack, profile, xml, json, tx, src, title + ".html", (resource == null ? profile.getResource().getType() : resource.getName()) + "/" + pack.getId() + "/" + profile.getId(), intro, notes, ig, false, false);
    if (st != null)
        src = insertSectionNumbers(src, st, title + ".html", level, null);
    else if (ig != null && !ig.isCore()) {
        src = addSectionNumbers(title + ".html", title, src, null, 1, null, ig);
        st = page.getSectionTrackerCache().get(ig.getCode() + "::" + title);
    }
    page.getHTMLChecker().registerFile(prefix + title + ".html", "StructureDefinition " + profile.getResource().getName(), HTMLLinkChecker.XHTML_TYPE, false);
    TextFile.stringToFile(src, page.getFolders().dstDir + prefix + title + ".html");
    new ProfileUtilities(page.getWorkerContext(), page.getValidationErrors(), page).generateSchematrons(new FileOutputStream(page.getFolders().dstDir + prefix + title + ".sch"), profile.getResource());
    if (pack.getExamples().size() > 0) {
        src = TextFile.fileToString(page.getFolders().templateDir + "template-profile-examples.html");
        src = page.processProfileIncludes(profile.getId(), profile.getId(), pack, profile, xml, json, tx, src, title + ".html", (resource == null ? profile.getResource().getType() : resource.getName()) + "/" + pack.getId() + "/" + profile.getId(), intro, notes, ig, false, false);
        page.getHTMLChecker().registerFile(prefix + title + "-examples.html", "Examples for StructureDefinition " + profile.getResource().getName(), HTMLLinkChecker.XHTML_TYPE, true);
        TextFile.stringToFile(src, page.getFolders().dstDir + prefix + title + "-examples.html");
    }
    src = TextFile.fileToString(page.getFolders().templateDir + "template-profile-definitions.html");
    src = page.processProfileIncludes(profile.getId(), profile.getId(), pack, profile, xml, json, tx, src, title + ".html", (resource == null ? profile.getResource().getType() : resource.getName()) + "/" + pack.getId() + "/" + profile.getId(), intro, notes, ig, false, false);
    if (st != null)
        src = insertSectionNumbers(src, st, title + "-definitions.html", level, null);
    page.getHTMLChecker().registerFile(prefix + title + "-definitions.html", "Definitions for StructureDefinition " + profile.getResource().getName(), HTMLLinkChecker.XHTML_TYPE, true);
    TextFile.stringToFile(src, page.getFolders().dstDir + prefix + title + "-definitions.html");
    src = TextFile.fileToString(page.getFolders().templateDir + "template-profile-mappings.html");
    src = page.processProfileIncludes(profile.getId(), profile.getId(), pack, profile, xml, json, tx, src, title + ".html", (resource == null ? profile.getResource().getType() : resource.getName()) + "/" + pack.getId() + "/" + profile.getId(), intro, notes, ig, false, false);
    if (st != null)
        src = insertSectionNumbers(src, st, title + "-mappings.html", level, null);
    page.getHTMLChecker().registerFile(prefix + title + "-mappings.html", "Mappings for StructureDefinition " + profile.getResource().getName(), HTMLLinkChecker.XHTML_TYPE, true);
    TextFile.stringToFile(src, page.getFolders().dstDir + prefix + title + "-mappings.html");
    try {
        processQuestionnaire(resource, profile.getResource(), st, false, prefix, ig);
    } catch (Exception e) {
        e.printStackTrace();
        page.log("Questionnaire Generation Failed: " + e.getMessage(), LogMessageType.Error);
    }
    new ReviewSpreadsheetGenerator().generate(page.getFolders().dstDir + prefix + Utilities.changeFileExt((String) profile.getResource().getUserData("filename"), "-review.xls"), "Health Level Seven International", page.getGenDate(), profile.getResource(), page);
    // xml to xhtml of xml
    // first pass is to strip the xsi: stuff. seems to need double
    // processing in order to delete namespace crap
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document xdoc = builder.parse(new CSFileInputStream(page.getFolders().dstDir + prefix + title + ".profile.xml"));
    XmlGenerator xmlgen = new XmlGenerator();
    xmlgen.generate(xdoc.getDocumentElement(), tmp, "http://hl7.org/fhir", xdoc.getDocumentElement().getLocalName());
    // reload it now
    builder = factory.newDocumentBuilder();
    xdoc = builder.parse(new CSFileInputStream(tmp.getAbsolutePath()));
    XhtmlGenerator xhtml = new XhtmlGenerator(new ExampleAdorner(page.getDefinitions(), page.genlevel(level)));
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    xhtml.generate(xdoc, b, "StructureDefinition", profile.getTitle(), 0, true, title + ".profile.xml.html");
    String html = TextFile.fileToString(page.getFolders().templateDir + "template-profile-example-xml.html").replace("<%example%>", b.toString());
    html = page.processProfileIncludes(title + ".profile.xml.html", profile.getId(), pack, profile, "", "", "", html, title + ".html", (resource == null ? profile.getResource().getType() : resource.getName()) + "/" + pack.getId() + "/" + profile.getId(), intro, notes, ig, false, hasNarrative(xdoc));
    TextFile.stringToFile(html, page.getFolders().dstDir + prefix + title + ".profile.xml.html");
    page.getHTMLChecker().registerFile(prefix + title + ".profile.xml.html", "StructureDefinition", HTMLLinkChecker.XHTML_TYPE, false);
    String n = prefix + title + ".profile";
    json = resource2Json(profile.getResource());
    json = "<div class=\"example\">\r\n<p>" + Utilities.escapeXml("StructureDefinition for " + profile.getResource().getDescription()) + "</p>\r\n<p><a href=\"" + title + ".profile.json\">Raw JSON</a></p>\r\n<pre class=\"json\">\r\n" + Utilities.escapeXml(json) + "\r\n</pre>\r\n</div>\r\n";
    html = TextFile.fileToString(page.getFolders().templateDir + "template-profile-example-json.html").replace("<%example%>", json);
    html = page.processProfileIncludes(title + ".profile.json.html", profile.getId(), pack, profile, "", "", "", html, title + ".html", (resource == null ? profile.getResource().getType() : resource.getName()) + "/" + pack.getId() + "/" + profile.getId(), intro, notes, ig, false, false);
    TextFile.stringToFile(html, page.getFolders().dstDir + prefix + title + ".profile.json.html");
    // page.getEpub().registerFile(n + ".json.html", description, EPubManager.XHTML_TYPE);
    page.getHTMLChecker().registerExternal(n + ".json.html");
    tmp.delete();
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) XmlSpecGenerator(org.hl7.fhir.definitions.generators.specification.XmlSpecGenerator) JsonSpecGenerator(org.hl7.fhir.definitions.generators.specification.JsonSpecGenerator) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XmlGenerator(org.hl7.fhir.utilities.xml.XmlGenerator) TerminologyNotesGenerator(org.hl7.fhir.definitions.generators.specification.TerminologyNotesGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CSFile(org.hl7.fhir.utilities.CSFile) Document(org.w3c.dom.Document) XhtmlDocument(org.hl7.fhir.utilities.xhtml.XhtmlDocument) ContactPoint(org.hl7.fhir.r5.model.ContactPoint) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ReviewSpreadsheetGenerator(org.hl7.fhir.definitions.generators.specification.ReviewSpreadsheetGenerator) XhtmlGenerator(org.hl7.fhir.utilities.xml.XhtmlGenerator) ProfileUtilities(org.hl7.fhir.r5.conformance.ProfileUtilities) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) CSFile(org.hl7.fhir.utilities.CSFile) TextFile(org.hl7.fhir.utilities.TextFile) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 2 with XhtmlGenerator

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

the class Publisher method cloneToXhtml.

private void cloneToXhtml(String n, String description, boolean adorn, String pageType, String crumbTitle, ImplementationGuideDefn igd, ResourceDefn rd, WorkGroup wg) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document xdoc = builder.parse(new CSFileInputStream(new CSFile(page.getFolders().dstDir + n + ".xml")));
    XhtmlGenerator xhtml = new XhtmlGenerator(new ExampleAdorner(page.getDefinitions(), page.genlevel(Utilities.charCount(n, File.separatorChar))));
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    xhtml.generate(xdoc, b, n.toUpperCase().substring(0, 1) + n.substring(1), description, 0, adorn, n + ".xml.html");
    String html = TextFile.fileToString(page.getFolders().templateDir + "template-example-xml.html").replace("<%example%>", b.toString());
    html = page.processPageIncludes(n + ".xml.html", html, pageType, null, n + ".xml.html", null, null, crumbTitle, (adorn && hasNarrative(xdoc)) ? new Boolean(true) : null, igd, rd, wg);
    TextFile.stringToFile(html, page.getFolders().dstDir + n + ".xml.html");
    // page.getEpub().registerFile(n + ".xml.html", description, EPubManager.XHTML_TYPE);
    page.getHTMLChecker().registerExternal(n + ".xml.html");
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) CSFile(org.hl7.fhir.utilities.CSFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) XhtmlDocument(org.hl7.fhir.utilities.xhtml.XhtmlDocument) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) XhtmlGenerator(org.hl7.fhir.utilities.xml.XhtmlGenerator)

Example 3 with XhtmlGenerator

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

the class PageProcessor method cloneToXhtml.

private void cloneToXhtml(String src, String dst, String name, String description, int level, boolean adorn, String pageType, String crumbTitle, ImplementationGuideDefn ig, ResourceDefn rd, WorkGroup wg) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document xdoc = builder.parse(new CSFileInputStream(new CSFile(src)));
    // XhtmlGenerator xhtml = new XhtmlGenerator(null);
    // xhtml.generate(xdoc, new CSFile(dst), name, description, level, adorn);
    String n = new File(dst).getName();
    n = n.substring(0, n.length() - 9);
    XhtmlGenerator xhtml = new XhtmlGenerator(new ExampleAdorner(definitions, genlevel(level)));
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    xhtml.generate(xdoc, b, name, description, level, adorn, n + ".xml.html");
    String html = ("<%setlevel " + Integer.toString(level) + "%>" + TextFile.fileToString(folders.srcDir + "template-example-xml.html")).replace("<%example%>", b.toString());
    html = processPageIncludes(n + ".xml.html", html, pageType, null, n + ".xml.html", null, null, crumbTitle, (adorn && hasNarrative(xdoc)) ? new Boolean(true) : null, ig, rd, wg);
    TextFile.stringToFile(html, dst);
    htmlchecker.registerExternal(dst);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) CSFile(org.hl7.fhir.utilities.CSFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) CSFile(org.hl7.fhir.utilities.CSFile) TextFile(org.hl7.fhir.utilities.TextFile) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) XhtmlGenerator(org.hl7.fhir.utilities.xml.XhtmlGenerator)

Example 4 with XhtmlGenerator

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

the class ExampleAdorner method getLink.

@Override
public String getLink(XhtmlGenerator ref, XhtmlGeneratorAdornerState state, Element node) throws Exception {
    if (state == null) {
        if (node == null || !definitions.hasResource(node.getLocalName()))
            return null;
        else {
            definitions.getResourceByName(node.getLocalName()).getRoot().setCoveredByExample(true);
            return prefix + node.getLocalName().toLowerCase() + "-definitions.html";
        }
    } else {
        ExampleAdornerState s = (ExampleAdornerState) state;
        if (s.definition == null)
            if (node.getNamespaceURI().equals("http://www.w3.org/1999/xhtml"))
                return prefix + "narrative.html";
            else
                return null;
        ElementDefn t = s.definition;
        if (t.typeCode().equals("Resource") && (s.getPath().endsWith(".entry.resource") || s.getPath().endsWith(".contained")) && definitions.isResource(node.getNodeName()))
            return null;
        ElementDefn child = t.getElementByName(node.getNodeName(), true, definitions, "adorn example", false);
        String p = child == null ? null : s.getPath() + "." + child.getName();
        while (child == null && t != null && definitions.hasElementDefn(t.typeCodeNoParams())) {
            t = definitions.getElementDefn(t.typeCodeNoParams());
            child = t.getElementByName(node.getNodeName(), true, definitions, "adorn example", false);
            if (child != null) {
                p = t.getName() + "." + child.getName();
            }
        }
        if (child == null) {
            if (node.getNamespaceURI().equals("http://www.w3.org/1999/xhtml"))
                return prefix + "narrative.html";
            else
                return null;
        } else {
            child.setCoveredByExample(true);
            String r = p.contains(".") ? p.substring(0, p.indexOf(".")) : p;
            return prefix + definitions.getSrcFile(r) + "-definitions.html#" + p;
        }
    }
}
Also used : ElementDefn(org.hl7.fhir.definitions.model.ElementDefn)

Example 5 with XhtmlGenerator

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

the class ExampleAdorner method getState.

@Override
public XhtmlGeneratorAdornerState getState(XhtmlGenerator ref, XhtmlGeneratorAdornerState state, Element node) throws Exception {
    if (state == null) {
        if (node == null)
            return new ExampleAdornerState(State.Unknown, "", null, "", "");
        else if (definitions.hasResource(node.getLocalName()))
            return new ExampleAdornerState(State.Element, node.getLocalName(), definitions.getResourceByName(node.getLocalName()).getRoot(), "", "");
        else
            return new ExampleAdornerState(State.Unknown, "", null, "", "");
    } else {
        ExampleAdornerState s = (ExampleAdornerState) state;
        if (s.getState() == State.Element) {
            if (s.definition == null) {
                System.out.println("??");
            }
            if (// account for extra element
            s.definition != null && "Resource".equals(s.definition.typeCode()) && (s.getPath().endsWith(".entry.resource") || s.getPath().endsWith(".contained")) && definitions.isResource(node.getNodeName()))
                return new ExampleAdornerState(State.Element, s.getPath(), definitions.getResourceByName(node.getNodeName()).getRoot(), "", "");
            String p = s.getPath() + "." + node.getNodeName();
            ElementDefn e = s.getDefinition().getElementByName(node.getLocalName(), true, definitions, "adorn example", false);
            if (e == null && definitions.hasElementDefn(s.getDefinition().typeCodeNoParams())) {
                // well, we see if it's inherited...
                ElementDefn t = definitions.getElementDefn(s.getDefinition().typeCodeNoParams());
                while (t != null && e == null) {
                    e = t.getElementByName(node.getLocalName(), true, definitions, "adorn example", false);
                    if (e != null)
                        p = t.getName() + "." + e.getName();
                    else if (definitions.hasElementDefn(t.typeCodeNoParams()))
                        t = definitions.getElementDefn(t.typeCodeNoParams());
                    else
                        t = null;
                }
            } else if (e != null)
                p = s.getPath() + "." + e.getName();
            if (e == null)
                return new ExampleAdornerState(State.Unknown, s.getPath(), null, "", "");
            if (!e.isBaseResourceElement() && e.typeCode().contains("Reference") && !e.typeCode().contains("CodeableReference"))
                return new ExampleAdornerState(State.Reference, p, e, "", "");
            else if (!e.isBaseResourceElement() && e.typeCode().contains("canonical"))
                return new ExampleAdornerState(State.Reference, p, e, "", "");
            else if (!e.isBaseResourceElement() && e.typeCode().equals("uri"))
                return new ExampleAdornerState(State.Reference, p, e, "", "");
            else
                return new ExampleAdornerState(State.Element, p, e, "", "");
        } else if (s.getState() == State.Reference) {
            if (node.getLocalName().equals("reference")) {
                String type = extractType(node.getAttribute("value"));
                String id = extractId(node.getAttribute("value"), type);
                if (id == null)
                    return new ExampleAdornerState(State.Element, s.getPath() + ".reference", null, "", "");
                ResourceDefn r = definitions.getResourceByName(type);
                if (r == null)
                    throw new Exception("unable to find type " + type);
                for (Example e : r.getExamples()) {
                    if (id.equals(e.getId()))
                        if (Utilities.noString(e.getIg())) {
                            return new ExampleAdornerState(State.Reference, s.getPath() + ".reference", s.getDefinition(), "<a href=\"" + prefix + e.getTitle() + ".xml.html\">", "</a>");
                        } else {
                            ImplementationGuideDefn ig = definitions.getIgs().get(e.getIg());
                            return new ExampleAdornerState(State.Reference, s.getPath() + ".reference", s.getDefinition(), "<a href=\"" + prefix + ig.getPrefix() + e.getTitle() + ".xml.html\">", "</a>");
                        }
                    if (e.getXml() != null && e.getXml().getDocumentElement().getLocalName().equals("feed")) {
                        List<Element> entries = new ArrayList<Element>();
                        XMLUtil.getNamedChildren(e.getXml().getDocumentElement(), "entry", entries);
                        String url = "http://hl7.org/fhir/" + type + "/" + id;
                        for (Element c : entries) {
                            String t = XMLUtil.getNamedChild(c, "id").getAttribute("value");
                            if (url.equals(t))
                                return new ExampleAdornerState(State.Reference, s.getPath() + ".reference", s.getDefinition(), "<a href=\"" + prefix + e.getTitle() + ".xml.html#" + id + "\">", "</a>");
                        }
                    }
                }
                return new ExampleAdornerState(State.Reference, s.getPath() + ".reference", s.getDefinition(), "<font color=\"red\">", "</font>");
            } else
                return new ExampleAdornerState(State.Reference, s.getPath(), s.getDefinition(), "", "");
        } else
            // else
            return new ExampleAdornerState(State.Unknown, s.getPath(), null, "", "");
    }
}
Also used : Example(org.hl7.fhir.definitions.model.Example) Element(org.w3c.dom.Element) ElementDefn(org.hl7.fhir.definitions.model.ElementDefn) ArrayList(java.util.ArrayList) List(java.util.List) ImplementationGuideDefn(org.hl7.fhir.definitions.model.ImplementationGuideDefn) ResourceDefn(org.hl7.fhir.definitions.model.ResourceDefn)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DocumentBuilder (javax.xml.parsers.DocumentBuilder)4 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)4 CSFile (org.hl7.fhir.utilities.CSFile)4 CSFileInputStream (org.hl7.fhir.utilities.CSFileInputStream)4 XhtmlGenerator (org.hl7.fhir.utilities.xml.XhtmlGenerator)4 XhtmlDocument (org.hl7.fhir.utilities.xhtml.XhtmlDocument)3 Document (org.w3c.dom.Document)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 TransformerException (javax.xml.transform.TransformerException)2 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)2 FHIRException (org.hl7.fhir.exceptions.FHIRException)2 XmlParser (org.hl7.fhir.r5.formats.XmlParser)2 ContactPoint (org.hl7.fhir.r5.model.ContactPoint)2 IniFile (org.hl7.fhir.utilities.IniFile)2