package item; import tomp.xml.egb.*; import java.util.*; import java.io.*; public class Item implements EgbElement { // properties for ATTRIBUTES // for attribute 'created' // single property 'created' of type java.util.Date private java.util.Date created = java.text.DateFormat.getDateInstance().parse("9999-99-99", new java.text.ParsePosition(0)); public void setCreated(String aCreated) { setCreated(java.text.DateFormat.getDateInstance().parse(aCreated, new java.text.ParsePosition(0))); } public void setCreated(java.util.Date aCreated) { created = aCreated; } public java.util.Date getCreated() { return created; } // for attribute 'price' // single property 'price' of type float private float price = 1.2f; public void setPrice(String aPrice) { setPrice(Float.parseFloat(aPrice)); } public void setPrice(float aPrice) { price = aPrice; } public float getPrice() { return price; } // for attribute 'count' // single property 'count' of type int private int count = 10; public void setCount(String aCount) { setCount(Integer.parseInt(aCount)); } public void setCount(int aCount) { count = aCount; } public int getCount() { return count; } // for attribute 'id' // single property 'id' of type String private String id = "ID"; public void setId(String aId) { id = aId; } public String getId() { return id; } // properties for TEXT nodes // properties for child ELEMENTS // for text()-only element 'title' // single property 'title' of type String private String title = "Titul zbozi"; public void setTitle(String aTitle) { title = aTitle; } public String getTitle() { return title; } // list of properties 'subitem's of type Subitem private List subitems = new ArrayList(); public void addSubitem(Subitem aSubitem) { subitems.add(aSubitem); } public Subitem getSubitem(int index) { return (Subitem)subitems.get(index); } public List getSubitems() { return subitems; } public int getSubitemCount() { return subitems.size(); } // for text()-only element 'subtitle' // list of properties 'subtitle's of type String private List subtitles = new ArrayList(); public void addSubtitle(String aSubtitle) { subtitles.add(aSubtitle); } public String getSubtitle(int index) { return (String)subtitles.get(index); } public List getSubtitles() { return subtitles; } public int getSubtitleCount() { return subtitles.size(); } // for text()-only element 'another' // single property 'another' of type String private String another = "dalsi"; public void setAnother(String aAnother) { another = aAnother; } public String getAnother() { return another; } // for text()-only element 'subtitle' // list of properties 'subtitle2's of type String private List subtitle2s = new ArrayList(); public void addSubtitle2(String aSubtitle2) { subtitle2s.add(aSubtitle2); } public String getSubtitle2(int index) { return (String)subtitle2s.get(index); } public List getSubtitle2s() { return subtitle2s; } public int getSubtitle2Count() { return subtitle2s.size(); } // end of properties // class content-type is eg:group // methods for SAX2 unmarshalling public void egbStartElement(EgbContext c, String namespaceURI, String localName, String qName, org.xml.sax.Attributes attrs) { egbWaitFor = null; if (egbStage == 0 && "title".equals(localName) && "".equals(namespaceURI)) { egbWaitFor = "{}title"; return; } if (egbStage == 1 && "subitem".equals(localName) && "".equals(namespaceURI)) { Subitem e = new Subitem(); c.push(e); e.egbAttributes(qName, attrs); addSubitem(e); egbStage = 1; return; } if (egbStage == 1 && "subtitle".equals(localName) && "".equals(namespaceURI)) { egbWaitFor = "{}subtitle"; return; } if (egbStage == 2 && "another".equals(localName) && "".equals(namespaceURI)) { egbWaitFor = "{}another"; return; } if (egbStage == 2 && "subtitle".equals(localName) && "".equals(namespaceURI)) { egbWaitFor = "{}subtitle"; return; } } public void egbAttributes(String qName, org.xml.sax.Attributes attrs) { egbQName = qName; String value = null; value = attrs.getValue("", "created"); if (value != null) { setCreated(value); } value = attrs.getValue("", "price"); if (value != null) { setPrice(value); } value = attrs.getValue("", "count"); if (value != null) { setCount(value); } value = attrs.getValue("", "id"); if (value != null) { setId(value); } } public void egbCharacters(String s) { if (egbStage == 0 && "{}title".equals(egbWaitFor)) { setTitle(s); egbStage = 1; return; } if (egbStage == 1 && "{}subtitle".equals(egbWaitFor)) { addSubtitle(s); egbStage = 2; return; } if (egbStage == 2 && "{}another".equals(egbWaitFor)) { setAnother(s); egbStage = 2; return; } if (egbStage == 2 && "{}subtitle".equals(egbWaitFor)) { addSubtitle2(s); egbStage = 2; return; } } public void egbEndElement(EgbContext c) { if (egbWaitFor == null) c.pop(); else egbWaitFor = null; } private String egbWaitFor; private String egbQName; private int egbStage = 0; // methods for SAX2 marshalling public void marshall(EgbContext c) throws org.xml.sax.SAXException { org.xml.sax.helpers.AttributesImpl attrs = new org.xml.sax.helpers.AttributesImpl(); attrs.addAttribute("", "created", "created", "CDATA", String.valueOf(getCreated())); attrs.addAttribute("", "price", "price", "CDATA", String.valueOf(getPrice())); attrs.addAttribute("", "count", "count", "CDATA", String.valueOf(getCount())); attrs.addAttribute("", "id", "id", "CDATA", String.valueOf(getId())); c.outStartElement("", "item", egbQName, attrs); // unmarshalling child-element property 'title' //*** is inline-text()-only c.outStartElement("", "title", "title", new org.xml.sax.helpers.AttributesImpl()); String title = String.valueOf(getTitle()); c.outCharacters(title.toCharArray(), 0, title.length()); c.outEndElement("", "title", "title"); // unmarshalling child-element property 'subitem' //*** is normal-recursive for (int i = 0; i < getSubitemCount(); i++) getSubitem(i).marshall(c); // unmarshalling child-element property 'subtitle' //*** is inline-text()-only for (int i = 0; i < getSubtitleCount(); i++) { c.outStartElement("", "subtitle", "subtitle", new org.xml.sax.helpers.AttributesImpl()); String subtitle = String.valueOf(getSubtitle(i)); c.outCharacters(subtitle.toCharArray(), 0, subtitle.length()); c.outEndElement("", "subtitle", "subtitle"); } // unmarshalling child-element property 'another' //*** is inline-text()-only c.outStartElement("", "another", "another", new org.xml.sax.helpers.AttributesImpl()); String another = String.valueOf(getAnother()); c.outCharacters(another.toCharArray(), 0, another.length()); c.outEndElement("", "another", "another"); // unmarshalling child-element property 'subtitle' //*** is inline-text()-only for (int i = 0; i < getSubtitle2Count(); i++) { c.outStartElement("", "subtitle", "subtitle", new org.xml.sax.helpers.AttributesImpl()); String subtitle2 = String.valueOf(getSubtitle2(i)); c.outCharacters(subtitle2.toCharArray(), 0, subtitle2.length()); c.outEndElement("", "subtitle", "subtitle"); } c.outEndElement("", "item", egbQName); } }