📄 categoryreader.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org . |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.storage.xml;
import java.util.Stack;
import org.rapla.components.util.Assert;
import org.rapla.entities.Annotatable;
import org.rapla.entities.Category;
import org.rapla.entities.IllegalAnnotationException;
import org.rapla.entities.MultiLanguageName;
import org.rapla.entities.internal.CategoryImpl;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
public class CategoryReader extends RaplaXMLReader
{
MultiLanguageName currentName = null;
Annotatable currentAnnotatable = null;
String currentLang = null;
Stack categoryStack = new Stack();
CategoryImpl superCategory;
String annotationKey = null;
CategoryImpl lastProcessedCategory = null;
boolean readOnlyThisCategory;
public CategoryReader( RaplaContext context ) throws RaplaException
{
super( context );
superCategory = getSuperCategory();
currentName = superCategory.getName();
}
public void setReadOnlyThisCategory( boolean enable )
{
readOnlyThisCategory = enable;
}
public void processElement(
String namespaceURI,
String localName,
String qName,
Attributes atts ) throws SAXException
{
if (localName.equals( "category" ) && namespaceURI.equals( RAPLA_NS ))
{
String key = atts.getValue( "key" );
Assert.notNull( key );
CategoryImpl category = new CategoryImpl();
category.setKey( key );
currentName = category.getName();
currentAnnotatable = category;
setVersionIfThere( category, atts);
if (atts.getValue( "id" )!=null)
{
setId( category, atts );
}
else
{
setNewId( category );
}
if (!readOnlyThisCategory)
{
if ( !categoryStack.empty() )
{
Category parent = (Category) categoryStack.peek();
parent.addCategory( category);
}
else
{
String parentId = atts.getValue( "parentid");
if ( parentId!= null)
{
Object parentIdN = getId( Category.TYPE, parentId);
category.getReferenceHandler().putId("parent", parentIdN);
}
else
{
if (atts.getValue( "id" )==null)
{
superCategory.addCategory( category);
}
else
{
// It is the super categorycategory.getReferenceHandler().put("parent", superCategory);
}
}
}
}
categoryStack.push( category );
/*
Category test = category;
String output = "";
while (test != null)
{
output = "/" + test.getKey() + output;
test = test.getParent();
}
// System.out.println("Storing category " + output );
*/
}
if (localName.equals( "name" ) && namespaceURI.equals( ANNOTATION_NS ))
{
startContent();
currentLang = atts.getValue( "lang" );
Assert.notNull( currentLang );
}
if (localName.equals( "annotation" ) && namespaceURI.equals( RAPLA_NS ))
{
annotationKey = atts.getValue( "key" );
Assert.notNull( annotationKey, "key attribute cannot be null" );
startContent();
}
}
public void processEnd( String namespaceURI, String localName, String qName )
throws SAXException
{
if (localName.equals( "category" ))
{
// Test Namespace uris here for possible xerces bug
if (namespaceURI.equals( "" ))
{
throw createSAXParseException( " category namespace empty. Possible Xerces Bug. Download a newer version of xerces." );
}
CategoryImpl category = (CategoryImpl) categoryStack.pop();
if (!readOnlyThisCategory)
{
add( category );
}
lastProcessedCategory = category;
}
else if (localName.equals( "name" ) && namespaceURI.equals( ANNOTATION_NS ))
{
String translation = readContent();
currentName.setName( currentLang, translation );
}
else if (localName.equals( "annotation" ) && namespaceURI.equals( RAPLA_NS ))
{
try
{
currentAnnotatable.setAnnotation( annotationKey, readContent() );
}
catch (IllegalAnnotationException ex)
{
}
}
}
public CategoryImpl getCurrentCategory()
{
return lastProcessedCategory;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -