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) { static public String getDocumentDeclaration(String encoding) {
return getDocumentDeclaration(encoding,null); return getDocumentDeclaration(encoding,null);
} }

View file

@ -49,11 +49,6 @@ import org.xml.sax.ext.DefaultHandler2;
*/ */
public class XMLWriter extends 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 encoding = null;
private String charNewline = null; private String charNewline = null;
private String charTab = 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); this(new OutputStreamWriter(out, XMLConstants.XML_DEFAULT_ENCODING),XMLConstants.XML_DEFAULT_ENCODING);
} }
/** /**
* @see org.xml.sax.ContentHandler#startDocument() * @see org.xml.sax.ContentHandler#startDocument()
*/ */
@Override @Override
public void startDocument() throws SAXException { public void startDocument() throws SAXException {
indent = 0; indent = 0;
try { write(XMLConstants.getDocumentDeclaration(encoding));
out.write(XMLConstants.getDocumentDeclaration(encoding));
} catch (IOException e) {
throw new SAXException(e);
}
} }
/** /**
@ -143,11 +132,7 @@ public class XMLWriter extends DefaultHandler2 {
*/ */
@Override @Override
public void endDocument() throws SAXException { public void endDocument() throws SAXException {
try { writeFlush();
out.flush();
} catch (IOException e) {
throw new SAXException(e);
}
} }
/** /**
@ -159,9 +144,8 @@ public class XMLWriter extends DefaultHandler2 {
*/ */
@Override @Override
public void startElement(String uri, String localName, String name,Attributes atts) throws SAXException { public void startElement(String uri, String localName, String name,Attributes atts) throws SAXException {
try {
if (startElement!=null) { if (startElement!=null) {
out.write(startElement.toString()); write(startElement.toString());
startElement=null; startElement=null;
} }
startElement = new StringBuffer(200); startElement = new StringBuffer(200);
@ -260,12 +244,8 @@ public class XMLWriter extends DefaultHandler2 {
startElement.append('"'); startElement.append('"');
} }
startElement.append(XMLConstants.TAG_CLOSE); startElement.append(XMLConstants.TAG_CLOSE);
} catch (IOException e) {
throw new SAXException(e);
} finally {
indent++; indent++;
} }
}
/** /**
* @param uri The xml namespace uri. * @param uri The xml namespace uri.
@ -275,18 +255,16 @@ public class XMLWriter extends DefaultHandler2 {
*/ */
@Override @Override
public void endElement(String uri, String localName, String name) throws SAXException { public void endElement(String uri, String localName, String name) throws SAXException {
try {
if (startElement!=null) { if (startElement!=null) {
String ss = startElement.toString(); write(startElement.toString());
out.write(ss,0,ss.length()-1); write(XMLConstants.TAG_CLOSE_EMPTY);
out.write(XMLConstants.TAG_CLOSE_EMPTY);
startElement=null; startElement=null;
indent--; indent--;
return; return;
} }
if (printedReturn==false) { if (printedReturn==false) {
out.write(charNewline); write(charNewline);
} }
printedReturn=false; printedReturn=false;
indent--; indent--;
@ -296,24 +274,21 @@ public class XMLWriter extends DefaultHandler2 {
localName = "null"; localName = "null";
} }
out.write(XMLConstants.TAG_OPEN_END); write(XMLConstants.TAG_OPEN_END);
if (XMLConstants.NULL_NS_URI.equals(uri) | uri==null) { if (XMLConstants.NULL_NS_URI.equals(uri) | uri==null) {
out.write(localName); write(localName);
} else { } else {
String prefix = prefixMapping.get(uri); String prefix = prefixMapping.get(uri);
if (prefix==null) { if (prefix==null) {
throw new SAXException("preFixUri: "+uri+" is not started."); throw new SAXException("preFixUri: "+uri+" is not started.");
} }
if (XMLConstants.NULL_NS_URI.equals(prefix)==false) { if (XMLConstants.NULL_NS_URI.equals(prefix)==false) {
out.write(prefix); write(prefix);
out.write(XMLConstants.XMLNS_ASSIGN); write(XMLConstants.XMLNS_ASSIGN);
} }
out.write(localName); write(localName);
}
out.write(XMLConstants.TAG_CLOSE);
} catch (IOException e) {
throw new SAXException(e);
} }
write(XMLConstants.TAG_CLOSE);
} }
/** /**
@ -360,21 +335,17 @@ public class XMLWriter extends DefaultHandler2 {
*/ */
@Override @Override
public void characters(char[] ch, int start, int length) throws SAXException { public void characters(char[] ch, int start, int length) throws SAXException {
try {
if (startElement!=null) { if (startElement!=null) {
out.write(startElement.toString()); write(startElement.toString());
startElement=null; startElement=null;
} }
for (int i=start;i<(start+length);i++) { for (int i=start;i<(start+length);i++) {
char c = ch[i]; char c = ch[i];
out.write(c); write(c);
if (c=='\n') { if (c=='\n') {
printedReturn=true; printedReturn=true;
} }
} }
} catch (IOException e) {
throw new SAXException(e);
}
} }
/** /**
@ -388,15 +359,11 @@ public class XMLWriter extends DefaultHandler2 {
*/ */
@Override @Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
try {
if (startElement!=null) { if (startElement!=null) {
out.write(startElement.toString()); write(startElement.toString());
startElement=null; startElement=null;
} }
out.write(ch, start, length); write(ch, start, length);
} catch (IOException e) {
throw new SAXException(e);
}
} }
/** /**
@ -408,18 +375,14 @@ public class XMLWriter extends DefaultHandler2 {
*/ */
@Override @Override
public void processingInstruction(String target, String data) throws SAXException { public void processingInstruction(String target, String data) throws SAXException {
try {
writeIndent(); writeIndent();
out.write(XMLConstants.PROCESS_START); write(XMLConstants.PROCESS_START);
out.write(target); write(target);
out.write(' '); write(' ');
out.write(data); write(data);
out.write(XMLConstants.PROCESS_END); write(XMLConstants.PROCESS_END);
out.write(charNewline); write(charNewline);
out.flush(); writeFlush();
} catch (IOException e) {
throw new SAXException(e);
}
} }
/** /**
@ -454,33 +417,61 @@ public class XMLWriter extends DefaultHandler2 {
*/ */
@Override @Override
public void comment(char[] ch, int start, int length) throws SAXException { public void comment(char[] ch, int start, int length) throws SAXException {
try {
writeIndent(); writeIndent();
out.write(XMLConstants.COMMENT_START); write(XMLConstants.COMMENT_START);
/// mmm todo improve a bit /// mmm todo improve a bit
for (int i=start;i<(start+length);i++) { for (int i=start;i<(start+length);i++) {
char c = ch[i]; char c = ch[i];
if (c=='\n') { if (c=='\n') {
out.write(c); write(c);
writeIndent(); writeIndent();
continue; continue;
} }
out.write(c); write(c);
}
out.write(XMLConstants.COMMENT_END);
} catch (IOException e) {
throw new SAXException(e);
} }
write(XMLConstants.COMMENT_END);
} }
/** /**
* Indent the output writer with tabs by indent count. * Indent the output writer with tabs by indent count.
* @throws IOException When prints gives exception. * @throws IOException When prints gives exception.
*/ */
private void writeIndent() throws IOException { private void writeIndent() throws SAXException {
for (int i = 0; i < indent; i++) { 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 { 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. */ /** Read-Only property returning the language we are working with. */
LANGUAGE_NAME(IO.GLOBAL,"language/name"), 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="JInternalFrameBindingHandler" bean.class="org.x4o.xml.test.swixml.bind.JInternalFrameBindingHandler"/>
<bindingHandler id="JPanelBindingHandler" bean.class="org.x4o.xml.test.swixml.bind.JPanelBindingHandler"/> <bindingHandler id="JPanelBindingHandler" bean.class="org.x4o.xml.test.swixml.bind.JPanelBindingHandler"/>
<bindingHandler id="JSplitPaneBindingHandler" bean.class="org.x4o.xml.test.swixml.bind.JSplitPaneBindingHandler"/> <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="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="todo"/> <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="todo"/> <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="todo"/> <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="todo"/> <classBindingHandler id="JMenu-JMenuItem" parentClass="javax.swing.JMenu" childClass="javax.swing.JMenuItem" addMethod="add" getMethod="getComponents"/>
<elementInterface id="Component" interfaceClass="java.awt.Component"> <elementInterface id="Component" interfaceClass="java.awt.Component">
<attribute name="bounds"> <attribute name="bounds">

View file

@ -38,11 +38,11 @@
<bindingHandler id="JInternalFrameBindingHandler" bean.class="org.x4o.xml.test.swixml.bind.JInternalFrameBindingHandler"/> <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="JPanelBindingHandler" bean.class="org.x4o.xml.test.swixml.bind.JPanelBindingHandler"/>
<bindingHandler id="JSplitPaneBindingHandler" bean.class="org.x4o.xml.test.swixml.bind.JSplitPaneBindingHandler"/> <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="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="todo"/> <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="todo"/> <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="todo"/> <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="todo"/> <classBindingHandler id="JMenu-JMenuItem" parentClass="javax.swing.JMenu" childClass="javax.swing.JMenuItem" addMethod="add" getMethod="getComponents"/>
<elementInterface id="Component" interfaceClass="java.awt.Component"> <elementInterface id="Component" interfaceClass="java.awt.Component">
<attribute name="bounds"> <attribute name="bounds">