[3] | 1 | package de.ugoe.cs.swe.memos.xml; |
---|
| 2 | |
---|
| 3 | import java.util.ArrayList; |
---|
| 4 | import java.util.Date; |
---|
| 5 | import java.util.HashMap; |
---|
| 6 | import java.util.Iterator; |
---|
| 7 | import java.util.Vector; |
---|
| 8 | |
---|
| 9 | import org.xml.sax.Attributes; |
---|
| 10 | import org.xml.sax.SAXException; |
---|
| 11 | import org.xml.sax.helpers.DefaultHandler; |
---|
| 12 | |
---|
| 13 | import de.ugoe.cs.swe.memos.datamodel.Category; |
---|
| 14 | import de.ugoe.cs.swe.memos.datamodel.File; |
---|
| 15 | import de.ugoe.cs.swe.memos.datamodel.Memo; |
---|
| 16 | import de.ugoe.cs.swe.memos.datamodel.Tag; |
---|
| 17 | import de.ugoe.cs.swe.memos.misc.Base64Coder; |
---|
| 18 | |
---|
| 19 | public class SAXHandler extends DefaultHandler { |
---|
| 20 | |
---|
| 21 | public static final int NONE = 0; |
---|
| 22 | public static final int TITLE = 1; |
---|
| 23 | public static final int AUTHOR = 2; |
---|
| 24 | public static final int TAGS = 4; |
---|
| 25 | public static final int CONTENT = 8; |
---|
| 26 | public static final int ATTACHEMENT = 16; |
---|
| 27 | |
---|
| 28 | private int state = NONE; |
---|
| 29 | private String content; |
---|
| 30 | |
---|
| 31 | private Vector<Memo> memos; |
---|
| 32 | private Vector<Category> categories; |
---|
| 33 | private HashMap<Long, Category> map; |
---|
| 34 | |
---|
| 35 | private Category category; |
---|
| 36 | private Memo memo; |
---|
| 37 | private File attachement; |
---|
| 38 | |
---|
| 39 | SAXHandler(Vector<Memo> memos) { |
---|
| 40 | this.memos = memos; |
---|
| 41 | this.memos.clear(); |
---|
| 42 | this.categories = new Vector<Category>(); |
---|
| 43 | this.map = new HashMap<Long, Category>(); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | @Override |
---|
| 47 | public void startElement(String uri, String localName, String qName, |
---|
| 48 | Attributes attributes) throws SAXException { |
---|
| 49 | |
---|
| 50 | if (qName.equals("category")) { |
---|
| 51 | this.category = new Category(""); |
---|
| 52 | this.category.setiD(Long.parseLong(attributes.getValue("id"))); |
---|
| 53 | this.category.setParentID(Long.parseLong(attributes |
---|
| 54 | .getValue("parent"))); |
---|
| 55 | |
---|
| 56 | this.categories.add(this.category); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | if (qName.equals("memo")) { |
---|
| 60 | this.memo = new Memo("", Boolean.parseBoolean(attributes |
---|
| 61 | .getValue("draft"))); |
---|
| 62 | this.memo.setiD(Long.parseLong(attributes.getValue("id"))); |
---|
| 63 | this.memo.setTimestamp(new Date(Long.parseLong(attributes |
---|
| 64 | .getValue("timestamp")))); |
---|
| 65 | this.memo.setCategoryID(Long.parseLong(attributes |
---|
| 66 | .getValue("category"))); |
---|
| 67 | this.memo.setCategory(this.map.get(this.memo.getCategoryID())); |
---|
| 68 | this.memo.setAllCategories(this.categories); |
---|
| 69 | this.memos.add(this.memo); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | if (qName.equals("title")) |
---|
| 73 | this.state = SAXHandler.TITLE; |
---|
| 74 | |
---|
| 75 | if (qName.equals("author")) |
---|
| 76 | this.state = SAXHandler.AUTHOR; |
---|
| 77 | |
---|
| 78 | if (qName.equals("tags")) |
---|
| 79 | this.state = SAXHandler.TAGS; |
---|
| 80 | |
---|
| 81 | if (qName.equals("content")) { |
---|
| 82 | this.state = SAXHandler.CONTENT; |
---|
| 83 | this.content = ""; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | if (qName.equals("attachements")) { |
---|
| 87 | this.memo.setFiles(new ArrayList<File>()); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | if (qName.equals("attachement")) { |
---|
| 91 | this.state = SAXHandler.ATTACHEMENT; |
---|
| 92 | this.attachement = new File(); |
---|
| 93 | this.attachement.memo = this.memo; |
---|
| 94 | this.attachement.name = Base64Coder.decodeString(attributes |
---|
| 95 | .getValue("name")); |
---|
| 96 | this.memo.getFiles().add(this.attachement); |
---|
| 97 | this.content = ""; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | @Override |
---|
| 102 | public void endElement(String uri, String localName, String qName) |
---|
| 103 | throws SAXException { |
---|
| 104 | |
---|
| 105 | if (qName.equals("category")) { |
---|
| 106 | this.category = null; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | if (qName.equals("memo")) { |
---|
| 110 | this.memo = null; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | if (qName.equals("categories")) { |
---|
| 114 | Iterator<Category> iterator = this.categories.iterator(); |
---|
| 115 | |
---|
| 116 | while (iterator.hasNext()) { |
---|
| 117 | Category current = iterator.next(); |
---|
| 118 | |
---|
| 119 | for (int i = 0; i < this.categories.size(); i++) { |
---|
| 120 | if (this.categories.elementAt(i).getiD().equals( |
---|
| 121 | current.getParentID())) |
---|
| 122 | current.setParentCategory(this.categories.elementAt(i)); |
---|
| 123 | else if (this.categories.elementAt(i).getParentID().equals( |
---|
| 124 | current.getiD())) |
---|
| 125 | current.getChildCategories().add( |
---|
| 126 | this.categories.elementAt(i)); |
---|
| 127 | } |
---|
| 128 | this.map.put(current.getiD(), current); |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | if (qName.equals("content") && this.content.length() > 0) { |
---|
| 133 | this.memo.setContent(Base64Coder.decodeString(this.content)); |
---|
| 134 | this.content = null; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | if (qName.equals("attachement") && this.attachement != null) { |
---|
| 138 | this.attachement.content = Base64Coder |
---|
| 139 | .decode(content.toCharArray()); |
---|
| 140 | this.attachement = null; |
---|
| 141 | this.content = null; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | @Override |
---|
| 146 | public void characters(char[] ch, int start, int length) |
---|
| 147 | throws SAXException { |
---|
| 148 | |
---|
| 149 | String data = (new String(ch)).substring(start, start + length).trim(); |
---|
| 150 | if (data.length() > 0) { |
---|
| 151 | if (this.category != null) |
---|
| 152 | this.category.setName(Base64Coder.decodeString(data)); |
---|
| 153 | |
---|
| 154 | if (this.memo != null) |
---|
| 155 | switch (this.state) { |
---|
| 156 | case TITLE: |
---|
| 157 | this.memo.setTitle(Base64Coder.decodeString(data)); |
---|
| 158 | break; |
---|
| 159 | case AUTHOR: |
---|
| 160 | this.memo.setAuthor(Base64Coder.decodeString(data)); |
---|
| 161 | break; |
---|
| 162 | case CONTENT: |
---|
| 163 | case ATTACHEMENT: |
---|
| 164 | this.content = this.content.concat(data); |
---|
| 165 | break; |
---|
| 166 | case TAGS: |
---|
| 167 | String[] tags = Base64Coder.decodeString(data).split("\0"); |
---|
| 168 | |
---|
| 169 | for (int x = 0; x < tags.length; x++) { |
---|
| 170 | Tag temp2 = new Tag(tags[x]); |
---|
| 171 | temp2.setMemo(this.memo); |
---|
| 172 | temp2.setMemoID(this.memo.getiD()); |
---|
| 173 | this.memo.getTags().add(temp2); |
---|
| 174 | } |
---|
| 175 | break; |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | @Override |
---|
| 181 | public void endDocument() throws SAXException { |
---|
| 182 | super.endDocument(); |
---|
| 183 | } |
---|
| 184 | } |
---|