001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org . |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013:
014: package org.rapla.storage.xml;
015:
016: import java.util.Stack;
017:
018: import org.rapla.components.util.Assert;
019: import org.rapla.entities.Annotatable;
020: import org.rapla.entities.Category;
021: import org.rapla.entities.IllegalAnnotationException;
022: import org.rapla.entities.MultiLanguageName;
023: import org.rapla.entities.internal.CategoryImpl;
024: import org.rapla.framework.RaplaContext;
025: import org.rapla.framework.RaplaException;
026: import org.xml.sax.Attributes;
027: import org.xml.sax.SAXException;
028:
029: public class CategoryReader extends RaplaXMLReader {
030: MultiLanguageName currentName = null;
031: Annotatable currentAnnotatable = null;
032: String currentLang = null;
033: Stack categoryStack = new Stack();
034: CategoryImpl super Category;
035: String annotationKey = null;
036: CategoryImpl lastProcessedCategory = null;
037: boolean readOnlyThisCategory;
038:
039: public CategoryReader(RaplaContext context) throws RaplaException {
040: super (context);
041: super Category = getSuperCategory();
042: currentName = super Category.getName();
043:
044: }
045:
046: public void setReadOnlyThisCategory(boolean enable) {
047: readOnlyThisCategory = enable;
048: }
049:
050: public void processElement(String namespaceURI, String localName,
051: String qName, Attributes atts) throws SAXException {
052: if (localName.equals("category")
053: && namespaceURI.equals(RAPLA_NS)) {
054: String key = atts.getValue("key");
055: Assert.notNull(key);
056: CategoryImpl category = new CategoryImpl();
057: category.setKey(key);
058: currentName = category.getName();
059: currentAnnotatable = category;
060: setVersionIfThere(category, atts);
061: if (atts.getValue("id") != null) {
062: setId(category, atts);
063: } else {
064: setNewId(category);
065: }
066:
067: if (!readOnlyThisCategory) {
068: if (!categoryStack.empty()) {
069: Category parent = (Category) categoryStack.peek();
070: parent.addCategory(category);
071: } else {
072: String parentId = atts.getValue("parentid");
073: if (parentId != null) {
074: Object parentIdN = getId(Category.TYPE,
075: parentId);
076: category.getReferenceHandler().putId("parent",
077: parentIdN);
078: } else {
079: if (atts.getValue("id") == null) {
080: super Category.addCategory(category);
081: } else {
082: // It is the super categorycategory.getReferenceHandler().put("parent", superCategory);
083: }
084: }
085: }
086:
087: }
088: categoryStack.push(category);
089:
090: /*
091: Category test = category;
092: String output = "";
093: while (test != null)
094: {
095: output = "/" + test.getKey() + output;
096: test = test.getParent();
097: }
098: // System.out.println("Storing category " + output );
099: */
100: }
101:
102: if (localName.equals("name")
103: && namespaceURI.equals(ANNOTATION_NS)) {
104: startContent();
105: currentLang = atts.getValue("lang");
106: Assert.notNull(currentLang);
107: }
108:
109: if (localName.equals("annotation")
110: && namespaceURI.equals(RAPLA_NS)) {
111: annotationKey = atts.getValue("key");
112: Assert.notNull(annotationKey,
113: "key attribute cannot be null");
114: startContent();
115: }
116: }
117:
118: public void processEnd(String namespaceURI, String localName,
119: String qName) throws SAXException {
120: if (localName.equals("category")) {
121: // Test Namespace uris here for possible xerces bug
122: if (namespaceURI.equals("")) {
123: throw createSAXParseException(" category namespace empty. Possible Xerces Bug. Download a newer version of xerces.");
124: }
125:
126: CategoryImpl category = (CategoryImpl) categoryStack.pop();
127: if (!readOnlyThisCategory) {
128: add(category);
129: }
130: lastProcessedCategory = category;
131: } else if (localName.equals("name")
132: && namespaceURI.equals(ANNOTATION_NS)) {
133: String translation = readContent();
134: currentName.setName(currentLang, translation);
135: } else if (localName.equals("annotation")
136: && namespaceURI.equals(RAPLA_NS)) {
137: try {
138: currentAnnotatable.setAnnotation(annotationKey,
139: readContent());
140: } catch (IllegalAnnotationException ex) {
141: }
142: }
143: }
144:
145: public CategoryImpl getCurrentCategory() {
146: return lastProcessedCategory;
147: }
148:
149: }
|