Skip to content

Commit

Permalink
Completed work on a CommonMark-based markdown unit test suite.
Browse files Browse the repository at this point in the history
Cleaned up markup handling code and fixed a bunch of bugs. Moved parsing from the adapters to the data classes.
Consolidated Flexmark extension classes for p tag and quote handling.
Fixed newline handling in XML markup generation.
Fixed bug related to vsch/flexmark-java#557.
Fixed table indentation.
  • Loading branch information
david-waltermire committed Jun 26, 2023
1 parent 7ac7eb4 commit 34b6183
Show file tree
Hide file tree
Showing 58 changed files with 2,931 additions and 3,227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.ctc.wstx.api.WstxOutputProperties;
import com.ctc.wstx.stax.WstxOutputFactory;

import gov.nist.secauto.metaschema.model.common.datatype.markup.IMarkupAdapter;
import gov.nist.secauto.metaschema.model.common.datatype.markup.IMarkupString;

import org.codehaus.stax2.XMLOutputFactory2;
Expand Down Expand Up @@ -87,7 +86,7 @@ public Object exec(@SuppressWarnings("rawtypes") List arguments) throws Template
List.of(NamespaceEventImpl.constructNamespace(null, prefix != null ? prefix : "", namespace)));
xmlStreamWriter.setNamespaceContext(nsContext);

IMarkupAdapter.writeHtml(text, namespace, xmlStreamWriter);
text.writeXHtml(namespace, xmlStreamWriter);

