Done swixml two sided binding handlers methods and small things in xml

writer.
This commit is contained in:
Willem Cazander 2013-04-08 22:31:11 +02:00
parent 37d249eb24
commit 9ea83fdd1a
5 changed files with 217 additions and 222 deletions

View file

@ -124,7 +124,6 @@ public final class XMLConstants {
static public String getDocumentDeclaration(String encoding) {
return getDocumentDeclaration(encoding,null);
}

View file

@ -49,11 +49,6 @@ import org.xml.sax.ext.DefaultHandler2;
*/
public class XMLWriter extends DefaultHandler2 {
private final static String ENCODING = "http://writer.x4o.org/xml/properties/encoding";
private final static String CHAR_NEWLINE = "http://writer.x4o.org/xml/properties/char/newline";
private final static String CHAR_TAB = "http://writer.x4o.org/xml/properties/char/tab";
private final static String URI_PREFX = "http://writer.x4o.org/xml/properties/char/";
private String encoding = null;
private String charNewline = null;
private String charTab = null;
@ -123,19 +118,13 @@ public class XMLWriter extends DefaultHandler2 {
this(new OutputStreamWriter(out, XMLConstants.XML_DEFAULT_ENCODING),XMLConstants.XML_DEFAULT_ENCODING);
}
/**
* @see org.xml.sax.ContentHandler#startDocument()
*/
@Override
public void startDocument() throws SAXException {
indent = 0;
try {
out.write(XMLConstants.getDocumentDeclaration(encoding));
} catch (IOException e) {
throw new SAXException(e);
}
write(XMLConstants.getDocumentDeclaration(encoding));
}
/**
@ -143,11 +132,7 @@ public class XMLWriter extends DefaultHandler2 {
*/
@Override
public void endDocument() throws SAXException {
try {
out.flush();
} catch (IOException e) {
throw new SAXException(e);
}
writeFlush();
}
/**
@ -159,9 +144,8 @@ public class XMLWriter extends DefaultHandler2 {
*/
@Override
public void startElement(String uri, String localName, String name,Attributes atts) throws SAXException {
try {
if (startElement!=null) {
out.write(startElement.toString());
write(startElement.toString());
startElement=null;
}
startElement = new StringBuffer(200);
@ -260,12 +244,8 @@ public class XMLWriter extends DefaultHandler2 {
startElement.append('"');
}
startElement.append(XMLConstants.TAG_CLOSE);
} catch (IOException e) {
throw new SAXException(e);
} finally {
indent++;
}
}
/**
* @param uri The xml namespace uri.
@ -275,18 +255,16 @@ public class XMLWriter extends DefaultHandler2 {
*/
@Override
public void endElement(String uri, String localName, String name) throws SAXException {
try {
if (startElement!=null) {
String ss = startElement.toString();
out.write(ss,0,ss.length()-1);
out.write(XMLConstants.TAG_CLOSE_EMPTY);
write(startElement.toString());
write(XMLConstants.TAG_CLOSE_EMPTY);
startElement=null;
indent--;
return;
}
if (printedReturn==false) {
out.write(charNewline);
write(charNewline);
}
printedReturn=false;
indent--;
@ -296,24 +274,21 @@ public class XMLWriter extends DefaultHandler2 {
localName = "null";
}
out.write(XMLConstants.TAG_OPEN_END);
write(XMLConstants.TAG_OPEN_END);
if (XMLConstants.NULL_NS_URI.equals(uri) | uri==null) {
out.write(localName);
write(localName);
} else {
String prefix = prefixMapping.get(uri);
if (prefix==null) {
throw new SAXException("preFixUri: "+uri+" is not started.");
}
if (XMLConstants.NULL_NS_URI.equals(prefix)==false) {
out.write(prefix);
out.write(XMLConstants.XMLNS_ASSIGN);
write(prefix);
write(XMLConstants.XMLNS_ASSIGN);
}
out.write(localName);
}
out.write(XMLConstants.TAG_CLOSE);
} catch (IOException e) {
throw new SAXException(e);
write(localName);
}
write(XMLConstants.TAG_CLOSE);
}
/**
@ -360,21 +335,17 @@ public class XMLWriter extends DefaultHandler2 {
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
try {
if (startElement!=null) {
out.write(startElement.toString());
write(startElement.toString());
startElement=null;
}
for (int i=start;i<(start+length);i++) {
char c = ch[i];
out.write(c);
write(c);
if (c=='\n') {
printedReturn=true;
}
}
} catch (IOException e) {
throw new SAXException(e);
}
}
/**
@ -388,15 +359,11 @@ public class XMLWriter extends DefaultHandler2 {
*/
@Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
try {
if (startElement!=null) {
out.write(startElement.toString());
write(startElement.toString());
startElement=null;
}
out.write(ch, start, length);
} catch (IOException e) {
throw new SAXException(e);
}
write(ch, start, length);
}
/**
@ -408,18 +375,14 @@ public class XMLWriter extends DefaultHandler2 {
*/
@Override
public void processingInstruction(String target, String data) throws SAXException {
try {
writeIndent();
out.write(XMLConstants.PROCESS_START);
out.write(target);
out.write(' ');
out.write(data);
out.write(XMLConstants.PROCESS_END);
out.write(charNewline);
out.flush();
} catch (IOException e) {
throw new SAXException(e);
}
write(XMLConstants.PROCESS_START);
write(target);
write(' ');
write(data);
write(XMLConstants.PROCESS_END);
write(charNewline);
writeFlush();
}
/**
@ -454,33 +417,61 @@ public class XMLWriter extends DefaultHandler2 {
*/
@Override
public void comment(char[] ch, int start, int length) throws SAXException {
try {
writeIndent();
out.write(XMLConstants.COMMENT_START);
write(XMLConstants.COMMENT_START);
/// mmm todo improve a bit
for (int i=start;i<(start+length);i++) {
char c = ch[i];
if (c=='\n') {
out.write(c);
write(c);
writeIndent();
continue;
}
out.write(c);
}
out.write(XMLConstants.COMMENT_END);
} catch (IOException e) {
throw new SAXException(e);
write(c);
}
write(XMLConstants.COMMENT_END);
}
/**
* Indent the output writer with tabs by indent count.
* @throws IOException When prints gives exception.
*/
private void writeIndent() throws IOException {
private void writeIndent() throws SAXException {
for (int i = 0; i < indent; i++) {
out.write(charTab);
write(charTab);
}
}
private void writeFlush() throws SAXException {
try {
out.flush();
} catch (IOException e) {
throw new SAXException(e);
}
}
private void write(String text) throws SAXException {
try {
out.write(text);
} catch (IOException e) {
throw new SAXException(e);
}
}
private void write(char[] ch, int start, int length) throws SAXException {
try {
out.write(ch,start,length);
} catch (IOException e) {
throw new SAXException(e);
}
}
private void write(char c) throws SAXException {
try {
out.write(c);
} catch (IOException e) {
throw new SAXException(e);
}
}
}

View file

@ -46,7 +46,12 @@ import org.xml.sax.ext.DefaultHandler2;
*/
public enum X4OLanguageProperty {
/* TODO: refactor all keys ?
private final static String ENCODING = "http://writer.x4o.org/xml/properties/encoding";
private final static String CHAR_NEWLINE = "http://writer.x4o.org/xml/properties/char/newline";
private final static String CHAR_TAB = "http://writer.x4o.org/xml/properties/char/tab";
private final static String URI_PREFX = "http://writer.x4o.org/xml/properties/char/";
*/
/** Read-Only property returning the language we are working with. */
LANGUAGE_NAME(IO.GLOBAL,"language/name"),

View file

@ -38,11 +38,11 @@
<bindingHandler id="JInternalFrameBindingHandler" bean.class="org.x4o.xml.test.swixml.bind.JInternalFrameBindingHandler"/>
<bindingHandler id="JPanelBindingHandler" bean.class="org.x4o.xml.test.swixml.bind.JPanelBindingHandler"/>
<bindingHandler id="JSplitPaneBindingHandler" bean.class="org.x4o.xml.test.swixml.bind.JSplitPaneBindingHandler"/>
<classBindingHandler id="JScrollPane-JComponent" parentClass="javax.swing.JScrollPane" childClass="javax.swing.JComponent" addMethod="setViewportView" getMethod="todo"/>
<classBindingHandler id="JDesktopPane-JInternalFrame" parentClass="javax.swing.JDesktopPane" childClass="javax.swing.JInternalFrame" addMethod="add" getMethod="todo"/>
<classBindingHandler id="JFrame-JDesktopPane" parentClass="javax.swing.JFrame" childClass="javax.swing.JDesktopPane" addMethod="setContentPane" getMethod="todo"/>
<classBindingHandler id="JMenuBar-JMenu" parentClass="javax.swing.JMenuBar" childClass="javax.swing.JMenu" addMethod="add" getMethod="todo"/>
<classBindingHandler id="JMenu-JMenuItem" parentClass="javax.swing.JMenu" childClass="javax.swing.JMenuItem" addMethod="add" getMethod="todo"/>
<classBindingHandler id="JScrollPane-JComponent" parentClass="javax.swing.JScrollPane" childClass="javax.swing.JComponent" addMethod="setViewportView" getMethod="getViewport"/>
<classBindingHandler id="JDesktopPane-JInternalFrame" parentClass="javax.swing.JDesktopPane" childClass="javax.swing.JInternalFrame" addMethod="add" getMethod="getComponents"/>
<classBindingHandler id="JFrame-JDesktopPane" parentClass="javax.swing.JFrame" childClass="javax.swing.JDesktopPane" addMethod="setContentPane" getMethod="getContentPane"/>
<classBindingHandler id="JMenuBar-JMenu" parentClass="javax.swing.JMenuBar" childClass="javax.swing.JMenu" addMethod="add" getMethod="getComponents"/>
<classBindingHandler id="JMenu-JMenuItem" parentClass="javax.swing.JMenu" childClass="javax.swing.JMenuItem" addMethod="add" getMethod="getComponents"/>
<elementInterface id="Component" interfaceClass="java.awt.Component">
<attribute name="bounds">

View file

@ -38,11 +38,11 @@
<bindingHandler id="JInternalFrameBindingHandler" bean.class="org.x4o.xml.test.swixml.bind.JInternalFrameBindingHandler"/>
<bindingHandler id="JPanelBindingHandler" bean.class="org.x4o.xml.test.swixml.bind.JPanelBindingHandler"/>
<bindingHandler id="JSplitPaneBindingHandler" bean.class="org.x4o.xml.test.swixml.bind.JSplitPaneBindingHandler"/>
<classBindingHandler id="JScrollPane-JComponent" parentClass="javax.swing.JScrollPane" childClass="javax.swing.JComponent" addMethod="setViewportView" getMethod="todo"/>
<classBindingHandler id="JDesktopPane-JInternalFrame" parentClass="javax.swing.JDesktopPane" childClass="javax.swing.JInternalFrame" addMethod="add" getMethod="todo"/>
<classBindingHandler id="JFrame-JDesktopPane" parentClass="javax.swing.JFrame" childClass="javax.swing.JDesktopPane" addMethod="setContentPane" getMethod="todo"/>
<classBindingHandler id="JMenuBar-JMenu" parentClass="javax.swing.JMenuBar" childClass="javax.swing.JMenu" addMethod="add" getMethod="todo"/>
<classBindingHandler id="JMenu-JMenuItem" parentClass="javax.swing.JMenu" childClass="javax.swing.JMenuItem" addMethod="add" getMethod="todo"/>
<classBindingHandler id="JScrollPane-JComponent" parentClass="javax.swing.JScrollPane" childClass="javax.swing.JComponent" addMethod="setViewportView" getMethod="getViewport"/>
<classBindingHandler id="JDesktopPane-JInternalFrame" parentClass="javax.swing.JDesktopPane" childClass="javax.swing.JInternalFrame" addMethod="add" getMethod="getComponents"/>
<classBindingHandler id="JFrame-JDesktopPane" parentClass="javax.swing.JFrame" childClass="javax.swing.JDesktopPane" addMethod="setContentPane" getMethod="getContentPane"/>
<classBindingHandler id="JMenuBar-JMenu" parentClass="javax.swing.JMenuBar" childClass="javax.swing.JMenu" addMethod="add" getMethod="getComponents"/>
<classBindingHandler id="JMenu-JMenuItem" parentClass="javax.swing.JMenu" childClass="javax.swing.JMenuItem" addMethod="add" getMethod="getComponents"/>
<elementInterface id="Component" interfaceClass="java.awt.Component">
<attribute name="bounds">