001 /**
002 *
003 * $Revision: 15496 $ $Date: 2009-07-10 16:28:14 +0200 (Fri, 10 Jul 2009) $
004 *
005 * This file is part of ** M y C o R e **
006 * Visit our homepage at http://www.mycore.de/ for details.
007 *
008 * This program is free software; you can use it, redistribute it
009 * and / or modify it under the terms of the GNU General Public License
010 * (GPL) as published by the Free Software Foundation; either version 2
011 * of the License or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program, normally in the file license.txt.
020 * If not, write to the Free Software Foundation Inc.,
021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
022 *
023 **/
024 package org.mycore.datamodel.classifications2.utils;
025
026 import java.net.URI;
027 import java.net.URISyntaxException;
028 import java.util.ArrayList;
029 import java.util.Collection;
030 import java.util.Collections;
031 import java.util.HashSet;
032 import java.util.List;
033
034 import org.jdom.Document;
035 import org.jdom.Element;
036 import org.jdom.Namespace;
037
038 import static org.mycore.common.MCRConstants.XLINK_NAMESPACE;
039 import org.mycore.datamodel.classifications2.MCRCategory;
040 import org.mycore.datamodel.classifications2.MCRCategoryID;
041 import org.mycore.datamodel.classifications2.MCRLabel;
042 import org.mycore.datamodel.classifications2.impl.MCRCategoryImpl;
043
044 public class MCRXMLTransformer {
045
046 public static MCRCategory getCategory(Document xml) throws URISyntaxException {
047 MCRCategoryImpl category = new MCRCategoryImpl();
048 category.setRoot(category);
049 final String classID = xml.getRootElement().getAttributeValue("ID");
050 category.setLevel(0);
051 category.setId(MCRCategoryID.rootID(classID));
052 //setChildren has to be called before setParent (below) can be called without
053 //database access see: org.mycore.datamodel.classifications2.impl.MCRAbstractCategoryImpl.getChildren()
054 category.setChildren(new ArrayList<MCRCategory>());
055 buildChildCategories(classID, xml.getRootElement().getChild("categories").getChildren("category"), category);
056 category.setLabels(getLabel(xml.getRootElement().getChildren("label")));
057 return category;
058 }
059
060 public static MCRCategory buildCategory(String classID, Element e, MCRCategory parent) throws URISyntaxException {
061 MCRCategoryImpl category = new MCRCategoryImpl();
062 //setId must be called before setParent (info important)
063 category.setId(new MCRCategoryID(classID, e.getAttributeValue("ID")));
064 category.setRoot(parent.getRoot());
065 category.setChildren(new ArrayList<MCRCategory>());
066 category.setParent(parent);
067 category.setLabels(getLabel(e.getChildren("label")));
068 category.setLevel(parent.getLevel()+1);
069 if (e.getChild("url") != null) {
070 final String uri = e.getChild("url").getAttributeValue("href", XLINK_NAMESPACE);
071 if (uri != null)
072 category.setURI(new URI(uri));
073 }
074 buildChildCategories(classID, e.getChildren("category"), category);
075 return category;
076 }
077
078 @SuppressWarnings("unchecked")
079 private static List<MCRCategory> buildChildCategories(String classID, List elements, MCRCategory parent) throws URISyntaxException {
080 List<MCRCategory> children = new ArrayList<MCRCategory>(elements.size());
081 for (Object o : elements) {
082 children.add(buildCategory(classID, (Element) o, parent));
083 }
084 return children;
085 }
086
087 @SuppressWarnings("unchecked")
088 private static Collection<MCRLabel> getLabel(List elements) {
089 Collection<MCRLabel> labels = new HashSet<MCRLabel>(elements.size(), 1l);
090 for (Object o : elements) {
091 Element e = (Element) o;
092 String lang = e.getAttributeValue("lang", Namespace.XML_NAMESPACE);
093 labels.add(new MCRLabel(lang, e.getAttributeValue("text"), e.getAttributeValue("description")));
094 }
095 return labels;
096 }
097
098 }