X4O: Formatted the SAX3 package to eclipse-160 (code+comment)

This commit is contained in:
Willem Cazander 2025-11-07 21:35:41 +01:00
parent 2819b36a45
commit a61ce241e1
26 changed files with 1192 additions and 909 deletions

View file

@ -20,7 +20,7 @@
* 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.
*/
package org.x4o.sax3;
package org.x4o.sax3;
import java.io.Closeable;
import java.io.IOException;
@ -38,16 +38,16 @@ import org.xml.sax.helpers.AttributesImpl;
*
* @author Willem Cazander
* @version 1.0 May 3, 2013
* @param <TAG> The enum for the XML tag values.
* @param <TAG> The enum for the XML tag values.
* @param <TAG_WRITER> The tag writer to output to.
*/
public class SAX3WriterEnum<TAG extends Enum<?>,TAG_WRITER extends ContentWriter> implements SAX3WriterEnumHammer<TAG>, Closeable {
public class SAX3WriterEnum<TAG extends Enum<?>, TAG_WRITER extends ContentWriter> implements SAX3WriterEnumHammer<TAG>, Closeable {
private final Attributes EMPTY_ATTRIBUTES = new AttributesImpl();
private final TAG_WRITER contentWriter;
private final String tagNamespaceUri;
private final String tagNamespacePrefix;
public SAX3WriterEnum(TAG_WRITER contentWriter) {
this(contentWriter, SAX3XMLConstants.NULL_NS_URI, SAX3XMLConstants.NULL_NS_URI);
}
@ -57,20 +57,20 @@ public class SAX3WriterEnum<TAG extends Enum<?>,TAG_WRITER extends ContentWriter
this.tagNamespaceUri = Objects.requireNonNull(tagNamespaceUri, "Can't create wrapper with null tagNamespaceUri");
this.tagNamespacePrefix = Objects.requireNonNull(tagNamespacePrefix, "Can't create wrapper with null tagNamespacePrefix");
}
@Override
public void close() throws IOException {
contentWriter.close();
}
public TAG_WRITER getContentWriterWrapped() {
return contentWriter;
}
public String getTagNamespaceUri() {
return tagNamespaceUri;
}
public void startDocument() throws IOException {
try {
contentWriter.startDocument();
@ -79,7 +79,7 @@ public class SAX3WriterEnum<TAG extends Enum<?>,TAG_WRITER extends ContentWriter
throw new IOException(e);
}
}
public void endDocument() throws IOException {
try {
contentWriter.endDocument();
@ -87,30 +87,30 @@ public class SAX3WriterEnum<TAG extends Enum<?>,TAG_WRITER extends ContentWriter
throw new IOException(e);
}
}
public ContentCloseable printTag(TAG tag) throws IOException {
return printTag(tag, EMPTY_ATTRIBUTES);
}
public ContentCloseable printTag(TAG tag, Attributes atts) throws IOException {
printTagStart(tag, atts);
return () -> printTagEnd(tag);
}
public void printTagStartEnd(TAG tag, Attributes atts) throws IOException {
printTagStart(tag, atts);
printTagEnd(tag);
}
public void printTagStartEnd(TAG tag) throws IOException {
printTagStart(tag);
printTagEnd(tag);
}
public void printTagStart(TAG tag) throws IOException {
printTagStart(tag, EMPTY_ATTRIBUTES);
}
public void printTagStart(TAG tag, Attributes atts) throws IOException {
try {
contentWriter.startElement(getTagNamespaceUri(), toTagString(tag), "", atts);
@ -118,15 +118,15 @@ public class SAX3WriterEnum<TAG extends Enum<?>,TAG_WRITER extends ContentWriter
throw new IOException(e);
}
}
public void printTagEnd(TAG tag) throws IOException {
try {
contentWriter.endElement(getTagNamespaceUri(),toTagString(tag) , "");
contentWriter.endElement(getTagNamespaceUri(), toTagString(tag), "");
} catch (SAXException e) {
throw new IOException(e);
}
}
private String toTagString(TAG tag) {
String result = tag.name();
if (result.startsWith("_")) {
@ -134,7 +134,7 @@ public class SAX3WriterEnum<TAG extends Enum<?>,TAG_WRITER extends ContentWriter
}
return result;
}
public void printTagCharacters(TAG tag, String text) throws IOException {
printTagStart(tag);
if (text == null) {
@ -143,7 +143,7 @@ public class SAX3WriterEnum<TAG extends Enum<?>,TAG_WRITER extends ContentWriter
printCharacters(text);
printTagEnd(tag);
}
public void printCharacters(String text) throws IOException {
try {
contentWriter.characters(text);
@ -151,7 +151,7 @@ public class SAX3WriterEnum<TAG extends Enum<?>,TAG_WRITER extends ContentWriter
throw new IOException(e);
}
}
public void printComment(String text) throws IOException {
try {
contentWriter.comment(text);

View file

@ -20,7 +20,7 @@
* 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.
*/
package org.x4o.sax3;
package org.x4o.sax3;
import java.io.IOException;
@ -34,26 +34,26 @@ import org.xml.sax.Attributes;
* @param <TAG> The enum for the XML tag values.
*/
public interface SAX3WriterEnumHammer<TAG extends Enum<?>> {
String getTagNamespaceUri();
void startDocument() throws IOException;
void endDocument() throws IOException;
void printTagStartEnd(TAG tag) throws IOException;
void printTagStartEnd(TAG tag, Attributes atts) throws IOException;
void printTagStart(TAG tag) throws IOException;
void printTagStart(TAG tag, Attributes atts) throws IOException;
void printTagEnd(TAG tag) throws IOException;
void printTagCharacters(TAG tag, String text) throws IOException;
void printCharacters(String text) throws IOException;
void printComment(String text) throws IOException;
}

View file

@ -20,7 +20,7 @@
* 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.
*/
package org.x4o.sax3;
package org.x4o.sax3;
import java.io.IOException;
import java.io.Writer;
@ -38,16 +38,16 @@ import org.xml.sax.helpers.AttributesImpl;
* @author Willem Cazander
* @version 1.0 Apr 30, 2013
*/
public class SAX3WriterHtml extends SAX3WriterEnum<SAX3WriterHtml.Tag,SAX3WriterXml> {
public class SAX3WriterHtml extends SAX3WriterEnum<SAX3WriterHtml.Tag, SAX3WriterXml> {
public SAX3WriterHtml(Writer out, String encoding) {
super(new SAX3WriterXml(out, encoding), "", SAX3XMLConstants.NULL_NS_URI);
}
public SAX3PropertyConfig getPropertyConfig() {
return getContentWriterWrapped().getPropertyConfig();
}
public void printDocType(DocType doc) throws IOException {
try {
getContentWriterWrapped().startDTD(doc.getName(), doc.getPublicId(), doc.getSystemId());
@ -55,7 +55,7 @@ public class SAX3WriterHtml extends SAX3WriterEnum<SAX3WriterHtml.Tag,SAX3Writer
throw new IOException(e);
}
}
public void printHtmlStart(String language) throws IOException {
AttributesImpl atts = new AttributesImpl();
if (language != null) {
@ -63,16 +63,16 @@ public class SAX3WriterHtml extends SAX3WriterEnum<SAX3WriterHtml.Tag,SAX3Writer
}
printTagStart(Tag.html, atts);
}
public void printHtmlEnd() throws IOException {
printTagEnd(Tag.html);
}
public void printHeadMetaDate() throws IOException {
Calendar cal = Calendar.getInstance();
printHeadMeta("date", cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH)+1) + "-" + cal.get(Calendar.DAY_OF_MONTH));
printHeadMeta("date", cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH));
}
public void printHeadTitle(String title) throws IOException {
printTagCharacters(Tag.title, title);
}
@ -84,14 +84,14 @@ public class SAX3WriterHtml extends SAX3WriterEnum<SAX3WriterHtml.Tag,SAX3Writer
atts.addAttribute("", "charset", "", "", getPropertyConfig().getPropertyString(SAX3WriterXml.OUTPUT_ENCODING));
printTagStartEnd(Tag.meta, atts);
}
public void printHeadMeta(String name, String content) throws IOException {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "name", "", "", name);
atts.addAttribute("", "content", "", "", content);
printTagStartEnd(Tag.meta, atts);
}
public void printHeadLinkCss(String cssUrl) throws IOException {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "rel", "", "", "stylesheet");
@ -100,7 +100,7 @@ public class SAX3WriterHtml extends SAX3WriterEnum<SAX3WriterHtml.Tag,SAX3Writer
atts.addAttribute("", "href", "", "", cssUrl);
printTagStartEnd(Tag.link, atts);
}
public void printScriptSrc(String jsUrl) throws IOException {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "type", "", "", "text/javascript");
@ -109,7 +109,7 @@ public class SAX3WriterHtml extends SAX3WriterEnum<SAX3WriterHtml.Tag,SAX3Writer
printCharacters(" "); // NOTE: space needed to keep browsers happy
printTagEnd(Tag.script);
}
public void printScriptInline(String script) throws IOException {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "type", "", "", "text/javascript");
@ -117,20 +117,22 @@ public class SAX3WriterHtml extends SAX3WriterEnum<SAX3WriterHtml.Tag,SAX3Writer
printComment(script);
printTagEnd(Tag.script);
}
public void printScriptNoDiv() throws IOException {
printScriptNoDiv(null);
}
public void printScriptNoDiv(String text) throws IOException {
if (text == null) {
text = "JavaScript is disabled on your browser.";
}
printTagStart(Tag.noscript);
printTagStart(Tag.div);printCharacters(text);printTagEnd(Tag.div);
printTagStart(Tag.div);
printCharacters(text);
printTagEnd(Tag.div);
printTagEnd(Tag.noscript);
}
public void printHrefNamed(String name) throws IOException {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "name", "", "", name);
@ -138,7 +140,7 @@ public class SAX3WriterHtml extends SAX3WriterEnum<SAX3WriterHtml.Tag,SAX3Writer
printComment(" ");
printTagEnd(Tag.a);
}
public void printHrefTarget(String href, String target, String text) throws IOException {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "href", "", "", href);
@ -147,28 +149,28 @@ public class SAX3WriterHtml extends SAX3WriterEnum<SAX3WriterHtml.Tag,SAX3Writer
printCharacters(text);
printTagEnd(Tag.a);
}
public void printHref(String href, String text) throws IOException {
printHref(href, text, text);
}
public void printHref(String href, String text, String title) throws IOException {
printHref(href, text, title, null);
}
public void printHref(String href, String text, String title, String spanClass) throws IOException {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "href", "", "", href);
if (title != null) {
atts.addAttribute("", "title", "", "", title);
}
printTagStart(Tag.a,atts);
printTagStart(Tag.a, atts);
if (spanClass != null) {
atts = new AttributesImpl();
if (spanClass.length() > 0) {
atts.addAttribute("", "class", "", "", spanClass);
}
printTagStart(Tag.span,atts);
printTagStart(Tag.span, atts);
}
printCharacters(text);
if (spanClass != null) {
@ -176,11 +178,11 @@ public class SAX3WriterHtml extends SAX3WriterEnum<SAX3WriterHtml.Tag,SAX3Writer
}
printTagEnd(Tag.a);
}
public void printTagCharacters(Tag tag, String text, String tagClass) throws IOException {
printTagCharacters(tag, text, tagClass, null);
}
public void printTagCharacters(Tag tag, String text, String tagClass, String tagId) throws IOException {
printTagStart(tag, tagClass, tagId, null);
if (text == null) {
@ -189,44 +191,44 @@ public class SAX3WriterHtml extends SAX3WriterEnum<SAX3WriterHtml.Tag,SAX3Writer
printCharacters(text);
printTagEnd(tag);
}
public ContentCloseable printTag(Tag tag, String tagClass) throws IOException {
return printTag(tag, tagClass, null, null);
}
public ContentCloseable printTag(Tag tag, Enum<?> tagClassEnum) throws IOException {
return printTag(tag, tagClassEnum, null);
}
public ContentCloseable printTag(Tag tag, Enum<?> tagClassEnum, String tagId) throws IOException {
return printTag(tag, tagClassEnum, tagId, null);
}
public ContentCloseable printTag(Tag tag, Enum<?> tagClassEnum, String tagId, String typeId) throws IOException {
return printTag(tag, tagClassEnum.name(), tagId, typeId);
}
public ContentCloseable printTag(Tag tag, String tagClass, String tagId, String typeId) throws IOException {
printTagStart(tag, tagClass, tagId, typeId);
return () -> printTagEnd(tag);
}
public void printTagStart(Tag tag, String tagClass) throws IOException {
printTagStart(tag, tagClass, null, null);
}
public void printTagStart(Tag tag, Enum<?> tagClassEnum) throws IOException {
printTagStart(tag, tagClassEnum, null);
}
public void printTagStart(Tag tag, Enum<?> tagClassEnum, String tagId) throws IOException {
printTagStart(tag, tagClassEnum, tagId, null);
}
public void printTagStart(Tag tag, Enum<?> tagClassEnum, String tagId, String typeId) throws IOException {
printTagStart(tag, tagClassEnum.name(), tagId, typeId);
}
public void printTagStart(Tag tag, String tagClass, String tagId, String typeId) throws IOException {
AttributesImpl atts = new AttributesImpl();
if (tagId != null && tagId.length() > 0) {
@ -240,80 +242,180 @@ public class SAX3WriterHtml extends SAX3WriterEnum<SAX3WriterHtml.Tag,SAX3Writer
}
printTagStart(tag, atts);
}
public enum Tag {
/* Deprecated TAGS */
frameset,frame,noframes,tt,font,dir,center,strike,
big,basefont,acronym,applet,iframe,
frameset,
frame,
noframes,
tt,
font,
dir,
center,
strike,
big,
basefont,
acronym,
applet,
iframe,
/* HTML 4 TAGS */
html,head,title,meta,link,base,body,script,style,
h1,h2,h3,h4,h5,h6,
a,div,span,p,pre,img,hr,br,
b,em,strong,small,noscript,
ul,li,dl,dt,dd,ol,
table,thead,tfoot,tbody,caption,th,tr,td,
abbr,address,area,bdo,blockquote,
cite,code,col,colgroup,del,dfn,i,ins,
kbd,legend,map,menu,object,param,
optgroup,q,s,samp,sub,u,var,
form,fieldset,input,option,
label,button,select,textarea,
html,
head,
title,
meta,
link,
base,
body,
script,
style,
h1,
h2,
h3,
h4,
h5,
h6,
a,
div,
span,
p,
pre,
img,
hr,
br,
b,
em,
strong,
small,
noscript,
ul,
li,
dl,
dt,
dd,
ol,
table,
thead,
tfoot,
tbody,
caption,
th,
tr,
td,
abbr,
address,
area,
bdo,
blockquote,
cite,
code,
col,
colgroup,
del,
dfn,
i,
ins,
kbd,
legend,
map,
menu,
object,
param,
optgroup,
q,
s,
samp,
sub,
u,
var,
form,
fieldset,
input,
option,
label,
button,
select,
textarea,
/* HTML 5 TAGS */
canvas,audio,video,source,embed,track,
datalist,keygen,output,
article,aside,bdi,command,details,dialog,summary,
figure,figcaption,footer,header,hgroup,mark,meter,
nav,progress,ruby,rt,rp,section,time,wbr,
;
canvas,
audio,
video,
source,
embed,
track,
datalist,
keygen,
output,
article,
aside,
bdi,
command,
details,
dialog,
summary,
figure,
figcaption,
footer,
header,
hgroup,
mark,
meter,
nav,
progress,
ruby,
rt,
rp,
section,
time,
wbr,;
}
private final static String DOCTYPE_NAME = "HTML PUBLIC";
public enum DocType {
/* Order from worst to better. */
HTML_5("html","",""),
HTML_4_FRAMESET(DOCTYPE_NAME,"\"-//W3C//DTD HTML 4.01 Frameset//EN\"","http://www.w3.org/TR/html4/frameset.dtd"),
HTML_4_TRANSITIONAL(DOCTYPE_NAME,"\"-//W3C//DTD HTML 4.01 Transitional//EN\"","http://www.w3.org/TR/html4/loose.dtd"),
HTML_4_STRICT(DOCTYPE_NAME,"\"-//W3C//DTD HTML 4.01//EN\"","http://www.w3.org/TR/html4/strict.dtd"),
HTML_5("html", "", ""),
HTML_4_FRAMESET(DOCTYPE_NAME, "\"-//W3C//DTD HTML 4.01 Frameset//EN\"", "http://www.w3.org/TR/html4/frameset.dtd"),
HTML_4_TRANSITIONAL(DOCTYPE_NAME, "\"-//W3C//DTD HTML 4.01 Transitional//EN\"", "http://www.w3.org/TR/html4/loose.dtd"),
HTML_4_STRICT(DOCTYPE_NAME, "\"-//W3C//DTD HTML 4.01//EN\"", "http://www.w3.org/TR/html4/strict.dtd"),
XHTML_1_FRAMESET(DOCTYPE_NAME, "\"-//W3C//DTD XHTML 1.0 Frameset//EN\"", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"),
XHTML_1_TRANSITIONAL(DOCTYPE_NAME, "\"-//W3C//DTD XHTML 1.0 Transitional//EN\"", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"),
XHTML_1_STRICT(DOCTYPE_NAME, "\"-//W3C//DTD XHTML 1.0 Strict//EN\"", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"),
XHTML_11(DOCTYPE_NAME, "\"-//W3C//DTD XHTML 1.1//EN\"", "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"),;
XHTML_1_FRAMESET(DOCTYPE_NAME,"\"-//W3C//DTD XHTML 1.0 Frameset//EN\"","http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"),
XHTML_1_TRANSITIONAL(DOCTYPE_NAME,"\"-//W3C//DTD XHTML 1.0 Transitional//EN\"","http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"),
XHTML_1_STRICT(DOCTYPE_NAME,"\"-//W3C//DTD XHTML 1.0 Strict//EN\"","http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"),
XHTML_11(DOCTYPE_NAME,"\"-//W3C//DTD XHTML 1.1//EN\"","http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"),
;
private final String name;
private final String publicId;
private final String systemId;
private DocType(String name, String publicId, String systemId) {
this.name = name;
this.publicId = publicId;
this.systemId = systemId;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the publicId
*/
public String getPublicId() {
return publicId;
}
/**
* @return the systemId
*/

View file

@ -20,7 +20,7 @@
* 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.
*/
package org.x4o.sax3;
package org.x4o.sax3;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
@ -36,29 +36,32 @@ import org.x4o.sax3.io.SAX3XMLConstants;
* @author Willem Cazander
* @version 1.0 Apr 17, 2005
*/
public class SAX3WriterXml extends AbstractContentWriter {
public class SAX3WriterXml extends AbstractContentWriter {
/**
* Creates XmlWriter which prints to the Writer interface.
* @param out The writer to print the xml to.
*
* @param out The writer to print the xml to.
*/
public SAX3WriterXml(Writer out, String encoding) {
super(out);
getPropertyConfig().setProperty(OUTPUT_ENCODING, encoding);
}
/**
* Creates XmlWriter which prints to the Writer interface.
* @param out The writer to print the xml to.
*
* @param out The writer to print the xml to.
*/
public SAX3WriterXml(Writer out) {
this(out,null);
this(out, null);
}
/**
* Creates XmlWriter which prints to the OutputStream interface.
* @param out The OutputStream to write to.
* @throws UnsupportedEncodingException Is thrown when UTF-8 can't we printed.
*
* @param out The OutputStream to write to.
* @throws UnsupportedEncodingException Is thrown when UTF-8 can't we printed.
*/
public SAX3WriterXml(OutputStream out) throws UnsupportedEncodingException {
this(new OutputStreamWriter(out, SAX3XMLConstants.XML_DEFAULT_ENCODING), SAX3XMLConstants.XML_DEFAULT_ENCODING);
@ -66,11 +69,12 @@ public class SAX3WriterXml extends AbstractContentWriter {
/**
* Creates XmlWriter which prints to the OutputStream interface.
* @param out The OutputStream to write to.
* @param encoding The OutputStream encoding.
* @throws UnsupportedEncodingException Is thrown when UTF-8 can't we printed.
*
* @param out The OutputStream to write to.
* @param encoding The OutputStream encoding.
* @throws UnsupportedEncodingException Is thrown when UTF-8 can't we printed.
*/
public SAX3WriterXml(OutputStream out,String encoding) throws UnsupportedEncodingException {
public SAX3WriterXml(OutputStream out, String encoding) throws UnsupportedEncodingException {
this(new OutputStreamWriter(out, encoding), encoding);
}
}

View file

@ -20,7 +20,7 @@
* 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.
*/
package org.x4o.sax3;
package org.x4o.sax3;
import java.io.IOException;
import java.io.Writer;
@ -35,23 +35,23 @@ import org.xml.sax.helpers.AttributesImpl;
* @author Willem Cazander
* @version 1.0 May 3, 2013
*/
public class SAX3WriterXsd extends SAX3WriterEnum<SAX3WriterXsd.Tag,SAX3WriterXml> {
public class SAX3WriterXsd extends SAX3WriterEnum<SAX3WriterXsd.Tag, SAX3WriterXml> {
public SAX3WriterXsd(Writer out, String encoding) {
super(new SAX3WriterXml(out, encoding), SAX3XMLConstants.XML_SCHEMA_NS_URI, SAX3XMLConstants.NULL_NS_URI);
}
public SAX3PropertyConfig getPropertyConfig() {
return getContentWriterWrapped().getPropertyConfig();
}
public void printXsdImport(String namespace, String schemaLocation) throws IOException {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "namespace", "", "", namespace);
atts.addAttribute("", "schemaLocation", "", "", schemaLocation);
printTagStartEnd(Tag._import, atts);
}
public void printXsdDocumentation(String description) throws IOException {
if (description == null) {
return;
@ -64,7 +64,7 @@ public class SAX3WriterXsd extends SAX3WriterEnum<SAX3WriterXsd.Tag,SAX3WriterXm
printTagEnd(Tag.documentation);
printTagEnd(Tag.annotation);
}
public void printXsdElementAttribute(String name, String type, String description) throws IOException {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "name", "", "", name);
@ -73,12 +73,37 @@ public class SAX3WriterXsd extends SAX3WriterEnum<SAX3WriterXsd.Tag,SAX3WriterXm
printXsdDocumentation(description);
printTagEnd(Tag.attribute);
}
public enum Tag {
all,annotation,any,anyAttribute,appinfo,attribute,attributeGroup,
choice,complexContent,complexType,documentation,element,extension,
field,group,_import,include,key,keyref,list,notation,
redefine,restriction,schema,selector,sequence,
simpleContent,simpleType,unoin,unique
all,
annotation,
any,
anyAttribute,
appinfo,
attribute,
attributeGroup,
choice,
complexContent,
complexType,
documentation,
element,
extension,
field,
group,
_import,
include,
key,
keyref,
list,
notation,
redefine,
restriction,
schema,
selector,
sequence,
simpleContent,
simpleType,
unoin,
unique
}
}

View file

@ -20,7 +20,7 @@
* 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.
*/
package org.x4o.sax3.io;
package org.x4o.sax3.io;
import java.io.Writer;
@ -34,17 +34,19 @@ import org.xml.sax.SAXException;
* @version 1.0 May 3, 2013
*/
public abstract class AbstractContentWriter extends AbstractContentWriterLexical implements ContentWriter {
/**
* Creates an Lecical content writer for XML.
* @param out The writer to write to.
*
* @param out The writer to write to.
*/
public AbstractContentWriter(Writer out) {
super(out);
}
/**
* Starts and end then element.
*
* @see org.x4o.sax3.io.ContentWriter#startElementEnd(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
public void startElementEnd(String uri, String localName, String name, Attributes atts) throws SAXException {

View file

@ -20,7 +20,7 @@
* 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.
*/
package org.x4o.sax3.io;
package org.x4o.sax3.io;
import java.io.BufferedReader;
import java.io.Closeable;
@ -53,19 +53,20 @@ import org.xml.sax.SAXException;
* @author Willem Cazander
* @version 1.0 May 3, 2013
*/
public class AbstractContentWriterHandler implements ContentHandler, Closeable {
public class AbstractContentWriterHandler implements ContentHandler, Closeable {
private final SAX3PropertyConfig propertyConfig;
private final Writer out;
private boolean started = false;
private int indent = 0;
private Map<String,String> prefixMapping = null;
private Map<String, String> prefixMapping = null;
private List<String> printedMappings = null;
private StringBuilder startElement = null;
private boolean printReturn = false;
private String lastElement = null;
private Stack<String> elements = null;
//@formatter:off
private final static String PROPERTY_CONTEXT_PREFIX = SAX3PropertyConfig.X4O_PROPERTIES_PREFIX + "content/"; // TODO: change to "writer/xml"
public final static SAX3PropertyConfig DEFAULT_PROPERTY_CONFIG;
public final static String OUTPUT_ENCODING = PROPERTY_CONTEXT_PREFIX + "output/encoding";
@ -105,19 +106,21 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
new PropertyConfigItem(ROOT_START_NAMESPACE_ALL, Boolean.class, true)
);
}
//@formatter:on
/**
* Creates XmlWriter which prints to the Writer interface.
* @param out The writer to print the xml to.
*
* @param out The writer to print the xml to.
*/
public AbstractContentWriterHandler(Writer out) {
this.out = Objects.requireNonNull(out, "Can't write on null writer.");
this.prefixMapping = new HashMap<String,String>(15);
this.prefixMapping = new HashMap<String, String>(15);
this.printedMappings = new ArrayList<String>(15);
this.elements = new Stack<String>();
this.propertyConfig = new SAX3PropertyConfig(DEFAULT_PROPERTY_CONFIG, PROPERTY_CONTEXT_PREFIX);
}
public SAX3PropertyConfig getPropertyConfig() {
return propertyConfig;
}
@ -126,7 +129,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
public void close() throws IOException {
out.close();
}
/**
* @see org.xml.sax.ContentHandler#startDocument()
*/
@ -140,7 +143,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
prologWriteLicence();
prologWriteUserComment();
}
private void prologWriteLicence() throws SAXException {
if (!propertyConfig.getPropertyBoolean(PROLOG_LICENCE_ENABLE)) {
return;
@ -177,7 +180,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
}
comment(licenceText);
}
private String readLicenceStream(InputStream inputStream, String encoding) throws IOException {
if (encoding == null) {
encoding = SAX3XMLConstants.XML_DEFAULT_ENCODING;
@ -203,7 +206,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
br.close();
}
}
private void prologWriteUserComment() throws SAXException {
if (!propertyConfig.getPropertyBoolean(PROLOG_USER_COMMENT_ENABLE)) {
return;
@ -220,7 +223,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
userComment = userComment.replaceAll("\n", chEnter + chTab);
comment(chEnter + chTab + userComment + chEnter);
}
/**
* @see org.xml.sax.ContentHandler#endDocument()
*/
@ -230,19 +233,19 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
throw new SAXException("Invalid xml still have " + elements.size() + " elements open.");
}
}
/**
* @param uri The xml namespace uri.
* @param localName The local name of the xml tag.
* @param name The (full) name of the xml tag.
* @param atts The attributes of the xml tag.
* @param uri The xml namespace uri.
* @param localName The local name of the xml tag.
* @param name The (full) name of the xml tag.
* @param atts The attributes of the xml tag.
* @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException {
if (localName == null) {
throw new SAXException("LocalName may not be null.");
}
if (SAX3XMLConstants.isNameString(localName)==false) {
if (SAX3XMLConstants.isNameString(localName) == false) {
throw new SAXException("LocalName of element is not valid in xml; '" + localName + "'");
}
for (int i = 0; i < atts.getLength(); i++) {
@ -272,8 +275,8 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
startElement.append(getPropertyConfig().getPropertyString(OUTPUT_CHAR_TAB));
}
startElement.append(SAX3XMLConstants.TAG_OPEN);
startElementTag(uri,localName);
startElementTag(uri, localName);
startElementNamespace(uri);
startElementNamespaceAll();
startElementAttributes(atts);
@ -282,7 +285,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
lastElement = localName;
elements.push(localName);
}
public void startElementTag(String uri, String localName) throws SAXException {
if (SAX3XMLConstants.NULL_NS_URI.equals(uri) || uri == null) {
startElement.append(localName);
@ -298,7 +301,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
startElement.append(localName);
}
}
public void startElementNamespace(String uri) throws SAXException {
if (uri == null) {
return;
@ -314,7 +317,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
throw new SAXException("preFixUri: " + uri + " is not started.");
}
printedMappings.add(uri);
startElement.append(' ');
startElement.append(SAX3XMLConstants.XMLNS_ATTRIBUTE);
if ("".equals(prefix) == false) {
@ -325,7 +328,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
startElement.append(uri);
startElement.append('"');
}
public void startElementNamespaceAll() throws SAXException {
if (!propertyConfig.getPropertyBoolean(ROOT_START_NAMESPACE_ALL)) {
return;
@ -360,14 +363,14 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
startElement.append(getPropertyConfig().getPropertyString(OUTPUT_CHAR_NEWLINE));
}
}
private void printElementAttributeNewLineSpace() {
startElement.append(SAX3XMLConstants.CHAR_NEWLINE);
for (int ii = 0; ii < indent+1; ii++) {
for (int ii = 0; ii < indent + 1; ii++) {
startElement.append(getPropertyConfig().getPropertyString(OUTPUT_CHAR_TAB));
}
}
private void startElementAttributes(Attributes atts) throws SAXException {
int prevChars = 0;
for (int i = 0; i < atts.getLength(); i++) {
@ -378,7 +381,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
attributeValue = propertyConfig.getPropertyString(OUTPUT_CHAR_NULL);
}
String attributeValueSafe = SAX3XMLConstants.escapeAttributeValue(attributeValue);
if (propertyConfig.getPropertyBoolean(OUTPUT_LINE_PER_ATTRIBUTE)) {
if (i == 0) {
printElementAttributeNewLineSpace();
@ -396,11 +399,11 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
startElement.append("=\"");
startElement.append(attributeValueSafe);
startElement.append('"');
if (propertyConfig.getPropertyBoolean(OUTPUT_LINE_PER_ATTRIBUTE)) {
printElementAttributeNewLineSpace();
}
int breakLines = propertyConfig.getPropertyInteger(OUTPUT_LINE_BREAK_WIDTH);
if (breakLines > 0) {
if (prevChars == 0 && startElement.length() > breakLines) {
@ -414,11 +417,11 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
}
}
}
/**
* @param uri The xml namespace uri.
* @param localName The local name of the xml tag.
* @param name The (full) name of the xml tag.
* @param uri The xml namespace uri.
* @param localName The local name of the xml tag.
* @param name The (full) name of the xml tag.
* @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
*/
public void endElement(String uri, String localName, String name) throws SAXException {
@ -426,16 +429,16 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
throw new SAXException("Unexpected end tag: " + localName + " should be: " + elements.peek());
}
elements.pop();
if (startElement != null) {
String tag = startElement.toString();
write(tag.substring(0,tag.length() - 1));// rm normal close
write(tag.substring(0, tag.length() - 1));// rm normal close
write(SAX3XMLConstants.TAG_CLOSE_EMPTY);
startElement = null;
indent--;
return;
}
indent--;
if (printReturn || !localName.equals(lastElement)) {
write(getPropertyConfig().getPropertyString(OUTPUT_CHAR_NEWLINE));
@ -446,7 +449,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
if (localName == null) {
localName = "null";
}
write(SAX3XMLConstants.TAG_OPEN_END);
if (SAX3XMLConstants.NULL_NS_URI.equals(uri) || uri == null) {
write(localName);
@ -466,25 +469,26 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
ignorableWhitespace(SAX3XMLConstants.CHAR_NEWLINE);
}
}
/**
* Starts the prefix mapping of an xml namespace uri.
* @param prefix The xml prefix of this xml namespace uri.
* @param uri The xml namespace uri to add the prefix for.
*
* @param prefix The xml prefix of this xml namespace uri.
* @param uri The xml namespace uri to add the prefix for.
* @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
*/
public void startPrefixMapping(String prefix, String uri) throws SAXException {
prefixMapping.put(uri, prefix);
}
/**
* @param prefix The xml prefix of this xml namespace uri to be ended.
* @param prefix The xml prefix of this xml namespace uri to be ended.
* @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
*/
public void endPrefixMapping(String prefix) throws SAXException {
Set<Map.Entry<String,String>> s = prefixMapping.entrySet();
Set<Map.Entry<String, String>> s = prefixMapping.entrySet();
String uri = null;
for (Map.Entry<String,String> e : s) {
for (Map.Entry<String, String> e : s) {
if (e.getValue() == null) {
continue; // way ?
}
@ -501,8 +505,8 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
/**
* Prints xml characters and uses characters(java.lang.String) method.
*
* @param ch Character buffer.
* @param start The start index of the chars in the ch buffer.
* @param ch Character buffer.
* @param start The start index of the chars in the ch buffer.
* @param length The length index of the chars in the ch buffer.
* @throws SAXException When IOException has happend while printing.
* @see org.xml.sax.ContentHandler#characters(char[], int, int)
@ -510,9 +514,10 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
public void characters(char[] ch, int start, int length) throws SAXException {
characters(new String(ch, start, length));
}
/**
* Escape and prints xml characters.
*
* @param text The text to write.
* @throws SAXException When IOException has happend while printing.
* @see org.x4o.sax3.io.ContentWriter#characters(java.lang.String)
@ -523,11 +528,11 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
}
charactersRaw(SAX3XMLConstants.escapeCharacters(text));
}
public void characters(char c) throws SAXException {
characters(new char[]{c}, 0, 1);
characters(new char[] { c }, 0, 1);
}
// move or remove ?
protected void charactersRaw(String text) throws SAXException {
if (text == null) {
@ -537,12 +542,12 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
checkPrintedReturn(text);
write(text);
}
/**
* Prints xml ignorable whitespace.
*
* @param ch Character buffer.
* @param start The start index of the chars in the ch buffer.
* @param ch Character buffer.
* @param start The start index of the chars in the ch buffer.
* @param length The length index of the chars in the ch buffer.
* @throws SAXException When IOException has happend while printing.
* @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
@ -550,11 +555,11 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
ignorableWhitespace(new String(ch, start, length));
}
/**
* Prints xml ignorable whitespace.
*
* @param text The text to print.
* @param text The text to print.
* @throws SAXException When IOException has happend while printing.
* @see org.x4o.sax3.io.ContentWriter#ignorableWhitespace(java.lang.String)
*/
@ -565,23 +570,23 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
autoCloseStartElement();
write(text); // TODO: check chars
}
/**
* Prints xml ignorable whitespace character.
*
* @param c The character to print.
* @param c The character to print.
* @throws SAXException When IOException has happend while printing.
*/
public void ignorableWhitespace(char c) throws SAXException {
ignorableWhitespace(new char[]{c}, 0, 1);
ignorableWhitespace(new char[] { c }, 0, 1);
}
/**
* Prints xml instructions.
*
* @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
* @param target The target.
* @param data The data.
* @param data The data.
*/
public void processingInstruction(String target, String data) throws SAXException {
String targetLow = target.toLowerCase();
@ -605,7 +610,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
writeFlush();
printReturn = true;
}
/**
* Not implemented.
*
@ -614,7 +619,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
*/
public void setDocumentLocator(Locator locator) {
}
/**
* Not implemented.
*
@ -624,12 +629,12 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
public void skippedEntity(String name) throws SAXException {
// is for validating parser support, so not needed in xml writing.
}
/**
* Prints xml comment.
*
* @param ch Character buffer.
* @param start The start index of the chars in the ch buffer.
* @param ch Character buffer.
* @param start The start index of the chars in the ch buffer.
* @param length The length index of the chars in the ch buffer.
* @throws SAXException When IOException has happend while printing.
* @see org.xml.sax.ext.DefaultHandler2#comment(char[], int, int)
@ -637,7 +642,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
public void comment(char[] ch, int start, int length) throws SAXException {
comment(new String(ch, start, length));
}
/**
* Prints xml comment.
*
@ -667,15 +672,15 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
write(getPropertyConfig().getPropertyString(OUTPUT_CHAR_NEWLINE));
writeIndent();
write(SAX3XMLConstants.COMMENT_START);
write(SAX3XMLConstants.escapeCharactersComment(text,getPropertyConfig().getPropertyString(OUTPUT_CHAR_TAB), indent));
write(SAX3XMLConstants.escapeCharactersComment(text, getPropertyConfig().getPropertyString(OUTPUT_CHAR_TAB), indent));
write(SAX3XMLConstants.COMMENT_END);
printReturn = true;
}
/**
* Checks if the value contains any new lines and sets the printReturn field.
*
* @param value The value to check.
* @param value The value to check.
*/
private void checkPrintedReturn(String value) {
if (value.indexOf(SAX3XMLConstants.CHAR_NEWLINE) > 0) {
@ -684,10 +689,11 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
printReturn = false;
}
}
/**
* Auto close the start element if working in printing event.
* @throws SAXException When prints gives exception.
*
* @throws SAXException When prints gives exception.
*/
protected void autoCloseStartElement() throws SAXException {
if (startElement == null) {
@ -696,17 +702,18 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
write(startElement.toString());
startElement = null;
}
/**
* Indent the output writer with tabs by indent count.
* @throws SAXException When prints gives exception.
*
* @throws SAXException When prints gives exception.
*/
private void writeIndent() throws SAXException {
for (int i = 0; i < indent; i++) {
write(getPropertyConfig().getPropertyString(OUTPUT_CHAR_TAB));
}
}
protected void writeFlush() throws SAXException {
try {
out.flush();
@ -714,7 +721,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
throw new SAXException(e);
}
}
private void write(String text) throws SAXException {
try {
out.write(text);
@ -722,7 +729,7 @@ public class AbstractContentWriterHandler implements ContentHandler, Closeable {
throw new SAXException(e);
}
}
private void write(char c) throws SAXException {
try {
out.write(c);

View file

@ -20,7 +20,7 @@
* 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.
*/
package org.x4o.sax3.io;
package org.x4o.sax3.io;
import java.io.Writer;
@ -33,18 +33,19 @@ import org.xml.sax.ext.LexicalHandler;
* @author Willem Cazander
* @version 1.0 May 3, 2013
*/
public abstract class AbstractContentWriterLexical extends AbstractContentWriterHandler implements LexicalHandler {
public abstract class AbstractContentWriterLexical extends AbstractContentWriterHandler implements LexicalHandler {
protected boolean printCDATA = false;
/**
* Creates XmlWriter which prints to the Writer interface.
* @param out The writer to print the xml to.
*
* @param out The writer to print the xml to.
*/
public AbstractContentWriterLexical(Writer out) {
super(out);
}
/**
* @see org.xml.sax.ext.LexicalHandler#startCDATA()
*/
@ -53,7 +54,7 @@ public abstract class AbstractContentWriterLexical extends AbstractContentWriter
charactersRaw(SAX3XMLConstants.CDATA_START);
printCDATA = true;
}
/**
* @see org.xml.sax.ext.LexicalHandler#endCDATA()
*/
@ -61,7 +62,7 @@ public abstract class AbstractContentWriterLexical extends AbstractContentWriter
charactersRaw(SAX3XMLConstants.CDATA_END);
printCDATA = false;
}
/**
* @see org.xml.sax.ext.LexicalHandler#startDTD(java.lang.String, java.lang.String, java.lang.String)
*/
@ -80,26 +81,26 @@ public abstract class AbstractContentWriterLexical extends AbstractContentWriter
}
charactersRaw(SAX3XMLConstants.TAG_CLOSE);
}
/**
* @see org.xml.sax.ext.LexicalHandler#endDTD()
*/
public void endDTD() throws SAXException {
writeFlush();
}
/**
* @see org.xml.sax.ext.LexicalHandler#startEntity(java.lang.String)
*/
public void startEntity(String arg0) throws SAXException {
}
/**
* @see org.xml.sax.ext.LexicalHandler#endEntity(java.lang.String)
*/
public void endEntity(String arg0) throws SAXException {
}
/**
* @see org.x4o.sax3.io.AbstractContentWriterHandler#characters(char[], int, int)
*/
@ -107,7 +108,7 @@ public abstract class AbstractContentWriterLexical extends AbstractContentWriter
public void characters(char[] ch, int start, int length) throws SAXException {
characters(new String(ch, start, length));
}
/**
* @see org.x4o.sax3.io.AbstractContentWriterHandler#characters(java.lang.String)
*/

View file

@ -37,14 +37,14 @@ import java.util.Set;
* @param <K> The key class.
* @param <V> The value class.
*/
public class AttributeMap<K,V> implements Map<K,V> {
public class AttributeMap<K, V> implements Map<K, V> {
/** stores the SAX attributes set */
private Attributes attributes = null;
/** stores the namespace uri for this attributes */
private String uri = null;
/**
* Constuctes an new AttributeMap.
*
@ -53,27 +53,27 @@ public class AttributeMap<K,V> implements Map<K,V> {
public AttributeMap(Attributes attributes) {
setAttributes(attributes);
}
/**
* Constructes an new AttributesMap.
*
* @param attributes The data backed of this map.
* @param uri The namespace of these attributes.
* @param attributes The data backed of this map.
* @param uri The namespace of these attributes.
*/
public AttributeMap(Attributes attributes, String uri) {
setAttributes(attributes);
setNameSpace(uri);
}
/**
* Sets the data backend of this map.
*
* @param attributes The Attributes to be used as data backend.
* @param attributes The Attributes to be used as data backend.
*/
public void setAttributes(Attributes attributes) {
this.attributes = attributes;
}
/**
* Return the data backend of this map.
*
@ -82,16 +82,16 @@ public class AttributeMap<K,V> implements Map<K,V> {
public Attributes getAttributes() {
return attributes;
}
/**
* Sets the namespace uri, when set to null it is disabled(default).
*
* @param uri The namespace uri for when parsing when an certain namespace is required.
* @param uri The namespace uri for when parsing when an certain namespace is required.
*/
public void setNameSpace(String uri) {
this.uri = uri;
}
/**
* Returns the namespace used for these attributes.
*
@ -100,13 +100,13 @@ public class AttributeMap<K,V> implements Map<K,V> {
public String getNameSpace() {
return uri;
}
// --------------- inner util functions.
/**
* Gets the local of full name by index.
*
* @param index The index of the attribute.
* @param index The index of the attribute.
* @return The name of the attribute.
*/
private String getName(int index) {
@ -115,7 +115,7 @@ public class AttributeMap<K,V> implements Map<K,V> {
}
return attributes.getQName(index);
}
/**
* Gets the value of the attributes. uses the uri if not null.
*
@ -131,9 +131,10 @@ public class AttributeMap<K,V> implements Map<K,V> {
}
return attributes.getValue(name);
}
/**
* Gets the attribute value.
*
* @param key The name of the attribute.
* @return The value of the attribute.
*/
@ -143,9 +144,9 @@ public class AttributeMap<K,V> implements Map<K,V> {
}
return getValue(key.toString());
}
// ------------------------------ MAP intreface
/**
* Returns the size of the map.
*
@ -154,7 +155,7 @@ public class AttributeMap<K,V> implements Map<K,V> {
public int size() {
return attributes.getLength();
}
/**
* Checks if there are attributes in this map.
*
@ -166,11 +167,11 @@ public class AttributeMap<K,V> implements Map<K,V> {
}
return false;
}
/**
* Checks if there is an attributes with an certain key.
*
* @param key The name of an attribute.
* @param key The name of an attribute.
* @return True if the attributes excist.
*/
public boolean containsKey(Object key) {
@ -179,11 +180,11 @@ public class AttributeMap<K,V> implements Map<K,V> {
}
return false;
}
/**
* Checks if there is an attributes with an value.
*
* @param value The value to check.
* @param value The value to check.
* @return True if an attributes has this value.
*/
public boolean containsValue(Object value) {
@ -194,56 +195,56 @@ public class AttributeMap<K,V> implements Map<K,V> {
}
return false;
}
/**
* Returns The value of an attribute.
*
* @param key The name of the attribute.
* @param key The name of the attribute.
* @return The value of the attribute.
*/
@SuppressWarnings("unchecked")
public V get(Object key) {
return (V)getValue(key);
return (V) getValue(key);
}
/**
* Function not implements. because we can't add to the attributes.
*
* @param key ignored.
* @param value ignored.
* @param key ignored.
* @param value ignored.
* @return always null
*/
public V put(K key, V value) {
return null; // we can't add.
}
/**
* Function not implements. because we can't remove to the attributes.
*
* @param key ignored.
* @param key ignored.
* @return always null
*/
public V remove(Object key) {
return null ;// we can't remove
return null;// we can't remove
}
/**
* Function not implements. because we can't add to the attributes.
*
* @param t ignored.
* @param t ignored.
*/
@SuppressWarnings("rawtypes")
public void putAll(Map t) {
// we can't add.
}
/**
* Function not implements. because we can't clear the attributes.
*/
public void clear() {
// we can't clear
}
/**
* Returns a new Set whith the names of the attributes.
*
@ -253,11 +254,11 @@ public class AttributeMap<K,V> implements Map<K,V> {
public Set<K> keySet() {
HashSet<K> result = new HashSet<K>();
for (int i = 0; i < size(); i++) {
result.add((K)getName(i));
result.add((K) getName(i));
}
return result;
}
/**
* Returns an Collection of the values in the attributes list.
*
@ -267,14 +268,13 @@ public class AttributeMap<K,V> implements Map<K,V> {
public Collection<V> values() {
ArrayList<V> result = new ArrayList<V>();
for (int i = 0; i < size(); i++) {
result.add((V)attributes.getValue(i));
result.add((V) attributes.getValue(i));
}
return result;
}
/**
* Returns an Set made of Map.Entry. note: This Map.Entry is not refenced by
* the attribute list. so changes to not reflect to this map object.
* Returns an Set made of Map.Entry. note: This Map.Entry is not refenced by the attribute list. so changes to not reflect to this map object.
*
* @see java.util.Map#entrySet()
* @return An entry set.
@ -289,17 +289,17 @@ public class AttributeMap<K,V> implements Map<K,V> {
}
return result;
}
/**
* Compares the SAX attribute list.
*
* @param o The Object to compare with.
* @param o The Object to compare with.
* @return True if the object are equal.
*/
public boolean equals(Object o) {
return attributes.equals(o); // compare to attributes
}
/**
* returns the hashCode of the attribute list.
*
@ -308,33 +308,35 @@ public class AttributeMap<K,V> implements Map<K,V> {
public int hashCode() {
return attributes.hashCode();
}
/**
* An innerclass used by entrySet().
*/
@SuppressWarnings("rawtypes")
class AttributeMapEntry implements Map.Entry {
private Object key = null;
private Object value = null;
/**
* Creates AttributeMapEntry with key object.
* @param key The key.
*
* @param key The key.
*/
protected AttributeMapEntry(Object key) {
this.key = key;
}
/**
* @return Returns the key.
*/
public Object getKey() {
return key;
}
/**
* Sets the value of this Map.Entry.
*
* @param value The value to set.
* @return The old value.
*/
@ -343,14 +345,14 @@ public class AttributeMap<K,V> implements Map<K,V> {
this.value = value;
return result;
}
/**
* @return The value of this Map.Entry.
*/
public Object getValue() {
return value;
}
/**
* @param o Check if o is equal.
* @return True if key and value of Map.Entry are equal.

View file

@ -20,12 +20,11 @@
* 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.
*/
package org.x4o.sax3.io;
package org.x4o.sax3.io;
import java.io.Closeable;
import java.io.IOException;
/**
* Functional closable callback for indenting api flow.
*
@ -34,9 +33,9 @@ import java.io.IOException;
*/
@FunctionalInterface
public interface ContentCloseable extends Closeable {
void closeContent() throws IOException;
@Override
default void close() throws IOException {
closeContent(); // wrap for stack trace users

View file

@ -20,7 +20,7 @@
* 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.
*/
package org.x4o.sax3.io;
package org.x4o.sax3.io;
import java.io.Closeable;
import java.io.IOException;
@ -30,7 +30,6 @@ import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;
/**
* ContentWriter is ContentHandler for writing sax events.
*
@ -38,12 +37,12 @@ import org.xml.sax.ext.LexicalHandler;
* @version 1.0 Apr 30, 2013
*/
public interface ContentWriter extends ContentHandler, LexicalHandler, Closeable {
/**
* Wrap startDocument and endDocument in a body closable code block.
*
* @return An closable resource block reference.
* @throws SAXException When IOException is thrown.
* @throws SAXException When IOException is thrown.
*/
default ContentCloseable startDocumentClosure() throws SAXException {
startDocument();
@ -55,49 +54,55 @@ public interface ContentWriter extends ContentHandler, LexicalHandler, Closeable
}
};
}
/**
* Starts and ends an element in one call.
* @param uri The uri of the element.
* @param localName The localName of the element.
* @param name The name of the element.
* @param atts The attributes of the element.
* @throws SAXException When IOException is thrown.
*
* @param uri The uri of the element.
* @param localName The localName of the element.
* @param name The name of the element.
* @param atts The attributes of the element.
* @throws SAXException When IOException is thrown.
*/
void startElementEnd(String uri, String localName, String name, Attributes atts) throws SAXException;
/**
* Writes a String as xml comment.
* @param text The text to have embedded as comment in xml.
* @throws SAXException On error.
*
* @param text The text to have embedded as comment in xml.
* @throws SAXException On error.
*/
void comment(String text) throws SAXException;
/**
* Writes a whitespace String to the body.
* @param text The String of whitespace to write.
* @throws SAXException On error.
*
* @param text The String of whitespace to write.
* @throws SAXException On error.
*/
void ignorableWhitespace(String text) throws SAXException;
/**
* Writes a whitespace character to the body.
* @param c The character to write.
*
* @param c The character to write.
* @throws SAXException On error;
*/
void ignorableWhitespace(char c) throws SAXException;
/**
* Writes a String in the body.
* @param text The text to write.
* @throws SAXException On error.
*
* @param text The text to write.
* @throws SAXException On error.
*/
void characters(String text) throws SAXException;
/**
* Writes a single character in the body body.
* @param c The character to write.
* @throws SAXException On error.
*
* @param c The character to write.
* @throws SAXException On error.
*/
void characters(char c) throws SAXException;
}

View file

@ -20,13 +20,12 @@
* 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.
*/
package org.x4o.sax3.io;
package org.x4o.sax3.io;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
/**
* ContentWriterAdapter is base ContentHandler for writing SAX3 events.
*
@ -34,80 +33,80 @@ import org.xml.sax.SAXException;
* @version 1.0 Dec 27, 2024
*/
abstract public class ContentWriterAdapter implements ContentWriter {
public void comment(char[] ch, int start, int length) throws SAXException {
comment(new String(ch, start, length));
}
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
ignorableWhitespace(new String(ch, start, length));
}
public void ignorableWhitespace(char c) throws SAXException {
ignorableWhitespace(new char[]{c}, 0, 1);
ignorableWhitespace(new char[] { c }, 0, 1);
}
public void startElementEnd(String uri, String localName, String name, Attributes atts) throws SAXException {
startElement(uri, localName, name, atts);
endElement(uri, localName, name);
}
public void characters(char c) throws SAXException {
characters(new char[]{c}, 0, 1);
characters(new char[] { c }, 0, 1);
}
public void characters(char[] ch, int start, int length) throws SAXException {
characters(new String(ch, start, length));
}
@Override
public void endCDATA() throws SAXException {
}
@Override
public void endDTD() throws SAXException {
}
@Override
public void endEntity(String arg0) throws SAXException {
}
@Override
public void startCDATA() throws SAXException {
}
@Override
public void startDTD(String arg0, String arg1, String arg2) throws SAXException {
}
@Override
public void startEntity(String arg0) throws SAXException {
}
@Override
public void endPrefixMapping(String arg0) throws SAXException {
}
@Override
public void processingInstruction(String arg0, String arg1) throws SAXException {
}
@Override
public void setDocumentLocator(Locator arg0) {
}
@Override
public void skippedEntity(String arg0) throws SAXException {
}
@Override
public void startPrefixMapping(String arg0, String arg1) throws SAXException {
}
@Override
public void comment(String text) throws SAXException {
}
@Override
public void ignorableWhitespace(String text) throws SAXException {
}

View file

@ -20,7 +20,7 @@
* 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.
*/
package org.x4o.sax3.io;
package org.x4o.sax3.io;
import java.io.File;
import java.util.ArrayList;
@ -37,12 +37,12 @@ import java.util.function.Supplier;
* @version 1.0 May 1, 2013
*/
public final class SAX3PropertyConfig implements Cloneable {
public final static String X4O_PROPERTIES_PREFIX = "http://x4o.org/properties/";
private final Map<String,PropertyConfigItem> items;
private final Map<String, PropertyConfigItem> items;
private final boolean readOnly;
private final String keyPrefix;
public SAX3PropertyConfig(String keyPrefix, PropertyConfigItem... items) {
this(false, null, keyPrefix, items);
}
@ -50,14 +50,14 @@ public final class SAX3PropertyConfig implements Cloneable {
public SAX3PropertyConfig(SAX3PropertyConfig parentPropertyConfig, String keyPrefix, PropertyConfigItem... items) {
this(false, parentPropertyConfig, keyPrefix, items);
}
public SAX3PropertyConfig(boolean readOnly, SAX3PropertyConfig parentPropertyConfig, String keyPrefix, PropertyConfigItem... itemConfig) {
if (keyPrefix == null) {
throw new NullPointerException("Can't create PropertyConfig with null keyPrefix.");
}
this.readOnly = readOnly;
this.keyPrefix = appendSlashIfMissing(keyPrefix);
Map<String,PropertyConfigItem> fillItems = new HashMap<String,PropertyConfigItem>(itemConfig.length);
Map<String, PropertyConfigItem> fillItems = new HashMap<String, PropertyConfigItem>(itemConfig.length);
fillPropertyConfigItems(fillItems, itemConfig);
copyParentPropertyConfig(fillItems, parentPropertyConfig);
if (fillItems.isEmpty()) {
@ -71,22 +71,22 @@ public final class SAX3PropertyConfig implements Cloneable {
}
items = Collections.unmodifiableMap(fillItems);
}
private final String appendSlashIfMissing(String keyPrefix) {
if (keyPrefix.endsWith("/") == false) {
keyPrefix += "/";
}
return keyPrefix;
}
private final void fillPropertyConfigItems(Map<String,PropertyConfigItem> fillItems, PropertyConfigItem... itemConfig) {
private final void fillPropertyConfigItems(Map<String, PropertyConfigItem> fillItems, PropertyConfigItem... itemConfig) {
for (int i = 0; i < itemConfig.length; i++) {
PropertyConfigItem item = itemConfig[i];
fillItems.put(item.getValueKey(),item);
fillItems.put(item.getValueKey(), item);
}
}
private final void copyParentPropertyConfig(Map<String,PropertyConfigItem> fillItems, SAX3PropertyConfig parentPropertyConfig) {
private final void copyParentPropertyConfig(Map<String, PropertyConfigItem> fillItems, SAX3PropertyConfig parentPropertyConfig) {
if (parentPropertyConfig == null) {
return;
}
@ -94,26 +94,26 @@ public final class SAX3PropertyConfig implements Cloneable {
fillItems.put(key, parentPropertyConfig.getPropertyConfigItem(key));
}
}
public static final class PropertyConfigItem implements Cloneable {
private final boolean valueRequired;
private final String valueKey;
private final Class<?> valueType;
private final Object valueDefault;
private Object value = null;
public PropertyConfigItem(boolean valueRequired, String valueKey, Class<?> valueType) {
this(valueRequired, valueKey, valueType, null);
}
public PropertyConfigItem(String valueKey, Class<?> valueType) {
this(false, valueKey, valueType, null);
}
public PropertyConfigItem(String valueKey, Class<?> valueType, Object valueDefault) {
this(false, valueKey, valueType, valueDefault); // with default then value can not be required.
}
private PropertyConfigItem(boolean valueRequired, String valueKey, Class<?> valueType, Object valueDefault) {
if (valueKey == null) {
throw new NullPointerException("Can't create PropertyConfigItem with null valueKey.");
@ -126,51 +126,52 @@ public final class SAX3PropertyConfig implements Cloneable {
this.valueType = valueType;
this.valueDefault = valueDefault;
}
/**
* @return the value key.
*/
public String getValueKey() {
return valueKey;
}
/**
* @return the valueType.
*/
public Class<?> getValueType() {
return valueType;
}
/**
* @return the value default.
*/
public Object getValueDefault() {
return valueDefault;
}
/**
* @return the valueRequired.
*/
public boolean isValueRequired() {
return valueRequired;
}
/**
* @return the value.
*/
public Object getValue() {
return value;
}
/**
* @param value the value to set.
*/
public void setValue(Object value) {
this.value = value;
}
/**
* Clones all the fields into the new PropertyConfigItem.
*
* @see java.lang.Object#clone()
*/
@Override
@ -180,7 +181,7 @@ public final class SAX3PropertyConfig implements Cloneable {
return clone;
}
}
private final PropertyConfigItem getPropertyConfigItem(String key) {
if (key == null) {
throw new NullPointerException("Can't search with null key.");
@ -191,23 +192,23 @@ public final class SAX3PropertyConfig implements Cloneable {
}
return item;
}
public final String getKeyPrefix() {
return keyPrefix;
}
public final boolean isPropertyRequired(String key) {
return getPropertyKeysRequired().contains(key);
}
public final List<String> getPropertyKeysRequired() {
return findPropertyKeysRequired(false);
}
public final List<String> getPropertyKeysRequiredValues() {
return findPropertyKeysRequired(true);
}
private final List<String> findPropertyKeysRequired(boolean checkValue) {
List<String> result = new ArrayList<String>(10);
for (String key : getPropertyKeys()) {
@ -223,26 +224,26 @@ public final class SAX3PropertyConfig implements Cloneable {
}
return result;
}
public final List<String> getPropertyKeys() {
List<String> result = new ArrayList<String>(items.keySet());
Collections.sort(result);
return Collections.unmodifiableList(result);
}
public final void setProperty(String key,Object value) {
public final void setProperty(String key, Object value) {
if (readOnly) {
throw new IllegalStateException("This property is readonly for key:" + key);
}
PropertyConfigItem item = getPropertyConfigItem(key);
item.setValue(value);
}
public final Object getPropertyDefault(String key) {
PropertyConfigItem item = getPropertyConfigItem(key);
return item.getValueDefault();
}
public final Object getProperty(String key) {
PropertyConfigItem item = getPropertyConfigItem(key);
Object value = item.getValue();
@ -251,12 +252,12 @@ public final class SAX3PropertyConfig implements Cloneable {
}
return value;
}
public final Class<?> getPropertyType(String key) {
PropertyConfigItem item = getPropertyConfigItem(key);
return item.getValueType();
}
public final Class<?> getPropertyType(String key, Supplier<Class<?>> defaultValue) {
Class<?> result = getPropertyType(key);
if (result != null) {
@ -264,18 +265,18 @@ public final class SAX3PropertyConfig implements Cloneable {
}
return defaultValue.get();
}
public final File getPropertyFile(String key) {
Object value = getProperty(key);
if (value instanceof File) {
return (File)value;
return (File) value;
}
if (value == null) {
return null;
}
throw new IllegalStateException("Wrong value type: " + value.getClass() + " for key: " + key);
}
public final File getPropertyFile(String key, Supplier<File> defaultValue) {
File result = getPropertyFile(key);
if (result != null) {
@ -283,18 +284,18 @@ public final class SAX3PropertyConfig implements Cloneable {
}
return defaultValue.get();
}
public final Boolean getPropertyBoolean(String key) {
Object value = getProperty(key);
if (value instanceof Boolean) {
return (Boolean)value;
return (Boolean) value;
}
if (value == null) {
return null;
}
throw new IllegalStateException("Wrong value type: " + value.getClass() + " for key: " + key);
}
public final Boolean getPropertyBoolean(String key, Supplier<Boolean> defaultValue) {
Boolean result = getPropertyBoolean(key);
if (result != null) {
@ -302,18 +303,18 @@ public final class SAX3PropertyConfig implements Cloneable {
}
return defaultValue.get();
}
public final Integer getPropertyInteger(String key) {
Object value = getProperty(key);
if (value instanceof Integer) {
return (Integer)value;
return (Integer) value;
}
if (value == null) {
return null;
}
throw new IllegalStateException("Wrong value type: " + value.getClass() + " for key: " + key);
}
public final Integer getPropertyInteger(String key, Supplier<Integer> defaultValue) {
Integer result = getPropertyInteger(key);
if (result != null) {
@ -321,19 +322,19 @@ public final class SAX3PropertyConfig implements Cloneable {
}
return defaultValue.get();
}
@SuppressWarnings("unchecked")
public final List<String> getPropertyList(String key) {
Object value = getProperty(key);
if (value instanceof List) {
return (List<String>)value;
return (List<String>) value;
}
if (value == null) {
return null;
}
throw new IllegalStateException("Wrong value type: " + value.getClass() + " for key: " + key);
}
public final List<String> getPropertyList(String key, Supplier<List<String>> defaultValue) {
List<String> result = getPropertyList(key);
if (result != null) {
@ -341,39 +342,39 @@ public final class SAX3PropertyConfig implements Cloneable {
}
return defaultValue.get();
}
@SuppressWarnings("unchecked")
public final Map<String,String> getPropertyMap(String key) {
public final Map<String, String> getPropertyMap(String key) {
Object value = getProperty(key);
if (value instanceof Map) {
return (Map<String,String>)value;
return (Map<String, String>) value;
}
if (value == null) {
return null;
}
throw new IllegalStateException("Wrong value type: " + value.getClass() + " for key: " + key);
}
public final Map<String,String> getPropertyMap(String key, Supplier<Map<String,String>> defaultValue) {
Map<String,String> result = getPropertyMap(key);
public final Map<String, String> getPropertyMap(String key, Supplier<Map<String, String>> defaultValue) {
Map<String, String> result = getPropertyMap(key);
if (result != null) {
return result;
}
return defaultValue.get();
}
public final String getPropertyString(String key) {
Object value = getProperty(key);
if (value instanceof String) {
return (String)value;
return (String) value;
}
if (value == null) {
return null;
}
throw new IllegalStateException("Wrong value type: " + value.getClass() + " for key: " + key);
}
public final String getPropertyString(String key,String defaultValue) {
public final String getPropertyString(String key, String defaultValue) {
String propertyValue = getPropertyString(key);
if (propertyValue == null) {
return defaultValue;
@ -381,15 +382,15 @@ public final class SAX3PropertyConfig implements Cloneable {
return propertyValue;
}
}
public final String getPropertyString(String key,Supplier<String> defaultValue) {
public final String getPropertyString(String key, Supplier<String> defaultValue) {
String result = getPropertyString(key);
if (result != null) {
return result;
}
return defaultValue.get();
}
public final void copyParentProperties(SAX3PropertyConfig config) {
for (String key : getPropertyKeys()) {
Object value = config.getProperty(key);
@ -399,9 +400,9 @@ public final class SAX3PropertyConfig implements Cloneable {
setProperty(key, value);
}
}
@SuppressWarnings("unchecked")
public final void setPropertyParsedValue(String key,String value) {
public final void setPropertyParsedValue(String key, String value) {
Class<?> valueType = getPropertyType(key);
if (String.class.equals(valueType)) {
setProperty(key, value);
@ -429,7 +430,7 @@ public final class SAX3PropertyConfig implements Cloneable {
}
if (List.class.equals(valueType)) {
String[] listValues = value.split(",");
List<String> result = (List<String>)getProperty(key);
List<String> result = (List<String>) getProperty(key);
if (result == null) {
result = new ArrayList<String>(10);
setProperty(key, result);
@ -441,9 +442,9 @@ public final class SAX3PropertyConfig implements Cloneable {
}
if (Map.class.equals(valueType)) {
String[] listValues = value.split(",");
Map<String,String> result = (Map<String,String>)getProperty(key);
Map<String, String> result = (Map<String, String>) getProperty(key);
if (result == null) {
result = new HashMap<String,String>(10);
result = new HashMap<String, String>(10);
setProperty(key, result);
}
if (listValues.length != 2) {
@ -455,9 +456,10 @@ public final class SAX3PropertyConfig implements Cloneable {
return;
}
}
/**
* Clones all the properties into the new PropertyConfig.
*
* @see java.lang.Object#clone()
*/
@Override

View file

@ -31,139 +31,140 @@ import java.util.PrimitiveIterator;
* @version 1.0 Mrt 31, 2012
*/
public final class SAX3XMLConstants {
/**
* Lowcase xml.
*/
public static final String XML = javax.xml.XMLConstants.XML_NS_PREFIX; // "xml"
/**
* XML Default encoding is utf-8.
*/
public static final String XML_DEFAULT_ENCODING = "UTF-8";
/**
* XML Default version is 1.0.
*/
public static final String XML_DEFAULT_VERSION = "1.0";
/**
* XML Namespace prefix attribute.
*/
public static final String XMLNS_ATTRIBUTE = javax.xml.XMLConstants.XMLNS_ATTRIBUTE; // "xmlns"
/**
* XML Namespace prefix seperator
*/
public static final String XMLNS_ASSIGN = ":";
/**
* XML Schema namespace URI.
*/
public static final String XML_SCHEMA_NS_URI = javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; // "http://www.w3.org/2001/XMLSchema"
public static final String XML_SCHEMA_NS_URI = javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; // "http://www.w3.org/2001/XMLSchema"
/**
* XML Schema instance namespace URI.
*/
public static final String XML_SCHEMA_INSTANCE_NS_URI = javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI; // "http://www.w3.org/2001/XMLSchema-instance"
/**
* Null or empty namespace uri.
*
* @see <a href="http://www.w3.org/TR/REC-xml-names/#defaulting">Namespaces in XML, 5.2 Namespace Defaulting</a>
*/
public static final String NULL_NS_URI = javax.xml.XMLConstants.NULL_NS_URI; // ""
/**
* Definition of DTD doctype opening tag.
*/
public static final String XML_DOCTYPE_TAG_OPEN = "<!DOCTYPE";
/**
* Opens xml element tag.
*/
public static final String TAG_OPEN = "<";
/**
* Opens end xml element tag.
*/
public static final String TAG_OPEN_END = "</";
/**
* Closes xml element tag.
*/
public static final String TAG_CLOSE = ">";
/**
* Close empty xml element tag.
*/
public static final String TAG_CLOSE_EMPTY = "/>";
/**
* Starts a comment.
*/
public static final String COMMENT_START = "<!--";
/**
* Ends a comment.
*/
public static final String COMMENT_END = "-->";
/**
* Starts a processing instruction.
*/
public static final String PROCESS_START = "<?";
/**
* Ends a processing instruction.
*/
public static final String PROCESS_END = "?>";
/**
* Starts a cdata section.
*/
public static final String CDATA_START = "<![CDATA[";
/**
* Ends a cdata section.
*/
public static final String CDATA_END = "]]>";
/**
* The regex expression of a cdata start section.
*/
public static final String CDATA_START_REGEX = "<!\\x"+Integer.toHexString('[')+"CDATA\\x"+Integer.toHexString('[');
public static final String CDATA_START_REGEX = "<!\\x" + Integer.toHexString('[') + "CDATA\\x" + Integer.toHexString('[');
/**
* The regex expression of a cdata end section.
*/
public static final String CDATA_END_REGEX = "\\x"+Integer.toHexString(']')+"\\x"+Integer.toHexString(']')+">";
public static final String CDATA_END_REGEX = "\\x" + Integer.toHexString(']') + "\\x" + Integer.toHexString(']') + ">";
/**
* Tab char
*/
public static final char CHAR_TAB = '\t';
/**
* Newline char
*/
public static final char CHAR_NEWLINE = '\n';
/// Unicode entity reference escape sequence starter
public static final int CODE_POINT_ENTIY_REF_ESCAPE = '&';
/// Unicode entity reference by number
public static final int CODE_POINT_ENTIY_REF_NUMBER = '#';
/// Unicode entity reference by hex number
public static final int CODE_POINT_ENTIY_REF_NUMBER_HEX = 'x';
/// Unicode entity reference escape sequence terminator
public static final int CODE_POINT_ENTIY_REF_TERMINATOR = ';';
static public String getDocumentDeclaration(String encoding) {
return getDocumentDeclaration(encoding,null);
return getDocumentDeclaration(encoding, null);
}
static public String getDocumentDeclaration(String encoding, String version) {
if (encoding == null) {
encoding = XML_DEFAULT_ENCODING;
@ -173,65 +174,136 @@ public final class SAX3XMLConstants {
}
return String.format("<?xml version=\"%s\" encoding=\"%s\"?>", version, encoding);
}
static public boolean isChar(int c) {
// Exclude "compatibility characters", as defined in section 2.3 of [Unicode]
if (c>=0x7F & c<=0x84) { return false; }
if (c>=0x86 & c<=0x9F) { return false; }
if (c>=0xFDD0 & c<=0xFDEF) { return false; }
if ((c>=0x1FFFE & c<=0x1FFFF)||(c>=0x2FFFE & c<=0x2FFFF)|(c>=0x3FFFE & c<=0x3FFFF)) { return false; }
if ((c>=0x4FFFE & c<=0x4FFFF)||(c>=0x5FFFE & c<=0x5FFFF)|(c>=0x6FFFE & c<=0x6FFFF)) { return false; }
if ((c>=0x7FFFE & c<=0x7FFFF)||(c>=0x8FFFE & c<=0x8FFFF)|(c>=0x9FFFE & c<=0x9FFFF)) { return false; }
if ((c>=0xAFFFE & c<=0xAFFFF)||(c>=0xBFFFE & c<=0xBFFFF)|(c>=0xCFFFE & c<=0xCFFFF)) { return false; }
if ((c>=0xDFFFE & c<=0xDFFFF)||(c>=0xEFFFE & c<=0xEFFFF)|(c>=0xFFFFE & c<=0xFFFFF)) { return false; }
if (c>=0x10FFFE & c<=0x10FFFF) { return false; }
if (c >= 0x7F & c <= 0x84) {
return false;
}
if (c >= 0x86 & c <= 0x9F) {
return false;
}
if (c >= 0xFDD0 & c <= 0xFDEF) {
return false;
}
if ((c >= 0x1FFFE & c <= 0x1FFFF) || (c >= 0x2FFFE & c <= 0x2FFFF) | (c >= 0x3FFFE & c <= 0x3FFFF)) {
return false;
}
if ((c >= 0x4FFFE & c <= 0x4FFFF) || (c >= 0x5FFFE & c <= 0x5FFFF) | (c >= 0x6FFFE & c <= 0x6FFFF)) {
return false;
}
if ((c >= 0x7FFFE & c <= 0x7FFFF) || (c >= 0x8FFFE & c <= 0x8FFFF) | (c >= 0x9FFFE & c <= 0x9FFFF)) {
return false;
}
if ((c >= 0xAFFFE & c <= 0xAFFFF) || (c >= 0xBFFFE & c <= 0xBFFFF) | (c >= 0xCFFFE & c <= 0xCFFFF)) {
return false;
}
if ((c >= 0xDFFFE & c <= 0xDFFFF) || (c >= 0xEFFFE & c <= 0xEFFFF) | (c >= 0xFFFFE & c <= 0xFFFFF)) {
return false;
}
if (c >= 0x10FFFE & c <= 0x10FFFF) {
return false;
}
// Source;
// #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
if (c==0x9) { return true; }
if (c==0xA) { return true; }
if (c==0xD) { return true; }
if (c>=0x20 & c<=0xD7FF) { return true; }
if (c>=0xE000 & c<=0xFFFD) { return true; }
if (c>=0x10000 & c<=0x10FFFF) { return true; }
if (c == 0x9) {
return true;
}
if (c == 0xA) {
return true;
}
if (c == 0xD) {
return true;
}
if (c >= 0x20 & c <= 0xD7FF) {
return true;
}
if (c >= 0xE000 & c <= 0xFFFD) {
return true;
}
if (c >= 0x10000 & c <= 0x10FFFF) {
return true;
}
return false;
}
static public boolean isNameStartChar(int c) {
// Source;
// ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
if (c>='a' & c<='z') { return true; }
if (c>='A' & c<='Z') { return true; }
if (c==':' || c=='_') { return true; }
if (c>=0xC0 & c<=0xD6) { return true; }
if (c>=0xD8 & c<=0xF6) { return true; }
if (c>=0xF8 & c<=0x2FF) { return true; }
if (c>=0x370 & c<=0x37D) { return true; }
if (c>=0x37F & c<=0x1FFF) { return true; }
if (c>=0x200C & c<=0x200D) { return true; }
if (c>=0x2070 & c<=0x218F) { return true; }
if (c>=0x2C00 & c<=0x2FEF) { return true; }
if (c>=0x3001 & c<=0xD7FF) { return true; }
if (c>=0xF900 & c<=0xFDCF) { return true; }
if (c>=0xFDF0 & c<=0xFFFD) { return true; }
if (c>=0x10000 & c<=0xEFFFF) { return true; }
// ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] |
// [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
if (c >= 'a' & c <= 'z') {
return true;
}
if (c >= 'A' & c <= 'Z') {
return true;
}
if (c == ':' || c == '_') {
return true;
}
if (c >= 0xC0 & c <= 0xD6) {
return true;
}
if (c >= 0xD8 & c <= 0xF6) {
return true;
}
if (c >= 0xF8 & c <= 0x2FF) {
return true;
}
if (c >= 0x370 & c <= 0x37D) {
return true;
}
if (c >= 0x37F & c <= 0x1FFF) {
return true;
}
if (c >= 0x200C & c <= 0x200D) {
return true;
}
if (c >= 0x2070 & c <= 0x218F) {
return true;
}
if (c >= 0x2C00 & c <= 0x2FEF) {
return true;
}
if (c >= 0x3001 & c <= 0xD7FF) {
return true;
}
if (c >= 0xF900 & c <= 0xFDCF) {
return true;
}
if (c >= 0xFDF0 & c <= 0xFFFD) {
return true;
}
if (c >= 0x10000 & c <= 0xEFFFF) {
return true;
}
return false;
}
static public boolean isNameChar(int c) {
// Source;
// NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
if (isNameStartChar(c)) {
return true;
}
if (c=='-' || c=='.') { return true; }
if (c>='0' & c<='9') { return true; }
if (c==0xB7) { return true; }
if (c>=0x0300 & c<=0x036F) { return true; }
if (c>=0x203F & c<=0x2040) { return true; }
if (c == '-' || c == '.') {
return true;
}
if (c >= '0' & c <= '9') {
return true;
}
if (c == 0xB7) {
return true;
}
if (c >= 0x0300 & c <= 0x036F) {
return true;
}
if (c >= 0x203F & c <= 0x2040) {
return true;
}
return false;
}
static public boolean isCharString(String value) {
PrimitiveIterator.OfInt iterator = value.codePoints().iterator();
while (iterator.hasNext()) {
@ -242,7 +314,7 @@ public final class SAX3XMLConstants {
}
return true;
}
static public boolean isNameString(String value) {
boolean first = true;
PrimitiveIterator.OfInt iterator = value.codePoints().iterator();
@ -260,7 +332,7 @@ public final class SAX3XMLConstants {
}
return true;
}
static private boolean escapeXMLValueAppend(int codePoint, StringBuilder result) {
if (codePoint == '<') {
result.append("&lt;");
@ -284,23 +356,23 @@ public final class SAX3XMLConstants {
}
return false;
}
static public String escapeAttributeValue(String value) {
// Reference ::= EntityRef | CharRef
// AttValue ::= '"' ([^<&"] | Reference)* '"' | "'" ([^<&'] | Reference)* "'"
// Reference ::= EntityRef | CharRef
// AttValue ::= '"' ([^<&"] | Reference)* '"' | "'" ([^<&'] | Reference)* "'"
StringBuilder result = new StringBuilder(value.length());
PrimitiveIterator.OfInt iterator = value.codePoints().iterator();
while (iterator.hasNext()) {
int c = iterator.nextInt();
// 3.3.3 Attribute-Value Normalization.
if (escapeXMLValueAppend(c,result)) {
if (escapeXMLValueAppend(c, result)) {
continue;
}
result.appendCodePoint(c);
}
return result.toString();
}
static public String escapeCharacters(String value) {
StringBuilder result = new StringBuilder(value.length());
PrimitiveIterator.OfInt iterator = value.codePoints().iterator();
@ -323,18 +395,18 @@ public final class SAX3XMLConstants {
result.append(entity);
continue; // contine to next i char.
} else {
//i = iOrg; // continue normal escape.
// i = iOrg; // continue normal escape.
PrimitiveIterator.OfInt entityIterator = entity.codePoints().iterator();
while (entityIterator.hasNext()) {
int entityCodePoint = entityIterator.nextInt();
if (escapeXMLValueAppend(entityCodePoint,result)) {
if (escapeXMLValueAppend(entityCodePoint, result)) {
continue;
}
result.appendCodePoint(entityCodePoint);
}
}
} else {
if (escapeXMLValueAppend(c,result)) {
if (escapeXMLValueAppend(c, result)) {
continue;
}
result.appendCodePoint(c);
@ -342,60 +414,110 @@ public final class SAX3XMLConstants {
}
return result.toString();
}
// TODO: make it loaded and xml and html version aware for correctness.
static private boolean charEntityAllowed(String entity) {
// Hex code points
if (entity.startsWith("&#")) {
String hex = entity.substring(2,entity.length() - 1);
String hex = entity.substring(2, entity.length() - 1);
if (hex.startsWith("x")) {
hex = hex.substring(1);
}
int codePoint = Integer.parseInt(hex, 16);
return codePoint > 0; // TODO: check if code point is valid.
}
// XML
if ("&lt;".equals(entity)) { return true; }
if ("&gt;".equals(entity)) { return true; }
if ("&amp;".equals(entity)) { return true; }
if ("&quot;".equals(entity)) { return true; }
if ("&apos;".equals(entity)) { return true; }
if ("&lt;".equals(entity)) {
return true;
}
if ("&gt;".equals(entity)) {
return true;
}
if ("&amp;".equals(entity)) {
return true;
}
if ("&quot;".equals(entity)) {
return true;
}
if ("&apos;".equals(entity)) {
return true;
}
// HTML
if ("&acute;".equals(entity)) { return true; }
if ("&alefsym;".equals(entity)) { return true; }
if ("&asymp;".equals(entity)) { return true; }
if ("&ang;".equals(entity)) { return true; }
if ("&lowast;".equals(entity)) { return true; }
if ("&clubs;".equals(entity)) { return true; }
if ("&diams;".equals(entity)) { return true; }
if ("&hearts;".equals(entity)) { return true; }
if ("&spades;".equals(entity)) { return true; }
if ("&acute;".equals(entity)) {
return true;
}
if ("&alefsym;".equals(entity)) {
return true;
}
if ("&asymp;".equals(entity)) {
return true;
}
if ("&ang;".equals(entity)) {
return true;
}
if ("&lowast;".equals(entity)) {
return true;
}
if ("&clubs;".equals(entity)) {
return true;
}
if ("&diams;".equals(entity)) {
return true;
}
if ("&hearts;".equals(entity)) {
return true;
}
if ("&spades;".equals(entity)) {
return true;
}
// TODO: etc/etc
if ("&yuml;".equals(entity)) { return true; }
if ("&yacute;".equals(entity)) { return true; }
if ("&ugrave;".equals(entity)) { return true; }
if ("&uuml;".equals(entity)) { return true; }
if ("&ucirc;".equals(entity)) { return true; }
if ("&uacute;".equals(entity)) { return true; }
if ("&divide;".equals(entity)) { return true; }
if ("&nbsp;".equals(entity)) { return true; }
if ("&reg;".equals(entity)) { return true; }
if ("&trade;".equals(entity)) { return true; }
if ("&copy;".equals(entity)) { return true; }
if ("&yuml;".equals(entity)) {
return true;
}
if ("&yacute;".equals(entity)) {
return true;
}
if ("&ugrave;".equals(entity)) {
return true;
}
if ("&uuml;".equals(entity)) {
return true;
}
if ("&ucirc;".equals(entity)) {
return true;
}
if ("&uacute;".equals(entity)) {
return true;
}
if ("&divide;".equals(entity)) {
return true;
}
if ("&nbsp;".equals(entity)) {
return true;
}
if ("&reg;".equals(entity)) {
return true;
}
if ("&trade;".equals(entity)) {
return true;
}
if ("&copy;".equals(entity)) {
return true;
}
return false;
}
static public String escapeCharactersCdata(String value,String replaceCdataStart,String replaceCdataEnd) {
static public String escapeCharactersCdata(String value, String replaceCdataStart, String replaceCdataEnd) {
value = value.replaceAll(CDATA_START_REGEX, replaceCdataStart);
value = value.replaceAll(CDATA_END_REGEX, replaceCdataEnd);
value = value.replaceAll(CDATA_END_REGEX, replaceCdataEnd);
return value;
}
static public String escapeCharactersComment(String value,String charTab,int indent) {
static public String escapeCharactersComment(String value, String charTab, int indent) {
value = value.replaceAll(COMMENT_START, "");
value = value.replaceAll(COMMENT_END, "");
StringBuilder result = new StringBuilder(value.length());

View file

@ -20,7 +20,7 @@
* 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.
*/
package org.x4o.sax3.io.xdbx;
package org.x4o.sax3.io.xdbx;
import java.io.OutputStream;
@ -38,7 +38,8 @@ public abstract class AbstractXDBXWriter extends AbstractXDBXWriterLexical imple
/**
* Creates writer which prints to the stream interface.
* @param out The stream to print the xml to.
*
* @param out The stream to print the xml to.
*/
public AbstractXDBXWriter(OutputStream out) {
super(out);
@ -46,10 +47,11 @@ public abstract class AbstractXDBXWriter extends AbstractXDBXWriterLexical imple
/**
* Starts and end then element.
*
* @see org.x4o.sax3.io.ContentWriter#startElementEnd(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
public void startElementEnd(String uri, String localName, String name, Attributes atts) throws SAXException {
startElement(uri,localName,name, atts);
startElement(uri, localName, name, atts);
endElement(uri, localName, name);
}
}

View file

@ -20,7 +20,7 @@
* 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.
*/
package org.x4o.sax3.io.xdbx;
package org.x4o.sax3.io.xdbx;
import java.io.BufferedReader;
import java.io.Closeable;
@ -56,15 +56,16 @@ import org.xml.sax.SAXException;
* @author Willem Cazander
* @version 1.0 Dec 19, 2024
*/
public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
private final SAX3PropertyConfig propertyConfig;
private final OutputStream out;
private Map<String,String> prefixMapping = null;
private Map<String, String> prefixMapping = null;
private List<String> printedMappings = null;
private Stack<String> elements = null;
private Map<String,Integer> stringIdx = null;
private Map<String, Integer> stringIdx = null;
//@formatter:off
private final static String PROPERTY_CONTEXT_PREFIX = SAX3PropertyConfig.X4O_PROPERTIES_PREFIX+"writer/xdbx/";
public final static SAX3PropertyConfig DEFAULT_PROPERTY_CONFIG;
public final static String OUTPUT_DECLARATION = PROPERTY_CONTEXT_PREFIX+"output/declaration";
@ -94,34 +95,36 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
new PropertyConfigItem(ROOT_START_NAMESPACE_ALL, Boolean.class, true)
);
}
//@formatter:on
/**
* Creates writer which prints to the stream interface.
* @param out The stream to print the xml to.
*
* @param out The stream to print the xml to.
*/
public AbstractXDBXWriterHandler(OutputStream out) {
this.out = Objects.requireNonNull(out, "Can't write on null OutputStream.");
this.prefixMapping = new HashMap<String,String>(15);
this.prefixMapping = new HashMap<String, String>(15);
this.printedMappings = new ArrayList<String>(15);
this.elements = new Stack<String>();
this.propertyConfig = new SAX3PropertyConfig(DEFAULT_PROPERTY_CONFIG, PROPERTY_CONTEXT_PREFIX);
this.stringIdx = new HashMap<>();
}
public SAX3PropertyConfig getPropertyConfig() {
return propertyConfig;
}
@Override
public void close() throws IOException {
out.close();
}
protected boolean xdbxStringId(String data) {
Integer result = stringIdx.get(data);
return result != null;
}
protected int xdbxStringStore(String data) {
Integer result = stringIdx.get(data);
if (result != null) {
@ -131,7 +134,7 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
stringIdx.put(data, result);
return result;
}
/**
* @see org.xml.sax.ContentHandler#startDocument()
*/
@ -146,14 +149,14 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
writeLengthValue("1.0");
writeTag(XDBXContentTag.ENCODING);
writeLengthValue("utf-8");
//Standalone in TV(standalone) form where the value of 'standalone' is either 0 or 1.
//write(XDBXContentTag.STANDALONE);
//write(1);
// Standalone in TV(standalone) form where the value of 'standalone' is either 0 or 1.
// write(XDBXContentTag.STANDALONE);
// write(1);
}
prologWriteLicence();
prologWriteUserComment();
}
private void prologWriteLicence() throws SAXException {
if (!propertyConfig.getPropertyBoolean(PROLOG_LICENCE_ENABLE)) {
return;
@ -168,7 +171,7 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
}
licenceInput = cl.getResourceAsStream(licenceResource);
if (licenceInput == null) {
throw new NullPointerException("Could not load licence resource from: "+licenceResource);
throw new NullPointerException("Could not load licence resource from: " + licenceResource);
}
}
if (licenceInput == null) {
@ -190,7 +193,7 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
}
comment(licenceText);
}
private String readLicenceStream(InputStream inputStream, String encoding) throws IOException {
if (encoding == null) {
encoding = SAX3XMLConstants.XML_DEFAULT_ENCODING;
@ -202,7 +205,7 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
sb.append('\n');
String line = br.readLine();
while (line != null) {
if (line.length()>0) {
if (line.length() > 0) {
sb.append(" ");
}
sb.append(line);
@ -216,7 +219,7 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
br.close();
}
}
private void prologWriteUserComment() throws SAXException {
if (!propertyConfig.getPropertyBoolean(PROLOG_USER_COMMENT_ENABLE)) {
return;
@ -228,12 +231,12 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
if (userComment.isEmpty()) {
return;
}
String chEnter = "\n"; //getPropertyConfig().getPropertyString(OUTPUT_CHAR_NEWLINE);
String chTab = "\t"; //getPropertyConfig().getPropertyString(OUTPUT_CHAR_TAB);
String chEnter = "\n"; // getPropertyConfig().getPropertyString(OUTPUT_CHAR_NEWLINE);
String chTab = "\t"; // getPropertyConfig().getPropertyString(OUTPUT_CHAR_TAB);
userComment = userComment.replaceAll("\n", chEnter + chTab);
comment(chEnter + chTab + userComment + chEnter);
}
/**
* @see org.xml.sax.ContentHandler#endDocument()
*/
@ -244,12 +247,12 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
throw new SAXException("Invalid xml still have " + elements.size() + " elements open.");
}
}
/**
* @param uri The xml namespace uri.
* @param localName The local name of the xml tag.
* @param name The (full) name of the xml tag.
* @param atts The attributes of the xml tag.
* @param uri The xml namespace uri.
* @param localName The local name of the xml tag.
* @param name The (full) name of the xml tag.
* @param atts The attributes of the xml tag.
* @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
*/
public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException {
@ -279,7 +282,7 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
}
}
}
startElementTag(uri,localName,name);
startElementTag(uri, localName, name);
startElementNamespace(uri);
startElementAttributes(atts);
if (SAX3XMLConstants.NULL_NS_URI.equals(uri) || uri == null) {
@ -288,10 +291,10 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
elements.push(uri + ":" + name);
}
}
public void startElementTag(String uri, String localName, String name) throws SAXException {
boolean localNameIdx = xdbxStringId(localName);
if (SAX3XMLConstants.NULL_NS_URI.equals(uri) || uri==null) {
if (SAX3XMLConstants.NULL_NS_URI.equals(uri) || uri == null) {
if (localNameIdx) {
writeTag(XDBXContentTag.ELEMENT_I);
writeVariableInteger(xdbxStringStore(localName));
@ -305,7 +308,7 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
} else {
String prefix = prefixMapping.get(uri);
if (prefix == null) {
throw new SAXException("preFixUri: "+uri+" is not started.");
throw new SAXException("preFixUri: " + uri + " is not started.");
}
if (localNameIdx) {
writeTag(XDBXContentTag.ELEMENT_III);
@ -318,22 +321,22 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
writeVariableInteger(xdbxStringStore(uri));
}
}
public void startElementNamespace(String uri) throws SAXException {
if ((uri != null && SAX3XMLConstants.NULL_NS_URI.equals(uri) == false) && printedMappings.contains(uri) == false) {
String prefix = prefixMapping.get(uri);
if (prefix == null) {
throw new SAXException("preFixUri: "+uri+" is not started.");
throw new SAXException("preFixUri: " + uri + " is not started.");
}
printedMappings.add(uri);
writeTag(XDBXContentTag.NS_DECL_II);
writeVariableInteger(xdbxStringStore(prefix));
writeVariableInteger(xdbxStringStore(uri));
}
startElementNamespaceAll(uri);
}
public void startElementNamespaceAll(String uri) throws SAXException {
if (!propertyConfig.getPropertyBoolean(ROOT_START_NAMESPACE_ALL)) {
return;
@ -343,17 +346,17 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
if (printedMappings.contains(uri2) == false) {
prefix = prefixMapping.get(uri2);
if (prefix == null) {
throw new SAXException("preFixUri: "+uri+" is not started.");
throw new SAXException("preFixUri: " + uri + " is not started.");
}
printedMappings.add(uri2);
writeTag(XDBXContentTag.NS_DECL_II);
writeVariableInteger(xdbxStringStore(prefix));
writeVariableInteger(xdbxStringStore(uri2));
}
}
}
private void startElementAttributes(Attributes atts) throws SAXException {
for (int i = 0; i < atts.getLength(); i++) {
String attributeUri = atts.getURI(i);
@ -365,8 +368,8 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
String attributeValueSafe = SAX3XMLConstants.escapeAttributeValue(attributeValue);
boolean attributeValueSimple = attributeValue.equals(attributeValueSafe);
boolean attributeNameIdx = xdbxStringId(attributeName);
if (SAX3XMLConstants.NULL_NS_URI.equals(attributeUri) || attributeUri ==null) {
if (SAX3XMLConstants.NULL_NS_URI.equals(attributeUri) || attributeUri == null) {
if (attributeNameIdx) {
writeTag(XDBXContentTag.ATTRIBUTE_I);
writeVariableInteger(xdbxStringStore(attributeName));
@ -399,15 +402,15 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
writeLengthValue(attributeValueSafe);
}
}
/**
* @param uri The xml namespace uri.
* @param localName The local name of the xml tag.
* @param name The (full) name of the xml tag.
* @param uri The xml namespace uri.
* @param localName The local name of the xml tag.
* @param name The (full) name of the xml tag.
* @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
*/
public void endElement(String uri, String localName, String name) throws SAXException {
if (SAX3XMLConstants.NULL_NS_URI.equals(uri) || uri==null) {
if (SAX3XMLConstants.NULL_NS_URI.equals(uri) || uri == null) {
if (elements.size() > 0 && elements.peek().equals(localName) == false) {
throw new SAXException("Unexpected end tag: " + localName + " should be: " + elements.peek());
}
@ -420,16 +423,17 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
elements.pop();
writeTag(XDBXContentTag.END_ELEMENT);
}
/**
* Starts the prefix mapping of an xml namespace uri.
* @param prefix The xml prefix of this xml namespace uri.
* @param uri The xml namespace uri to add the prefix for.
*
* @param prefix The xml prefix of this xml namespace uri.
* @param uri The xml namespace uri to add the prefix for.
* @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
*/
public void startPrefixMapping(String prefix, String uri) throws SAXException {
prefixMapping.put(uri, prefix);
boolean prefixIdx = xdbxStringId(prefix);
if (!prefixIdx) {
int prefixIdxNum = xdbxStringStore(prefix);
@ -443,15 +447,15 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
writeVariableInteger(uriIdxNum);
}
}
/**
* @param prefix The xml prefix of this xml namespace uri to be ended.
* @param prefix The xml prefix of this xml namespace uri to be ended.
* @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
*/
public void endPrefixMapping(String prefix) throws SAXException {
Set<Map.Entry<String,String>> s = prefixMapping.entrySet();
Set<Map.Entry<String, String>> s = prefixMapping.entrySet();
String uri = null;
for (Map.Entry<String,String> e : s) {
for (Map.Entry<String, String> e : s) {
if (e.getValue() == null) {
continue; // way ?
}
@ -463,12 +467,12 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
prefixMapping.remove(uri);
}
}
/**
* Prints xml characters and uses characters(java.lang.String) method.
*
* @param ch Character buffer.
* @param start The start index of the chars in the ch buffer.
* @param ch Character buffer.
* @param start The start index of the chars in the ch buffer.
* @param length The length index of the chars in the ch buffer.
* @throws SAXException When IOException has happend while printing.
* @see org.xml.sax.ContentHandler#characters(char[], int, int)
@ -476,9 +480,10 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
public void characters(char[] ch, int start, int length) throws SAXException {
characters(new String(ch, start, length));
}
/**
* Escape and prints xml characters.
*
* @param text The text to write.
* @throws SAXException When IOException has happend while printing.
* @see org.x4o.sax3.io.ContentWriter#characters(java.lang.String)
@ -489,11 +494,11 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
}
charactersRaw(SAX3XMLConstants.escapeCharacters(text));
}
public void characters(char c) throws SAXException {
characters(new char[]{c}, 0, 1);
characters(new char[] { c }, 0, 1);
}
// move or remove ?
protected void charactersRaw(String text) throws SAXException {
if (text == null) {
@ -501,12 +506,12 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
}
writeTagLengthValue(XDBXContentTag.TEXT_T, text);
}
/**
* Prints xml ignorable whitespace.
*
* @param ch Character buffer.
* @param start The start index of the chars in the ch buffer.
* @param ch Character buffer.
* @param start The start index of the chars in the ch buffer.
* @param length The length index of the chars in the ch buffer.
* @throws SAXException When IOException has happend while printing.
* @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
@ -514,11 +519,11 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
ignorableWhitespace(new String(ch, start, length));
}
/**
* Prints xml ignorable whitespace.
*
* @param text The text to print.
* @param text The text to print.
* @throws SAXException When IOException has happend while printing.
* @see org.x4o.sax3.io.ContentWriter#ignorableWhitespace(java.lang.String)
*/
@ -528,23 +533,23 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
}
writeTagLengthValue(XDBXContentTag.TEXT_WHITE_SPACE, text);
}
/**
* Prints xml ignorable whitespace character.
*
* @param c The character to print.
* @param c The character to print.
* @throws SAXException When IOException has happend while printing.
*/
public void ignorableWhitespace(char c) throws SAXException {
ignorableWhitespace(new char[]{c}, 0, 1);
ignorableWhitespace(new char[] { c }, 0, 1);
}
/**
* Prints xml instructions.
*
* @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
* @param target The target.
* @param data The data.
* @param data The data.
*/
public void processingInstruction(String target, String data) throws SAXException {
String targetLow = target.toLowerCase();
@ -557,20 +562,20 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
if (SAX3XMLConstants.isCharString(data) == false) {
throw new SAXException("Processing instruction data is invalid char; '" + data + "'");
}
//'I' LengthValue StringID
// 'I' LengthValue StringID
int targetIdx = xdbxStringStore(target);
writeTagLengthValue(XDBXContentTag.STRING_ID, target);
writeVariableInteger(targetIdx);
//'P' StringID LengthValue
// 'P' StringID LengthValue
writeTag(XDBXContentTag.PROCESSING_INSTRUCTION);
writeVariableInteger(targetIdx);
writeLengthValue(data);
writeFlush();
}
/**
* Not implemented.
*
@ -579,7 +584,7 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
*/
public void setDocumentLocator(Locator locator) {
}
/**
* Not implemented.
*
@ -589,12 +594,12 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
public void skippedEntity(String name) throws SAXException {
// is for validating parser support, so not needed in xml writing.
}
/**
* Prints xml comment.
*
* @param ch Character buffer.
* @param start The start index of the chars in the ch buffer.
* @param ch Character buffer.
* @param start The start index of the chars in the ch buffer.
* @param length The length index of the chars in the ch buffer.
* @throws SAXException When IOException has happend while printing.
* @see org.xml.sax.ext.DefaultHandler2#comment(char[], int, int)
@ -602,7 +607,7 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
public void comment(char[] ch, int start, int length) throws SAXException {
comment(new String(ch, start, length));
}
/**
* Prints xml comment.
*
@ -621,7 +626,7 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
char textStart = text.charAt(0);
char textEnd = text.charAt(text.length() - 1);
if (textStart != ' ' && textStart != '\t' && textStart != '\n') {
text = " "+text;
text = " " + text;
}
if (textEnd != ' ' && textEnd != '\t' && textEnd != '\n') {
text = text + " ";
@ -629,7 +634,7 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
}
writeTagLengthValue(XDBXContentTag.COMMENT, SAX3XMLConstants.escapeCharactersComment(text, "", 0));
}
protected void writeFlush() throws SAXException {
try {
out.flush();
@ -637,7 +642,7 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
throw new SAXException(e);
}
}
private void write(byte[] data) throws SAXException {
try {
out.write(data);
@ -645,7 +650,7 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
throw new SAXException(e);
}
}
private void write(int c) throws SAXException {
try {
out.write(c);
@ -653,23 +658,23 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
throw new SAXException(e);
}
}
public void writeHeaderFlags(OutputStream out, int flags) throws SAXException {
write(flags >> 24);
write(flags >> 16);
write(flags >> 8);
write(flags >> 0);
}
protected void writeTag(XDBXContentTag tag) throws SAXException {
write(tag.getTagNumber());
}
protected void writeTagLengthValue(XDBXContentTag tag, String data) throws SAXException {
writeTag(tag);
writeLengthValue(data);
}
protected void writeVariableInteger(int i) throws SAXException {
if (i < 128) {
write(i);
@ -684,12 +689,12 @@ public class AbstractXDBXWriterHandler implements ContentHandler, Closeable {
write((i >> 7) & 0b1111111 + 128);
write(i & 0b1111111);
}
protected void writeLengthValue(byte[] data) throws SAXException {
writeVariableInteger(data.length);
write(data);
}
protected void writeLengthValue(String data) throws SAXException {
writeLengthValue(data.getBytes(StandardCharsets.UTF_8));
}

View file

@ -20,7 +20,7 @@
* 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.
*/
package org.x4o.sax3.io.xdbx;
package org.x4o.sax3.io.xdbx;
import java.io.OutputStream;
@ -35,56 +35,57 @@ import org.xml.sax.ext.LexicalHandler;
* @version 1.0 Dec 20, 2024
*/
public abstract class AbstractXDBXWriterLexical extends AbstractXDBXWriterHandler implements LexicalHandler {
protected boolean printCDATA = false;
/**
* Creates writer which prints to the stream interface.
* @param out The stream to print the xml to.
*
* @param out The stream to print the xml to.
*/
public AbstractXDBXWriterLexical(OutputStream out) {
super(out);
}
/**
* @see org.xml.sax.ext.LexicalHandler#startCDATA()
*/
public void startCDATA() throws SAXException {
printCDATA = true;
}
/**
* @see org.xml.sax.ext.LexicalHandler#endCDATA()
*/
public void endCDATA() throws SAXException {
printCDATA = false;
}
/**
* @see org.xml.sax.ext.LexicalHandler#startDTD(java.lang.String, java.lang.String, java.lang.String)
*/
public void startDTD(String name, String publicId, String systemId) throws SAXException {
// not supported
}
/**
* @see org.xml.sax.ext.LexicalHandler#endDTD()
*/
public void endDTD() throws SAXException {
}
/**
* @see org.xml.sax.ext.LexicalHandler#startEntity(java.lang.String)
*/
public void startEntity(String arg0) throws SAXException {
}
/**
* @see org.xml.sax.ext.LexicalHandler#endEntity(java.lang.String)
*/
public void endEntity(String arg0) throws SAXException {
public void endEntity(String arg0) throws SAXException {
}
/**
* @see org.x4o.sax3.io.AbstractContentWriterHandler#characters(char[], int, int)
*/
@ -92,7 +93,7 @@ public abstract class AbstractXDBXWriterLexical extends AbstractXDBXWriterHandle
public void characters(char[] ch, int start, int length) throws SAXException {
characters(new String(ch, start, length));
}
/**
* @see org.x4o.sax3.io.AbstractContentWriterHandler#characters(java.lang.String)
*/

View file

@ -29,11 +29,11 @@ package org.x4o.sax3.io.xdbx;
* @version 1.0 Dec 20, 2024
*/
public final class XDBXConstants {
static public final byte[] HEADER_MARKER = new byte[] {(byte) 0xCA, 0x3B};
static public final byte[] HEADER_MARKER = new byte[] { (byte) 0xCA, 0x3B };
static public final byte HEADER_LENGHT = 0x05;
static public final byte HEADER_VERSION = 0x01;
static public final int FLAG_XML_SEQUENCE = 0x1;
static public final int FLAG_STRING_ID = 0x2;
static public final int FLAG_STRING_ID_IDX = 0x20;

View file

@ -29,7 +29,7 @@ package org.x4o.sax3.io.xdbx;
* @version 1.0 Dec 20, 2024
*/
public enum XDBXContentTag {
DOCUMENT_END('Z'),
COMPLETED_DOC('d'),
ATOMIC_VALUE('V'),
@ -54,15 +54,14 @@ public enum XDBXContentTag {
PROCESSING_INSTRUCTION('P'),
STRING_ID('I'),
HINT('H'),
SEQUENCE_SEPERATOR('@'),
;
SEQUENCE_SEPERATOR('@'),;
private final int tagNumber;
private XDBXContentTag(char tag) {
tagNumber = tag;
}
public int getTagNumber() {
return tagNumber;
}

View file

@ -20,7 +20,7 @@
* 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.
*/
package org.x4o.sax3.io.xdbx;
package org.x4o.sax3.io.xdbx;
import java.io.IOException;
import java.io.InputStream;
@ -39,7 +39,7 @@ import org.xml.sax.helpers.AttributesImpl;
* @author Willem Cazander
* @version 1.0 Dec 20, 2024
*/
public class XDBXReaderXml {
public class XDBXReaderXml {
private final ContentWriter out;
private final Map<Integer, String> stringIdx;
@ -48,19 +48,19 @@ public class XDBXReaderXml {
private String docEncoding;
private Stack<XDBXElement> elementStack;
private boolean failOnUnsupportedTag = true;
public XDBXReaderXml(ContentWriter out) {
this.out = out;
this.stringIdx = new HashMap<>();
this.elementStack = new Stack<>();
}
// TODO: replace with proper PropertyConfig support
public XDBXReaderXml withNoFailOnUnsupportedTag() {
failOnUnsupportedTag = false;
return this;
return this;
}
public void parse(InputStream in) throws IOException {
if (!Arrays.equals(XDBXConstants.HEADER_MARKER, in.readNBytes(2))) {
throw new IOException("Wrong magic marker");
@ -92,7 +92,7 @@ public class XDBXReaderXml {
throw new IOException(e);
}
}
private void parseToken(int token, InputStream in) throws IOException, SAXException {
if (XDBXContentTag.STRING_ID.getTagNumber() == token) {
String value = readLengthValue(in);
@ -233,10 +233,10 @@ public class XDBXReaderXml {
return; // NOP is done by caller
}
if (failOnUnsupportedTag) {
throw new SAXException("Unsupported tag: " + (char)token + " 0x" + Integer.toHexString(token).toUpperCase());
throw new SAXException("Unsupported tag: " + (char) token + " 0x" + Integer.toHexString(token).toUpperCase());
}
}
protected void flushDocDeclaration() throws SAXException {
if (docMetaFlushed) {
return;
@ -244,7 +244,7 @@ public class XDBXReaderXml {
docMetaFlushed = true;
out.declaration(docXmlVersion, docEncoding, "");
}
protected void flushElement() throws SAXException {
flushDocDeclaration();
if (elementStack.isEmpty()) {
@ -257,13 +257,13 @@ public class XDBXReaderXml {
element.started = true;
out.startElement(element.uri, element.localName, element.name, element.atts);
}
protected String readLengthValue(InputStream in) throws IOException {
int len = readVariableInteger(in);
String value = new String(in.readNBytes(len));
return value;
}
protected int readVariableInteger(InputStream in) throws IOException {
int result = in.read();
if (result < 128) {
@ -275,7 +275,7 @@ public class XDBXReaderXml {
}
return result + (in.read() << 14);
}
public int readHeaderFlags(InputStream in) throws IOException {
int result = 0;
result += in.read() << 24;
@ -284,7 +284,7 @@ public class XDBXReaderXml {
result += in.read() << 0;
return result;
}
public String getDocXmlVersion() {
return docXmlVersion;
}
@ -292,17 +292,18 @@ public class XDBXReaderXml {
public String getDocEncoding() {
return docEncoding;
}
class XDBXElement {
String uri;
String localName;
String name;
final AttributesImpl atts;
boolean started = false;
XDBXElement() {
atts = new AttributesImpl();
}
XDBXElement(String localName) {
this();
this.localName = localName;

View file

@ -20,7 +20,7 @@
* 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.
*/
package org.x4o.sax3.io.xdbx;
package org.x4o.sax3.io.xdbx;
import java.io.OutputStream;
@ -30,11 +30,12 @@ import java.io.OutputStream;
* @author Willem Cazander
* @version 1.0 Dev 20, 2024
*/
public class XDBXWriterXml extends AbstractXDBXWriter {
public class XDBXWriterXml extends AbstractXDBXWriter {
/**
* Creates XmlWriter which prints to the OutputStream interface.
* @param out The OutputStream to write to.
*
* @param out The OutputStream to write to.
*/
public XDBXWriterXml(OutputStream out) {
super(out);

View file

@ -47,39 +47,41 @@ import org.xml.sax.helpers.AttributesImpl;
* @version 1.0 S 17, 2012
*/
public class SAX3WriterXmlAttributeTest {
@Test
public void testAttributeNormal() throws Exception {
StringWriter outputWriter = new StringWriter();
try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) {
writer.startDocument();
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "attr", "", "", "foobar");
atts.addAttribute("", "attr", "", "", "foobar");
writer.startElementEnd("", "test", "", atts);
writer.endDocument();
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test attr=\"foobar\"/>"), output);
}
@Test
public void testAttributeEscape() throws Exception {
StringWriter outputWriter = new StringWriter();
try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) {
writer.startDocument();
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "attr", "", "", "<test/> & 'foobar' is \"quoted\"!");
atts.addAttribute("", "attr", "", "", "<test/> & 'foobar' is \"quoted\"!");
writer.startElementEnd("", "test", "", atts);
writer.endDocument();
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test attr=\"&lt;test/&gt; &amp; &apos;foobar&apos; is &quot;quoted&quot;!\"/>"), output);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(
output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test attr=\"&lt;test/&gt; &amp; &apos;foobar&apos; is &quot;quoted&quot;!\"/>"),
output);
}
@Test
public void testAttributeWhiteSpace() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -87,57 +89,57 @@ public class SAX3WriterXmlAttributeTest {
writer.startDocument();
Assertions.assertThrows(SAXException.class, () -> {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "", "", "", "junit");
atts.addAttribute("", "", "", "", "junit");
writer.startElementEnd("", "test", "", atts);
});
Assertions.assertThrows(SAXException.class, () -> {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", " ", "", "", "junit");
atts.addAttribute("", " ", "", "", "junit");
writer.startElementEnd("", "test", "", atts);
});
Assertions.assertThrows(SAXException.class, () -> {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "foo bar", "", "", "junit");
atts.addAttribute("", "foo bar", "", "", "junit");
writer.startElementEnd("", "test", "", atts);
});
Assertions.assertThrows(SAXException.class, () -> {
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "foobar", "", "", "junit");
atts.addAttribute("", "foobar", "", "", "junit");
writer.startElementEnd("", "test junit", "", atts);
});
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "abc", "", "", " junit ");
atts.addAttribute("", "abc", "", "", " junit ");
writer.startElementEnd("", "root", "", atts);
writer.endDocument();
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root abc=\" junit \"/>"), output);
}
@Test
public void testAttributeChinees() throws Exception {
StringWriter outputWriter = new StringWriter();
try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) {
writer.startDocument();
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "仙上主天", "", "", "仙上主天");
atts.addAttribute("", "仙上主天", "", "", "仙上主天");
writer.startElementEnd("", "chinees", "", atts);
writer.endDocument();
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<chinees 仙上主天=\"仙上主天\"/>"), output);
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxFactory.newSAXParser();
XMLReader saxReader = saxParser.getXMLReader();
XMLReader saxReader = saxParser.getXMLReader();
saxReader.parse(new InputSource(new ByteArrayInputStream(output.getBytes(StandardCharsets.UTF_8))));
Assertions.assertNotNull(saxReader);
}
@Test
public void testAttributeBrahmi() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -145,38 +147,40 @@ public class SAX3WriterXmlAttributeTest {
// Legal by spec, but not in JVM SAX Reader
writer.startDocument();
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "𑀳𑁂𑀮𑀺𑀉𑁄𑀤𑁄𑀭𑁂𑀡𑀪𑀸𑀕", "", "", "𑀳𑁂𑀮𑀺𑀉𑁄𑀤𑁄𑀭𑁂𑀡𑀪𑀸𑀕");
atts.addAttribute ("", "ᒡᒢᑊᒻᒻᓫᔿ", "", "", "ᒡᒢᑊᒻᒻᓫᔿ");
atts.addAttribute("", "𑀳𑁂𑀮𑀺𑀉𑁄𑀤𑁄𑀭𑁂𑀡𑀪𑀸𑀕", "", "", "𑀳𑁂𑀮𑀺𑀉𑁄𑀤𑁄𑀭𑁂𑀡𑀪𑀸𑀕");
atts.addAttribute("", "ᒡᒢᑊᒻᒻᓫᔿ", "", "", "ᒡᒢᑊᒻᒻᓫᔿ");
writer.startElementEnd("", "test", "", atts);
writer.endDocument();
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test 𑀳𑁂𑀮𑀺𑀉𑁄𑀤𑁄𑀭𑁂𑀡𑀪𑀸𑀕=\"𑀳𑁂𑀮𑀺𑀉𑁄𑀤𑁄𑀭𑁂𑀡𑀪𑀸𑀕\" ᒡᒢᑊᒻᒻᓫᔿ=\"ᒡᒢᑊᒻᒻᓫᔿ\"/>"), output);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test 𑀳𑁂𑀮𑀺𑀉𑁄𑀤𑁄𑀭𑁂𑀡𑀪𑀸𑀕=\"𑀳𑁂𑀮𑀺𑀉𑁄𑀤𑁄𑀭𑁂𑀡𑀪𑀸𑀕\" ᒡᒢᑊᒻᒻᓫᔿ=\"ᒡᒢᑊᒻᒻᓫᔿ\"/>"),
output);
// TODO: fix SAX impl
Assertions.assertThrows(SAXException.class, () -> {
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxFactory.newSAXParser();
XMLReader saxReader = saxParser.getXMLReader();
XMLReader saxReader = saxParser.getXMLReader();
saxReader.parse(new InputSource(new ByteArrayInputStream(output.getBytes(StandardCharsets.UTF_8))));
});
}
private String createLongAttribute(Map<String,Object> para) throws SAXException, IOException {
private String createLongAttribute(Map<String, Object> para) throws SAXException, IOException {
StringWriter outputWriter = new StringWriter();
try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) {
for (String key:para.keySet()) {
for (String key : para.keySet()) {
Object value = para.get(key);
writer.getPropertyConfig().setProperty(key, value);
}
AttributesImpl atts = new AttributesImpl();
String data = "_FOR_FOO_BAR";
String dataValue = "LOOP";
for (int i=0;i<15;i++) {
atts.addAttribute("", "attr"+i, "", "", dataValue+=data);
for (int i = 0; i < 15; i++) {
atts.addAttribute("", "attr" + i, "", "", dataValue += data);
}
writer.startDocument();
writer.startElement("", "test", "", atts);
@ -187,43 +191,43 @@ public class SAX3WriterXmlAttributeTest {
}
return outputWriter.toString();
}
@Test
public void testAttributeLongNormal() throws Exception {
Map<String,Object> para = new HashMap<String,Object>();
Map<String, Object> para = new HashMap<String, Object>();
String output = createLongAttribute(para);
int newlines = output.split("\n").length;
Assertions.assertNotNull(output);
Assertions.assertTrue(newlines==4, "outputs: "+newlines);
Assertions.assertTrue(newlines == 4, "outputs: " + newlines);
}
@Test
public void testAttributeLongPerLine() throws Exception {
Map<String,Object> para = new HashMap<String,Object>();
Map<String, Object> para = new HashMap<String, Object>();
para.put(SAX3WriterXml.OUTPUT_LINE_PER_ATTRIBUTE, true);
String output = createLongAttribute(para);
int newlines = output.split("\n").length;
Assertions.assertNotNull(output);
Assertions.assertTrue(newlines==20, "outputs: "+newlines);
Assertions.assertTrue(newlines == 20, "outputs: " + newlines);
}
@Test
public void testAttributeLongSplit80() throws Exception {
Map<String,Object> para = new HashMap<String,Object>();
Map<String, Object> para = new HashMap<String, Object>();
para.put(SAX3WriterXml.OUTPUT_LINE_BREAK_WIDTH, 80);
String output = createLongAttribute(para);
int newlines = output.split("\n").length;
Assertions.assertNotNull(output);
Assertions.assertTrue(newlines==16, "outputs: "+newlines);
Assertions.assertTrue(newlines == 16, "outputs: " + newlines);
}
@Test
public void testAttributeLongSplit180() throws Exception {
Map<String,Object> para = new HashMap<String,Object>();
Map<String, Object> para = new HashMap<String, Object>();
para.put(SAX3WriterXml.OUTPUT_LINE_BREAK_WIDTH, 180);
String output = createLongAttribute(para);
int newlines = output.split("\n").length;
Assertions.assertNotNull(output);
Assertions.assertTrue(newlines==11, "outputs: "+newlines);
Assertions.assertTrue(newlines == 11, "outputs: " + newlines);
}
}

View file

@ -35,7 +35,7 @@ import org.x4o.sax3.SAX3WriterXml;
* @version 1.0 Sep 17, 2013
*/
public class SAX3WriterXmlCDataTest {
@Test
public void testCDATANone() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -46,10 +46,10 @@ public class SAX3WriterXmlCDataTest {
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>foobar"), output);
}
@Test
public void testCDATANoneTagEscape() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -60,10 +60,10 @@ public class SAX3WriterXmlCDataTest {
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>foobar&lt;test/&gt;"), output);
}
@Test
public void testCDATANormal() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -76,10 +76,10 @@ public class SAX3WriterXmlCDataTest {
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[foobar]]>"), output);
}
@Test
public void testCDATAEscapeTag() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -92,10 +92,10 @@ public class SAX3WriterXmlCDataTest {
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[foobar<test/>]]>"), output);
}
@Test
public void testCDATAEscapeStart() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -108,10 +108,10 @@ public class SAX3WriterXmlCDataTest {
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[foobar]]>"), output);
}
@Test
public void testCDATAEscapeEnd() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -124,10 +124,10 @@ public class SAX3WriterXmlCDataTest {
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[foobar]]>"), output);
}
@Test
public void testCDATAEscapeEndEskimo() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -140,10 +140,10 @@ public class SAX3WriterXmlCDataTest {
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[ᒡᒢᑊᒻᒻᓫᔿ]]>"), output);
}
@Test
public void testCDATAEscapeInvalid() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -156,10 +156,10 @@ public class SAX3WriterXmlCDataTest {
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[tokens like \'\' are <invalid>]]>"), output);
}
@Test
public void testCDATAEscapeValid() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -172,7 +172,7 @@ public class SAX3WriterXmlCDataTest {
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><![CDATA[tokens like \']]>\' are <valid>]]>"));
}
}

View file

@ -37,7 +37,7 @@ import org.xml.sax.helpers.AttributesImpl;
* @version 1.0 Aug 26, 2012
*/
public class SAX3WriterXmlTest {
@Test
public void testCharactersNormal() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -48,10 +48,10 @@ public class SAX3WriterXmlTest {
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>test is foobar!"), output);
}
@Test
public void testCharactersEscape() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -62,10 +62,10 @@ public class SAX3WriterXmlTest {
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>&lt;test/&gt; &amp; &apos;foobar&apos; is &quot;quoted&quot;!"), output);
}
@Test
public void testCommentNormal() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -76,10 +76,10 @@ public class SAX3WriterXmlTest {
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- foobar -->"), output);
}
@Test
public void testCommentEscape() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -89,17 +89,17 @@ public class SAX3WriterXmlTest {
writer.endDocument();
}
// note two space because auto-space is before escaping and places spaces over comment tags.
// 1) "<!-- foobar -->" - argu
// 1) "<!-- foobar -->" - argu
// 2) " <!-- foobar --> " - auto-space (default enabled)
// 3) " foobar " - escapes
// 4) "<!-- foobar -->" - printed
// 3) " foobar " - escapes
// 4) "<!-- foobar -->" - printed
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- foobar -->"), output);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- foobar -->"), output);
}
@Test
public void testXmlInvalid() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -110,13 +110,13 @@ public class SAX3WriterXmlTest {
writer.startElement("", "test", "", atts);
writer.startElement("", "foobar", "", atts);
writer.endElement("", "test", "");
writer.endDocument();
writer.endDocument();
});
Assertions.assertTrue(e.getMessage().contains("tag"));
Assertions.assertTrue(e.getMessage().contains("foobar"));
}
}
@Test
public void testXmlInvalidEnd() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -133,7 +133,7 @@ public class SAX3WriterXmlTest {
Assertions.assertTrue(e.getMessage().contains("open"));
}
}
@Test
public void testProcessingInstruction() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -143,14 +143,14 @@ public class SAX3WriterXmlTest {
writer.processingInstruction("target", "data");
writer.startElement("", "test", "", atts);
writer.endElement("", "test", "");
writer.endDocument();
writer.endDocument();
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?target data?>\n<test/>"), output);
}
@Test
public void testProcessingInstructionInline() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -161,14 +161,15 @@ public class SAX3WriterXmlTest {
writer.startElement("", "test", "", atts);
writer.processingInstruction("target-doc", "data-doc");
writer.endElement("", "test", "");
writer.endDocument();
writer.endDocument();
}
String output = outputWriter.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length()>0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?target data?>\n<test>\n\t<?target-doc data-doc?>\n</test>\n"), output);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?target data?>\n<test>\n\t<?target-doc data-doc?>\n</test>\n"),
output);
}
@Test
public void testProcessingInstructionTargetXmlPrefix() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -181,7 +182,7 @@ public class SAX3WriterXmlTest {
Assertions.assertTrue(e.getMessage().contains("start with xml"));
}
}
@Test
public void testProcessingInstructionTargetNoneNameChar() throws Exception {
StringWriter outputWriter = new StringWriter();
@ -195,14 +196,14 @@ public class SAX3WriterXmlTest {
Assertions.assertTrue(e.getMessage().contains("4Prefix"));
}
}
@Test
public void testProcessingInstructionDataNoneChar() throws Exception {
StringWriter outputWriter = new StringWriter();
try (SAX3WriterXml writer = new SAX3WriterXml(outputWriter)) {
SAXException e = Assertions.assertThrows(SAXException.class, () -> {
writer.startDocument();
writer.processingInstruction("target", "isInvalidChar="+0xD800);
writer.processingInstruction("target", "isInvalidChar=" + 0xD800);
});
Assertions.assertTrue(e.getMessage().contains("instruction"));
Assertions.assertTrue(e.getMessage().contains("invalid char"));

View file

@ -45,61 +45,61 @@ import org.xml.sax.helpers.AttributesImpl;
* @version 1.0 Dec 21, 2024
*/
public class XDBXReaderXmlTest {
@Test
public void testReadWrite() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (XDBXWriterXml writer = new XDBXWriterXml(baos)) {
writer.startDocument();
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "attr", "", "", "foobar");
atts.addAttribute("", "attr", "", "", "foobar");
writer.startElementEnd("", "test", "", atts);
writer.endDocument();
}
StringWriter outputXmlStr = new StringWriter();
SAX3WriterXml outputXml = new SAX3WriterXml(outputXmlStr);
XDBXReaderXml reader = new XDBXReaderXml(outputXml);
byte[] writeData = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(writeData);
reader.parse(bais);
String output = outputXmlStr.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test attr=\"foobar\"/>"), output);
}
@Test
public void testReadWritePom() throws Exception {
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
saxFactory.setNamespaceAware(true);
SAXParser saxParser = saxFactory.newSAXParser();
XMLReader saxReader = saxParser.getXMLReader();
XMLReader saxReader = saxParser.getXMLReader();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XDBXWriterXml writer = new XDBXWriterXml(baos);
saxReader.setContentHandler(writer);
saxReader.parse(new InputSource(new FileInputStream(new File("../pom.xml"))));
StringWriter outputXmlStr = new StringWriter();
SAX3WriterXml outputXml = new SAX3WriterXml(outputXmlStr);
XDBXReaderXml reader = new XDBXReaderXml(outputXml);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
reader.parse(bais);
String output = outputXmlStr.toString();
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length() > 0);
Assertions.assertTrue(output.contains("<artifactId>nx01-x4o-driver</artifactId>"), output);
long sizePom = new File("../pom.xml").length();
long sizeXDBX = baos.toByteArray().length;
System.out.println("size-pom.xml:: " + sizePom);
System.out.println("size-pom.xdbx: " + sizeXDBX);
Assertions.assertTrue(sizePom > sizeXDBX, "XDBX is not smaller");
}
}

View file

@ -38,104 +38,103 @@ import org.xml.sax.helpers.AttributesImpl;
* @version 1.0 Dec 21, 2024
*/
public class XDBXWriterXmlTest {
@Test
public void testExample1DefaultEncoding() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (XDBXWriterXml writer = new XDBXWriterXml(baos)) {
writer.getPropertyConfig().setProperty(XDBXWriterXml.OUTPUT_DECLARATION, false);
writer.startDocument();
writer.startElement("", "root", "", new AttributesImpl());
AttributesImpl atts = new AttributesImpl();
atts.addAttribute ("", "mgr", "", "", "NO");
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "mgr", "", "", "NO");
writer.startElement("", "name", "", atts);
writer.characters("Joe");
writer.endElement("", "name", "");
writer.startElement("", "name", "", new AttributesImpl());
writer.characters("Susan");
writer.endElement("", "name", "");
writer.startElement("", "name", "", new AttributesImpl());
writer.characters("Bill");
writer.endElement("", "name", "");
writer.endElement("", "root", "");
writer.endDocument();
}
byte[] output = baos.toByteArray();
int outIdx = 8;
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length > 0);
Assertions.assertEquals((byte)'X', output[outIdx++]);
Assertions.assertEquals((byte) 'X', output[outIdx++]);
Assertions.assertEquals((byte) 4, output[outIdx++]);
Assertions.assertEquals((byte)'r', output[outIdx++]);
Assertions.assertEquals((byte)'o', output[outIdx++]);
Assertions.assertEquals((byte)'o', output[outIdx++]);
Assertions.assertEquals((byte)'t', output[outIdx++]);
Assertions.assertEquals((byte) 'r', output[outIdx++]);
Assertions.assertEquals((byte) 'o', output[outIdx++]);
Assertions.assertEquals((byte) 'o', output[outIdx++]);
Assertions.assertEquals((byte) 't', output[outIdx++]);
Assertions.assertEquals((byte) 1, output[outIdx++]);
Assertions.assertEquals((byte) 0, output[outIdx++]);
Assertions.assertEquals((byte) 0, output[outIdx++]);
Assertions.assertEquals((byte)'X', output[outIdx++]);
Assertions.assertEquals((byte) 'X', output[outIdx++]);
Assertions.assertEquals((byte) 4, output[outIdx++]);
Assertions.assertEquals((byte)'n', output[outIdx++]);
Assertions.assertEquals((byte)'a', output[outIdx++]);
Assertions.assertEquals((byte)'m', output[outIdx++]);
Assertions.assertEquals((byte)'e', output[outIdx++]);
Assertions.assertEquals((byte) 'n', output[outIdx++]);
Assertions.assertEquals((byte) 'a', output[outIdx++]);
Assertions.assertEquals((byte) 'm', output[outIdx++]);
Assertions.assertEquals((byte) 'e', output[outIdx++]);
Assertions.assertEquals((byte) 2, output[outIdx++]);
Assertions.assertEquals((byte) 0, output[outIdx++]);
Assertions.assertEquals((byte) 0, output[outIdx++]);
Assertions.assertEquals((byte)'Y', output[outIdx++]);
Assertions.assertEquals((byte) 'Y', output[outIdx++]);
Assertions.assertEquals((byte) 3, output[outIdx++]);
Assertions.assertEquals((byte)'m', output[outIdx++]);
Assertions.assertEquals((byte)'g', output[outIdx++]);
Assertions.assertEquals((byte)'r', output[outIdx++]);
Assertions.assertEquals((byte) 'm', output[outIdx++]);
Assertions.assertEquals((byte) 'g', output[outIdx++]);
Assertions.assertEquals((byte) 'r', output[outIdx++]);
Assertions.assertEquals((byte) 3, output[outIdx++]);
Assertions.assertEquals((byte) 0, output[outIdx++]);
Assertions.assertEquals((byte) 0, output[outIdx++]);
Assertions.assertEquals((byte) 2, output[outIdx++]);
Assertions.assertEquals((byte)'N', output[outIdx++]);
Assertions.assertEquals((byte)'O', output[outIdx++]);
Assertions.assertEquals((byte)'T', output[outIdx++]);
Assertions.assertEquals((byte) 'N', output[outIdx++]);
Assertions.assertEquals((byte) 'O', output[outIdx++]);
Assertions.assertEquals((byte) 'T', output[outIdx++]);
Assertions.assertEquals((byte) 3, output[outIdx++]);
Assertions.assertEquals((byte)'J', output[outIdx++]);
Assertions.assertEquals((byte)'o', output[outIdx++]);
Assertions.assertEquals((byte)'e', output[outIdx++]);
Assertions.assertEquals((byte)'z', output[outIdx++]);
Assertions.assertEquals((byte)'e', output[outIdx++]); // IBM SPEC example FAIL: 'x' is for WITH namespace
Assertions.assertEquals((byte) 'J', output[outIdx++]);
Assertions.assertEquals((byte) 'o', output[outIdx++]);
Assertions.assertEquals((byte) 'e', output[outIdx++]);
Assertions.assertEquals((byte) 'z', output[outIdx++]);
Assertions.assertEquals((byte) 'e', output[outIdx++]); // IBM SPEC example FAIL: 'x' is for WITH namespace
Assertions.assertEquals((byte) 2, output[outIdx++]);
//Assertions.assertEquals((byte) 0, output[outIdx++]); // IBM SPEC example FAIL: 'x' is for WITH namespace
//Assertions.assertEquals((byte) 0, output[outIdx++]); // IBM SPEC example FAIL: 'x' is for WITH namespace
Assertions.assertEquals((byte)'T', output[outIdx++]);
// Assertions.assertEquals((byte) 0, output[outIdx++]); // IBM SPEC example FAIL: 'x' is for WITH namespace
// Assertions.assertEquals((byte) 0, output[outIdx++]); // IBM SPEC example FAIL: 'x' is for WITH namespace
Assertions.assertEquals((byte) 'T', output[outIdx++]);
Assertions.assertEquals((byte) 5, output[outIdx++]);
Assertions.assertEquals((byte)'S', output[outIdx++]);
Assertions.assertEquals((byte)'u', output[outIdx++]);
Assertions.assertEquals((byte)'s', output[outIdx++]);
Assertions.assertEquals((byte)'a', output[outIdx++]);
Assertions.assertEquals((byte)'n', output[outIdx++]);
Assertions.assertEquals((byte)'z', output[outIdx++]);
Assertions.assertEquals((byte)'e', output[outIdx++]); // IBM SPEC example FAIL: 'x' is for WITH namespace
Assertions.assertEquals((byte) 'S', output[outIdx++]);
Assertions.assertEquals((byte) 'u', output[outIdx++]);
Assertions.assertEquals((byte) 's', output[outIdx++]);
Assertions.assertEquals((byte) 'a', output[outIdx++]);
Assertions.assertEquals((byte) 'n', output[outIdx++]);
Assertions.assertEquals((byte) 'z', output[outIdx++]);
Assertions.assertEquals((byte) 'e', output[outIdx++]); // IBM SPEC example FAIL: 'x' is for WITH namespace
Assertions.assertEquals((byte) 2, output[outIdx++]);
//Assertions.assertEquals((byte) 0, output[outIdx++]); // IBM SPEC example FAIL: 'x' is for WITH namespace
//Assertions.assertEquals((byte) 0, output[outIdx++]); // IBM SPEC example FAIL: 'x' is for WITH namespace
Assertions.assertEquals((byte)'T', output[outIdx++]);
// Assertions.assertEquals((byte) 0, output[outIdx++]); // IBM SPEC example FAIL: 'x' is for WITH namespace
// Assertions.assertEquals((byte) 0, output[outIdx++]); // IBM SPEC example FAIL: 'x' is for WITH namespace
Assertions.assertEquals((byte) 'T', output[outIdx++]);
Assertions.assertEquals((byte) 4, output[outIdx++]);
Assertions.assertEquals((byte)'B', output[outIdx++]);
Assertions.assertEquals((byte)'i', output[outIdx++]);
Assertions.assertEquals((byte)'l', output[outIdx++]);
Assertions.assertEquals((byte)'l', output[outIdx++]);
Assertions.assertEquals((byte)'z', output[outIdx++]);
Assertions.assertEquals((byte)'z', output[outIdx++]);
Assertions.assertEquals((byte)'Z', output[outIdx++]);
Assertions.assertEquals((byte) 'B', output[outIdx++]);
Assertions.assertEquals((byte) 'i', output[outIdx++]);
Assertions.assertEquals((byte) 'l', output[outIdx++]);
Assertions.assertEquals((byte) 'l', output[outIdx++]);
Assertions.assertEquals((byte) 'z', output[outIdx++]);
Assertions.assertEquals((byte) 'z', output[outIdx++]);
Assertions.assertEquals((byte) 'Z', output[outIdx++]);
}
@Test
public void testExample3StringIds() throws Exception {
@ -143,14 +142,14 @@ public class XDBXWriterXmlTest {
try (XDBXWriterXml writer = new XDBXWriterXml(baos)) {
writer.getPropertyConfig().setProperty(XDBXWriterXml.OUTPUT_DECLARATION, false);
AttributesImpl atts;
writer.startDocument();
writer.startPrefixMapping("foo", "bar");
writer.startElement("", "root", "", new AttributesImpl());
writer.startElement("", "Person", "", new AttributesImpl());
atts = new AttributesImpl();
atts.addAttribute ("", "mgr", "", "", "NO");
atts = new AttributesImpl();
atts.addAttribute("", "mgr", "", "", "NO");
writer.startElement("", "name", "", atts);
writer.characters("Bill");
writer.endElement("", "name", "");
@ -158,10 +157,10 @@ public class XDBXWriterXmlTest {
writer.characters("35");
writer.endElement("bar", "age", "age");
writer.endElement("", "Person", "");
writer.startElement("", "Person", "", new AttributesImpl());
atts = new AttributesImpl();
atts.addAttribute ("", "mgr", "", "", "NO");
atts = new AttributesImpl();
atts.addAttribute("", "mgr", "", "", "NO");
writer.startElement("", "name", "", atts);
writer.characters("Joe");
writer.endElement("", "name", "");
@ -169,134 +168,134 @@ public class XDBXWriterXmlTest {
writer.characters("45");
writer.endElement("bar", "age", "age");
writer.endElement("", "Person", "");
writer.endElement("", "root", "");
writer.endDocument();
}
byte[] output = baos.toByteArray();
int outIdx = 8;
Assertions.assertNotNull(output);
Assertions.assertTrue(output.length > 0);
Assertions.assertEquals((byte)'I', output[outIdx++]);
Assertions.assertEquals((byte) 'I', output[outIdx++]);
Assertions.assertEquals((byte) 3, output[outIdx++]);
Assertions.assertEquals((byte)'f', output[outIdx++]);
Assertions.assertEquals((byte)'o', output[outIdx++]);
Assertions.assertEquals((byte)'o', output[outIdx++]);
Assertions.assertEquals((byte) 'f', output[outIdx++]);
Assertions.assertEquals((byte) 'o', output[outIdx++]);
Assertions.assertEquals((byte) 'o', output[outIdx++]);
Assertions.assertEquals((byte) 1, output[outIdx++]);
Assertions.assertEquals((byte)'I', output[outIdx++]);
Assertions.assertEquals((byte) 'I', output[outIdx++]);
Assertions.assertEquals((byte) 3, output[outIdx++]);
Assertions.assertEquals((byte)'b', output[outIdx++]);
Assertions.assertEquals((byte)'a', output[outIdx++]);
Assertions.assertEquals((byte)'r', output[outIdx++]);
Assertions.assertEquals((byte) 'b', output[outIdx++]);
Assertions.assertEquals((byte) 'a', output[outIdx++]);
Assertions.assertEquals((byte) 'r', output[outIdx++]);
Assertions.assertEquals((byte) 2, output[outIdx++]);
Assertions.assertEquals((char)'X', (char)output[outIdx++]);
Assertions.assertEquals((char) 'X', (char) output[outIdx++]);
Assertions.assertEquals((byte) 4, output[outIdx++]);
Assertions.assertEquals((byte)'r', output[outIdx++]);
Assertions.assertEquals((byte)'o', output[outIdx++]);
Assertions.assertEquals((byte)'o', output[outIdx++]);
Assertions.assertEquals((byte)'t', output[outIdx++]);
Assertions.assertEquals((byte) 'r', output[outIdx++]);
Assertions.assertEquals((byte) 'o', output[outIdx++]);
Assertions.assertEquals((byte) 'o', output[outIdx++]);
Assertions.assertEquals((byte) 't', output[outIdx++]);
Assertions.assertEquals((byte) 3, output[outIdx++]);
Assertions.assertEquals((byte) 0, output[outIdx++]);
Assertions.assertEquals((byte) 0, output[outIdx++]);
Assertions.assertEquals((char)'m', (char)output[outIdx++]);
Assertions.assertEquals((char) 'm', (char) output[outIdx++]);
Assertions.assertEquals((byte) 1, output[outIdx++]);
Assertions.assertEquals((byte) 2, output[outIdx++]);
Assertions.assertEquals((char)'X', (char)output[outIdx++]);
Assertions.assertEquals((char) 'X', (char) output[outIdx++]);
Assertions.assertEquals((byte) 6, output[outIdx++]);
Assertions.assertEquals((byte)'P', output[outIdx++]);
Assertions.assertEquals((byte)'e', output[outIdx++]);
Assertions.assertEquals((byte)'r', output[outIdx++]);
Assertions.assertEquals((byte)'s', output[outIdx++]);
Assertions.assertEquals((byte)'o', output[outIdx++]);
Assertions.assertEquals((byte)'n', output[outIdx++]);
Assertions.assertEquals((byte) 'P', output[outIdx++]);
Assertions.assertEquals((byte) 'e', output[outIdx++]);
Assertions.assertEquals((byte) 'r', output[outIdx++]);
Assertions.assertEquals((byte) 's', output[outIdx++]);
Assertions.assertEquals((byte) 'o', output[outIdx++]);
Assertions.assertEquals((byte) 'n', output[outIdx++]);
Assertions.assertEquals((byte) 4, output[outIdx++]);
Assertions.assertEquals((byte) 0, output[outIdx++]);
Assertions.assertEquals((byte) 0, output[outIdx++]);
Assertions.assertEquals((char)'X', (char)output[outIdx++]);
Assertions.assertEquals((char) 'X', (char) output[outIdx++]);
Assertions.assertEquals((byte) 4, output[outIdx++]);
Assertions.assertEquals((byte)'n', output[outIdx++]);
Assertions.assertEquals((byte)'a', output[outIdx++]);
Assertions.assertEquals((byte)'m', output[outIdx++]);
Assertions.assertEquals((byte)'e', output[outIdx++]);
Assertions.assertEquals((byte) 'n', output[outIdx++]);
Assertions.assertEquals((byte) 'a', output[outIdx++]);
Assertions.assertEquals((byte) 'm', output[outIdx++]);
Assertions.assertEquals((byte) 'e', output[outIdx++]);
Assertions.assertEquals((byte) 5, output[outIdx++]);
Assertions.assertEquals((byte) 0, output[outIdx++]);
Assertions.assertEquals((byte) 0, output[outIdx++]);
Assertions.assertEquals((char)'Y', (char)output[outIdx++]);
Assertions.assertEquals((char) 'Y', (char) output[outIdx++]);
Assertions.assertEquals((byte) 3, output[outIdx++]);
Assertions.assertEquals((byte)'m', output[outIdx++]);
Assertions.assertEquals((byte)'g', output[outIdx++]);
Assertions.assertEquals((byte)'r', output[outIdx++]);
Assertions.assertEquals((byte) 'm', output[outIdx++]);
Assertions.assertEquals((byte) 'g', output[outIdx++]);
Assertions.assertEquals((byte) 'r', output[outIdx++]);
Assertions.assertEquals((byte) 6, output[outIdx++]);
Assertions.assertEquals((byte) 0, output[outIdx++]);
Assertions.assertEquals((byte) 0, output[outIdx++]);
Assertions.assertEquals((byte) 2, output[outIdx++]);
Assertions.assertEquals((byte)'N', output[outIdx++]);
Assertions.assertEquals((byte)'O', output[outIdx++]);
Assertions.assertEquals((byte)'T', output[outIdx++]);
Assertions.assertEquals((byte) 'N', output[outIdx++]);
Assertions.assertEquals((byte) 'O', output[outIdx++]);
Assertions.assertEquals((byte) 'T', output[outIdx++]);
Assertions.assertEquals((byte) 4, output[outIdx++]);
Assertions.assertEquals((byte)'B', output[outIdx++]);
Assertions.assertEquals((byte)'i', output[outIdx++]);
Assertions.assertEquals((byte)'l', output[outIdx++]);
Assertions.assertEquals((byte)'l', output[outIdx++]);
Assertions.assertEquals((byte)'z', output[outIdx++]);
Assertions.assertEquals((char)'X', (char)output[outIdx++]);
Assertions.assertEquals((byte) 'B', output[outIdx++]);
Assertions.assertEquals((byte) 'i', output[outIdx++]);
Assertions.assertEquals((byte) 'l', output[outIdx++]);
Assertions.assertEquals((byte) 'l', output[outIdx++]);
Assertions.assertEquals((byte) 'z', output[outIdx++]);
Assertions.assertEquals((char) 'X', (char) output[outIdx++]);
Assertions.assertEquals((byte) 3, output[outIdx++]);
Assertions.assertEquals((byte)'a', output[outIdx++]);
Assertions.assertEquals((byte)'g', output[outIdx++]);
Assertions.assertEquals((byte)'e', output[outIdx++]);
Assertions.assertEquals((byte) 'a', output[outIdx++]);
Assertions.assertEquals((byte) 'g', output[outIdx++]);
Assertions.assertEquals((byte) 'e', output[outIdx++]);
Assertions.assertEquals((byte) 7, output[outIdx++]);
Assertions.assertEquals((byte) 1, output[outIdx++]);
Assertions.assertEquals((byte) 2, output[outIdx++]);
Assertions.assertEquals((byte) 'T', output[outIdx++]);
Assertions.assertEquals((byte) 2, output[outIdx++]);
Assertions.assertEquals((byte)'3', output[outIdx++]);
Assertions.assertEquals((byte)'5', output[outIdx++]);
Assertions.assertEquals((byte)'z', output[outIdx++]);
Assertions.assertEquals((byte)'z', output[outIdx++]);
Assertions.assertEquals((byte)'e', output[outIdx++]);
Assertions.assertEquals((byte) '3', output[outIdx++]);
Assertions.assertEquals((byte) '5', output[outIdx++]);
Assertions.assertEquals((byte) 'z', output[outIdx++]);
Assertions.assertEquals((byte) 'z', output[outIdx++]);
Assertions.assertEquals((byte) 'e', output[outIdx++]);
Assertions.assertEquals((byte) 4, output[outIdx++]);
Assertions.assertEquals((byte)'e', output[outIdx++]);
Assertions.assertEquals((byte) 'e', output[outIdx++]);
Assertions.assertEquals((byte) 5, output[outIdx++]);
Assertions.assertEquals((byte)'a', output[outIdx++]);
Assertions.assertEquals((byte) 'a', output[outIdx++]);
Assertions.assertEquals((byte) 6, output[outIdx++]);
Assertions.assertEquals((byte) 2, output[outIdx++]);
Assertions.assertEquals((byte)'N', output[outIdx++]);
Assertions.assertEquals((byte)'O', output[outIdx++]);
Assertions.assertEquals((byte)'T', output[outIdx++]);
Assertions.assertEquals((byte) 'N', output[outIdx++]);
Assertions.assertEquals((byte) 'O', output[outIdx++]);
Assertions.assertEquals((byte) 'T', output[outIdx++]);
Assertions.assertEquals((byte) 3, output[outIdx++]);
Assertions.assertEquals((byte)'J', output[outIdx++]);
Assertions.assertEquals((byte)'o', output[outIdx++]);
Assertions.assertEquals((byte)'e', output[outIdx++]);
Assertions.assertEquals((byte)'z', output[outIdx++]);
Assertions.assertEquals((byte)'x', output[outIdx++]);
Assertions.assertEquals((byte) 'J', output[outIdx++]);
Assertions.assertEquals((byte) 'o', output[outIdx++]);
Assertions.assertEquals((byte) 'e', output[outIdx++]);
Assertions.assertEquals((byte) 'z', output[outIdx++]);
Assertions.assertEquals((byte) 'x', output[outIdx++]);
Assertions.assertEquals((byte) 7, output[outIdx++]);
Assertions.assertEquals((byte) 1, output[outIdx++]);
Assertions.assertEquals((byte) 2, output[outIdx++]);
Assertions.assertEquals((byte)'T', output[outIdx++]);
Assertions.assertEquals((byte) 'T', output[outIdx++]);
Assertions.assertEquals((byte) 2, output[outIdx++]);
Assertions.assertEquals((byte)'4', output[outIdx++]);
Assertions.assertEquals((byte)'5', output[outIdx++]);
Assertions.assertEquals((byte)'z', output[outIdx++]);
Assertions.assertEquals((byte)'z', output[outIdx++]);
Assertions.assertEquals((byte)'z', output[outIdx++]);
Assertions.assertEquals((byte)'Z', output[outIdx++]);
Assertions.assertEquals((byte) '4', output[outIdx++]);
Assertions.assertEquals((byte) '5', output[outIdx++]);
Assertions.assertEquals((byte) 'z', output[outIdx++]);
Assertions.assertEquals((byte) 'z', output[outIdx++]);
Assertions.assertEquals((byte) 'z', output[outIdx++]);
Assertions.assertEquals((byte) 'Z', output[outIdx++]);
StringWriter outputXmlStr = new StringWriter();
SAX3WriterXml outputXml = new SAX3WriterXml(outputXmlStr);
XDBXReaderXml reader = new XDBXReaderXml(outputXml);
ByteArrayInputStream bais = new ByteArrayInputStream(output);
reader.parse(bais);
String outputStr = outputXmlStr.toString();
Assertions.assertTrue(outputStr.contains("<root"));
Assertions.assertTrue(outputStr.contains("xmlns:foo=\"bar\""));