From: Mathieu Baudier Date: Sun, 17 Jan 2021 11:52:28 +0000 (+0100) Subject: Improve DocBook support. X-Git-Tag: argeo-suite-2.1.18~35 X-Git-Url: https://git.argeo.org/?p=gpl%2Fargeo-suite.git;a=commitdiff_plain;h=682b5baf9a60b9772c1e13ba76dc0744be077b13 Improve DocBook support. --- diff --git a/org.argeo.suite.core/src/org/argeo/suite/core/SuiteTermsManager.java b/org.argeo.suite.core/src/org/argeo/suite/core/SuiteTermsManager.java index 3c5a59f..1362f94 100644 --- a/org.argeo.suite.core/src/org/argeo/suite/core/SuiteTermsManager.java +++ b/org.argeo.suite.core/src/org/argeo/suite/core/SuiteTermsManager.java @@ -46,6 +46,8 @@ public class SuiteTermsManager implements TermsManager { if (t == null) { Node termsNode = Jcr.getNode(adminSession, "SELECT * FROM [{0}] WHERE NAME()=\"{1}\"", EntityType.terms.get(), typology); + if (termsNode == null) + throw new IllegalArgumentException("Typology " + typology + " not found."); t = loadTypology(termsNode); } return t; diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/Paragraph.java b/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/Paragraph.java index 2189f34..6ff4281 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/Paragraph.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/Paragraph.java @@ -9,6 +9,7 @@ import org.argeo.cms.ui.viewers.SectionPart; import org.argeo.cms.ui.widgets.EditableText; import org.argeo.cms.ui.widgets.TextStyles; +/** An editable paragraph.*/ public class Paragraph extends EditableText implements SectionPart { private static final long serialVersionUID = 3746457776229542887L; diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/TextSection.java b/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/TextSection.java index ef4bb0f..4f514bf 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/TextSection.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/TextSection.java @@ -5,6 +5,7 @@ import javax.jcr.Node; import org.argeo.cms.ui.util.CmsUiUtils; import org.argeo.cms.ui.viewers.Section; import org.argeo.cms.ui.widgets.TextStyles; +import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; public class TextSection extends Section implements CmsNames { @@ -12,6 +13,10 @@ public class TextSection extends Section implements CmsNames { private String defaultTextStyle = TextStyles.TEXT_DEFAULT; private String titleStyle; + private final boolean flat; + + private boolean titleReadOnly = false; + public TextSection(Composite parent, int style, Node node) { this(parent, findSection(parent), style, node); } @@ -22,13 +27,18 @@ public class TextSection extends Section implements CmsNames { private TextSection(Composite parent, Section parentSection, int style, Node node) { super(parent, parentSection, style, node); - CmsUiUtils.style(this, TextStyles.TEXT_SECTION); + flat = SWT.FLAT == (style & SWT.FLAT); + //CmsUiUtils.style(this, TextStyles.TEXT_SECTION); } public String getDefaultTextStyle() { return defaultTextStyle; } + public boolean isFlat() { + return flat; + } + public String getTitleStyle() { if (titleStyle != null) return titleStyle; @@ -44,4 +54,13 @@ public class TextSection extends Section implements CmsNames { public void setTitleStyle(String titleStyle) { this.titleStyle = titleStyle; } + + public boolean isTitleReadOnly() { + return titleReadOnly; + } + + public void setTitleReadOnly(boolean titleReadOnly) { + this.titleReadOnly = titleReadOnly; + } + } diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/AbstractDbkViewer.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/AbstractDbkViewer.java index 39ae080..8a52779 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/AbstractDbkViewer.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/AbstractDbkViewer.java @@ -1,6 +1,5 @@ package org.argeo.docbook.ui; -import static javax.jcr.Property.JCR_TITLE; import static org.argeo.cms.ui.util.CmsUiUtils.fillWidth; import java.util.ArrayList; @@ -11,7 +10,6 @@ import java.util.Observer; import javax.jcr.Item; import javax.jcr.Node; import javax.jcr.NodeIterator; -import javax.jcr.Property; import javax.jcr.RepositoryException; import javax.jcr.Session; import javax.jcr.nodetype.NodeType; @@ -97,14 +95,13 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke CmsUiUtils.clear(section); Node node = section.getNode(); TextSection textSection = (TextSection) section; - if (node.hasProperty(Property.JCR_TITLE)) { + if (node.hasNode(DocBookTypes.TITLE)) { if (section.getHeader() == null) section.createHeader(); - if (node.hasProperty(Property.JCR_TITLE)) { - SectionTitle title = newSectionTitle(textSection, node); - title.setLayoutData(CmsUiUtils.fillWidth()); - updateContent(title); - } + Node titleNode = node.getNode(DocBookTypes.TITLE); + DocBookSectionTitle title = newSectionTitle(textSection, titleNode); + title.setLayoutData(CmsUiUtils.fillWidth()); + updateContent(title); } for (NodeIterator ni = node.getNodes(DocBookNames.DBK_PARA); ni.hasNext();) { @@ -125,15 +122,15 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke ((Control) sectionPart).setLayoutData(CmsUiUtils.fillWidth()); } - if (!flat) - for (NodeIterator ni = section.getNode().getNodes(DocBookNames.DBK_SECTION); ni.hasNext();) { - Node child = ni.nextNode(); - if (child.isNodeType(DocBookTypes.SECTION)) { - TextSection newSection = new TextSection(section, SWT.NONE, child); - newSection.setLayoutData(CmsUiUtils.fillWidth()); - refresh(newSection); - } +// if (!flat) + for (NodeIterator ni = section.getNode().getNodes(DocBookNames.DBK_SECTION); ni.hasNext();) { + Node child = ni.nextNode(); + if (child.isNodeType(DocBookTypes.SECTION)) { + TextSection newSection = new TextSection(section, SWT.NONE, child); + newSection.setLayoutData(CmsUiUtils.fillWidth()); + refresh(newSection); } + } } else { for (Section s : section.getSubSections().values()) refresh(s); @@ -175,21 +172,28 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke return img; } - protected SectionTitle newSectionTitle(TextSection parent, Node node) throws RepositoryException { - SectionTitle title = new SectionTitle(parent.getHeader(), parent.getStyle(), node.getProperty(JCR_TITLE)); + protected DocBookSectionTitle newSectionTitle(TextSection parent, Node titleNode) throws RepositoryException { + int style = parent.getStyle(); + if (parent.isTitleReadOnly()) + style = style | SWT.READ_ONLY; + DocBookSectionTitle title = new DocBookSectionTitle(parent.getHeader(), style, titleNode); updateContent(title); title.setMouseListener(getMouseListener()); return title; } - protected SectionTitle prepareSectionTitle(Section newSection, String titleText) throws RepositoryException { + protected DocBookSectionTitle prepareSectionTitle(Section newSection, String titleText) throws RepositoryException { Node sectionNode = newSection.getNode(); - if (!sectionNode.hasProperty(JCR_TITLE)) - sectionNode.setProperty(Property.JCR_TITLE, ""); - getTextInterpreter().write(sectionNode.getProperty(Property.JCR_TITLE), titleText); + Node titleNode; + if (!sectionNode.hasNode(DocBookTypes.TITLE)) { + titleNode = sectionNode.addNode(DocBookTypes.TITLE, DocBookTypes.TITLE); + } else { + titleNode = sectionNode.getNode(DocBookTypes.TITLE); + } + getTextInterpreter().write(titleNode, titleText); if (newSection.getHeader() == null) newSection.createHeader(); - SectionTitle sectionTitle = newSectionTitle((TextSection) newSection, sectionNode); + DocBookSectionTitle sectionTitle = newSectionTitle((TextSection) newSection, sectionNode); return sectionTitle; } @@ -220,14 +224,14 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke EditableImage editableImage = (EditableImage) part; imageManager.load(partNode, part.getControl(), editableImage.getPreferredImageSize()); } - } else if (part instanceof SectionTitle) { - SectionTitle title = (SectionTitle) part; + } else if (part instanceof DocBookSectionTitle) { + DocBookSectionTitle title = (DocBookSectionTitle) part; title.setStyle(title.getSection().getTitleStyle()); // use control AFTER setting style if (title == getEdited()) - title.setText(textInterpreter.read(title.getProperty())); + title.setText(textInterpreter.read(title.getNode())); else - title.setText(textInterpreter.raw(title.getProperty())); + title.setText(textInterpreter.raw(title.getNode())); } } @@ -236,6 +240,8 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke protected void save(EditablePart part) throws RepositoryException { if (part instanceof EditableText) { EditableText et = (EditableText) part; + if (!et.getEditable()) + return; String text = ((Text) et.getControl()).getText(); String[] lines = text.split("[\r\n]+"); @@ -267,11 +273,11 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke currentParagraph = newParagraph; currentParagraphN = newNode; } - persistChanges(sectionNode); } // TODO or rather return the created paragarphs? layout(toLayout.toArray(new Control[toLayout.size()])); } + persistChanges(et.getNode()); } } @@ -383,8 +389,8 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke Paragraph secondParagraph = paragraphSplitted(paragraph, secondNode); edit(secondParagraph, 0); - } else if (getEdited() instanceof SectionTitle) { - SectionTitle sectionTitle = (SectionTitle) getEdited(); + } else if (getEdited() instanceof DocBookSectionTitle) { + DocBookSectionTitle sectionTitle = (DocBookSectionTitle) getEdited(); Text text = (Text) sectionTitle.getControl(); String txt = text.getText(); int caretPosition = text.getCaretPosition(); @@ -394,7 +400,7 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke // paragraphNode.addMixin(CmsTypes.CMS_STYLED); textInterpreter.write(paragraphNode, txt.substring(caretPosition)); - textInterpreter.write(sectionNode.getProperty(Property.JCR_TITLE), txt.substring(0, caretPosition)); + textInterpreter.write(sectionNode.getNode(DocBookTypes.TITLE), txt.substring(0, caretPosition)); sectionNode.orderBefore(p(paragraphNode.getIndex()), p(1)); persistChanges(sectionNode); @@ -509,13 +515,13 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke Node sectionNode = section.getNode(); // main title if (section == mainSection && section instanceof TextSection && paragraphNode.getIndex() == 1 - && !sectionNode.hasProperty(JCR_TITLE)) { - SectionTitle sectionTitle = prepareSectionTitle(section, txt); + && !sectionNode.hasNode(DocBookTypes.TITLE)) { + DocBookSectionTitle sectionTitle = prepareSectionTitle(section, txt); edit(sectionTitle, 0); return; } Node newSectionNode = sectionNode.addNode(DocBookNames.DBK_SECTION, DocBookTypes.SECTION); - newSectionNode.addMixin(NodeType.MIX_TITLE); + // newSectionNode.addMixin(NodeType.MIX_TITLE); sectionNode.orderBefore(h(newSectionNode.getIndex()), h(1)); int paragraphIndex = paragraphNode.getIndex(); @@ -529,9 +535,9 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke if (sp instanceof Control) ((Control) sp).dispose(); } - // create property - newSectionNode.setProperty(Property.JCR_TITLE, ""); - getTextInterpreter().write(newSectionNode.getProperty(Property.JCR_TITLE), txt); + // create title + Node titleNode = newSectionNode.addNode(DocBookTypes.TITLE, DocBookTypes.TITLE); + getTextInterpreter().write(titleNode, txt); TextSection newSection = new TextSection(section, section.getStyle(), newSectionNode); newSection.setLayoutData(CmsUiUtils.fillWidth()); @@ -545,8 +551,8 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke newSection.getParent().layout(); layout(newSection); persistChanges(sectionNode); - } else if (getEdited() instanceof SectionTitle) { - SectionTitle sectionTitle = (SectionTitle) getEdited(); + } else if (getEdited() instanceof DocBookSectionTitle) { + DocBookSectionTitle sectionTitle = (DocBookSectionTitle) getEdited(); Section section = sectionTitle.getSection(); Section parentSection = section.getParentSection(); if (parentSection == null) @@ -602,7 +608,7 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke // newParagrapheNode.addMixin(CmsTypes.CMS_STYLED); if (mergedHasSubSections) mergedNode.orderBefore(p(newParagrapheNode.getIndex()), h(1)); - String txt = getTextInterpreter().read(sectionNode.getProperty(Property.JCR_TITLE)); + String txt = getTextInterpreter().read(sectionNode.getNode(DocBookTypes.TITLE)); getTextInterpreter().write(newParagrapheNode, txt); // move NodeIterator paragraphs = sectionNode.getNodes(DocBookNames.DBK_PARA); @@ -650,7 +656,8 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke return newParagraph; } - protected Paragraph sectionTitleSplitted(SectionTitle sectionTitle, Node newNode) throws RepositoryException { + protected Paragraph sectionTitleSplitted(DocBookSectionTitle sectionTitle, Node newNode) + throws RepositoryException { updateContent(sectionTitle); Paragraph newParagraph = newParagraph(sectionTitle.getSection(), newNode); // we assume beforeFirst is not null since there was a sectionTitle diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/CustomDbkEditor.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/CustomDbkEditor.java index 801cd1c..561b1e1 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/CustomDbkEditor.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/CustomDbkEditor.java @@ -13,12 +13,11 @@ import org.eclipse.swt.widgets.Composite; public class CustomDbkEditor extends AbstractDbkViewer { private static final long serialVersionUID = 656302500183820802L; - protected CustomDbkEditor(Composite parent, int style, Node textNode, CmsEditable cmsEditable) { + public CustomDbkEditor(Composite parent, int style, Node textNode, CmsEditable cmsEditable) { this(new Section(parent, style, textNode), style, cmsEditable); } - protected CustomDbkEditor(Section parent, int style, CmsEditable cmsEditable) { + public CustomDbkEditor(Section parent, int style, CmsEditable cmsEditable) { super(parent, style, cmsEditable); } - } diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkTextInterpreter.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkTextInterpreter.java index 51af55e..6ca5025 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkTextInterpreter.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkTextInterpreter.java @@ -18,7 +18,7 @@ public class DbkTextInterpreter implements TextInterpreter { try { if (item instanceof Node) { Node node = (Node) item; - if (node.isNodeType(DocBookTypes.PARA)) { + if (node.isNodeType(DocBookTypes.PARA)||node.isNodeType(DocBookTypes.TITLE)) { String raw = convertToStorage(node, content); validateBeforeStoring(raw); Node jcrText; @@ -56,7 +56,7 @@ public class DbkTextInterpreter implements TextInterpreter { item.getSession().refresh(true); if (item instanceof Node) { Node node = (Node) item; - if (node.isNodeType(DocBookTypes.PARA)) { + if (node.isNodeType(DocBookTypes.PARA)||node.isNodeType(DocBookTypes.TITLE)) { Node jcrText = node.getNode(Jcr.JCR_XMLTEXT); String txt = jcrText.getProperty(Jcr.JCR_XMLCHARACTERS).getString(); // TODO make it more robust diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkUtils.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkUtils.java index b53c305..ee675d2 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkUtils.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkUtils.java @@ -1,7 +1,10 @@ package org.argeo.docbook.ui; import javax.jcr.Node; +import javax.jcr.RepositoryException; +import org.argeo.jcr.JcrException; +import org.argeo.jcr.JcrUtils; import org.argeo.jcr.JcrxApi; /** Utilities around DocBook. */ @@ -10,6 +13,25 @@ public class DbkUtils { return JcrxApi.getXmlValue(node, DocBookTypes.TITLE); } + public static void setTitle(Node node, String txt) { + try { + Node titleNode = JcrUtils.getOrAdd(node, DocBookTypes.TITLE, DocBookTypes.TITLE); + JcrxApi.setXmlValue(node, titleNode, txt); + } catch (RepositoryException e) { + throw new JcrException("Cannot add empty paragraph to " + node, e); + } + } + + public static Node addParagraph(Node node, String txt) { + try { + Node para = node.addNode(DocBookTypes.PARA, DocBookTypes.PARA); + JcrxApi.setXmlValue(node, para, txt); + return para; + } catch (RepositoryException e) { + throw new JcrException("Cannot add empty paragraph to " + node, e); + } + } + /** Singleton. */ private DbkUtils() { } diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DocBookSectionTitle.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DocBookSectionTitle.java new file mode 100644 index 0000000..fcde875 --- /dev/null +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DocBookSectionTitle.java @@ -0,0 +1,33 @@ +package org.argeo.docbook.ui; + +import javax.jcr.Node; +import javax.jcr.Property; +import javax.jcr.RepositoryException; + +import org.argeo.cms.text.TextSection; +import org.argeo.cms.ui.viewers.EditablePart; +import org.argeo.cms.ui.viewers.NodePart; +import org.argeo.cms.ui.widgets.EditableText; +import org.eclipse.swt.widgets.Composite; + +/** The title of a section, based on an XML text node. */ +public class DocBookSectionTitle extends EditableText implements EditablePart, NodePart { + private static final long serialVersionUID = -1787983154946583171L; + + private final TextSection section; + + public DocBookSectionTitle(Composite parent, int swtStyle, Node titleNode) throws RepositoryException { + super(parent, swtStyle, titleNode); + section = (TextSection) TextSection.findSection(this); + } + + public TextSection getSection() { + return section; + } + + @Override + public Node getItem() throws RepositoryException { + return getNode(); + } + +} diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DocBookTypes.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DocBookTypes.java index 03721b2..aa37c16 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DocBookTypes.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DocBookTypes.java @@ -4,6 +4,7 @@ public interface DocBookTypes { public final static String BOOK = "dbk:book"; public final static String ARTICLE = "dbk:article"; public final static String TITLE = "dbk:title"; + public final static String INFO = "dbk:info"; public final static String SECTION = "dbk:section"; public final static String PARA = "dbk:para"; // public final static String XMLTEXT = "dbk:xmltext"; diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/docbook.cnd b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/docbook.cnd index 0398c25..3c44533 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/docbook.cnd +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/docbook.cnd @@ -255,6 +255,7 @@ orderable + dbk:orgname (dbk:orgname) = dbk:orgname * + dbk:subtitle (dbk:subtitle) = dbk:subtitle * + dbk:title (dbk:title) = dbk:title * + + * (nt:base) = nt:unstructured * [dbk:inlinemediaobject] > argeodbk:base, argeodbk:linkingAttributes + dbk:alt (dbk:alt) = dbk:alt