xmlStreamWriter.flush();
return os.toString(StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public boolean write(Object instance, QName parentName, IXmlWritingContext conte
throws XMLStreamException, IOException {
Object value = getValue(instance);
if (value != null) {
getJavaTypeAdapter().writeXmlCharacters(value, parentName, context.getWriter());
getJavaTypeAdapter().writeXmlValue(value, parentName, context.getWriter());
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public Object get(Object parentInstance, StartElement start, IXmlParsingContext
@Override
public void accept(Object item, QName currentParentName, IXmlWritingContext context)
throws IOException, XMLStreamException {
getJavaTypeAdapter().writeXmlCharacters(item, currentParentName, context.getWriter());
getJavaTypeAdapter().writeXmlValue(item, currentParentName, context.getWriter());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import gov.nist.secauto.metaschema.binding.model.annotations.XmlNsForm;
import gov.nist.secauto.metaschema.binding.model.annotations.XmlSchema;
import gov.nist.secauto.metaschema.model.common.datatype.markup.IMarkupString;
import gov.nist.secauto.metaschema.model.common.datatype.markup.flexmark.FlexmarkConfiguration;
import gov.nist.secauto.metaschema.model.common.util.ObjectUtils;

import java.io.IOException;
Expand Down Expand Up @@ -96,7 +97,7 @@ public PackageProductionImpl(@NonNull String javaPackage, @NonNull URI xmlNamesp
}

public static String formatMarkdown(@NonNull IMarkupString<?> markup) {
Formatter.Builder builder = Formatter.builder();
Formatter.Builder builder = Formatter.builder(FlexmarkConfiguration.FLEXMARK_CONFIG);
builder.set(ObjectUtils.notNull(Formatter.FORMAT_FLAGS), LineAppendable.F_WHITESPACE_REMOVAL);
// builder.set(Formatter.ESCAPE_SPECIAL_CHARS, false);
Formatter formatter = builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public String asString(Object value) {
}

@Override
public void writeXml(Object value, StartElement parent, XMLEventFactory2 eventFactory, XMLEventWriter eventWriter)
public void writeXmlValue(Object value, StartElement parent, XMLEventFactory2 eventFactory, XMLEventWriter eventWriter)
throws IOException, XMLStreamException {
try {
String content = asString(value);
Expand All @@ -171,7 +171,7 @@ public void writeXml(Object value, StartElement parent, XMLEventFactory2 eventFa
}

@Override
public void writeXmlCharacters(Object value, QName parentName, XMLStreamWriter2 writer) throws XMLStreamException {
public void writeXmlValue(Object value, QName parentName, XMLStreamWriter2 writer) throws XMLStreamException {
String content = asString(value);
writer.writeCharacters(content);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public interface IDataTypeAdapter<TYPE> {

/**
* The JSON primative type of the data type.
*
* @return the JSON data type
*/
JsonFormatTypes getJsonRawType();

Expand Down Expand Up @@ -314,14 +316,14 @@ default Supplier<TYPE> parseAndSupply(@NonNull JsonParser parser) throws IOExcep
* @throws IOException
* if an unexpected error occurred while writing to the output stream
*/
void writeXml(@NonNull Object instance, @NonNull StartElement parent, @NonNull XMLEventFactory2 eventFactory,
void writeXmlValue(@NonNull Object instance, @NonNull StartElement parent, @NonNull XMLEventFactory2 eventFactory,
@NonNull XMLEventWriter eventWriter)
throws IOException, XMLStreamException;

/**
* Writes the provided Java class instance data as an XML character sequence. The parent element
* information is provided as an XML {@link QName}, which allows namespace information to be
* obtained from the parent element. Additional namespace information can be gathered using the
* Writes the provided Java class instance data as XML. The parent element information is provided
* as an XML {@link QName}, which allows namespace information to be obtained from the parent
* element. Additional namespace information can be gathered using the
* {@link XMLStreamWriter2#getNamespaceContext()} method, which can be used when writing the
* provided instance value.
*
Expand All @@ -334,7 +336,7 @@ void writeXml(@NonNull Object instance, @NonNull StartElement parent, @NonNull X
* @throws XMLStreamException
* if an unexpected error occurred while processing the XML output
*/
void writeXmlCharacters(@NonNull Object instance, @NonNull QName parentName, @NonNull XMLStreamWriter2 writer)
void writeXmlValue(@NonNull Object instance, @NonNull QName parentName, @NonNull XMLStreamWriter2 writer)
throws XMLStreamException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;

import gov.nist.secauto.metaschema.model.common.datatype.AbstractCustomJavaDataTypeAdapter;
import gov.nist.secauto.metaschema.model.common.metapath.item.IMarkupItem;
Expand All @@ -47,18 +46,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;

public abstract class AbstractMarkupAdapter<TYPE extends IMarkupString<TYPE>>
extends AbstractCustomJavaDataTypeAdapter<TYPE, IMarkupItem> implements IMarkupAdapter<TYPE> {

private static final MarkupParser MARKUP_PARSER = new MarkupParser();

/**
* Gets the markup parser used to read and write markup.
*
* @return the parser
*/
protected static MarkupParser getMarkupParser() {
return MARKUP_PARSER;
}
extends AbstractCustomJavaDataTypeAdapter<TYPE, IMarkupItem> {

/**
* Construct a new adapter.
Expand All @@ -80,50 +68,39 @@ public boolean isXmlMixed() {
return true;
}


// TODO: verify that read/write methods cannot be generalized in the base class
@Override
public void writeXml(Object value, StartElement parent, XMLEventFactory2 eventFactory, XMLEventWriter eventWriter)
public void writeXmlValue(Object value, StartElement parent, XMLEventFactory2 eventFactory, XMLEventWriter eventWriter)
throws XMLStreamException {

IMarkupWriter<XMLEventWriter, XMLStreamException> writer = new MarkupXmlEventWriter(
ObjectUtils.notNull(parent.getName().getNamespaceURI()),
eventWriter,
eventFactory);
IMarkupString<?> markupString = (IMarkupString<?>) value;

IMarkupString<?> markupString = (IMarkupString<?>)value;

IMarkupVisitor<XMLEventWriter, XMLStreamException> visitor = new MarkupVisitor<>(markupString.isBlock());
visitor.visitDocument(markupString.getDocument(), writer);
markupString.writeXHtml(
ObjectUtils.notNull(parent.getName().getNamespaceURI()),
eventFactory,
eventWriter);
}

@Override
public void writeXmlCharacters(Object value, QName parentName, XMLStreamWriter2 streamWriter) throws XMLStreamException {
IMarkupString<?> markupString = (IMarkupString<?>)value;
public void writeXmlValue(Object value, QName parentName, XMLStreamWriter2 streamWriter)
throws XMLStreamException {
IMarkupString<?> markupString = (IMarkupString<?>) value;

IMarkupAdapter.writeHtml(
markupString,
markupString.writeXHtml(
ObjectUtils.notNull(parentName.getNamespaceURI()),
streamWriter);
}

@Override
public void writeJsonValue(Object value, JsonGenerator generator) throws IOException {

IMarkupString<?> markupString;
try {
markupString = (IMarkupString<?>)value;
markupString = (IMarkupString<?>) value;
} catch (ClassCastException ex) {
throw new IOException(ex);
}

String jsonString;
if (generator instanceof YAMLGenerator) {
jsonString = markupString.toMarkdownYaml().trim();
} else {
jsonString = markupString.toMarkdown().trim();
}
generator.writeString(jsonString);
generator.writeString(markupString.toMarkdown().trim());
}

}

This file was deleted.

Loading

0 comments on commit 34b6183

Please sign in to comment.