001package com.hfg.xml.dublincore; 002 003 004import java.lang.reflect.Constructor; 005import java.util.HashMap; 006import java.util.List; 007import java.util.Map; 008 009import com.hfg.svg.SvgException; 010import com.hfg.util.StringUtil; 011import com.hfg.util.collection.CollectionUtil; 012import com.hfg.xml.XMLAttribute; 013import com.hfg.xml.XMLName; 014import com.hfg.xml.XMLNamespace; 015import com.hfg.xml.XMLTag; 016 017//------------------------------------------------------------------------------ 018/** 019 * Dublin Core base tag class. 020 * 021 * @author J. Alex Taylor, hairyfatguy.com 022 */ 023//------------------------------------------------------------------------------ 024// com.hfg XML/HTML Coding Library 025// 026// This library is free software; you can redistribute it and/or 027// modify it under the terms of the GNU Lesser General Public 028// License as published by the Free Software Foundation; either 029// version 2.1 of the License, or (at your option) any later version. 030// 031// This library is distributed in the hope that it will be useful, 032// but WITHOUT ANY WARRANTY; without even the implied warranty of 033// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 034// Lesser General Public License for more details. 035// 036// You should have received a copy of the GNU Lesser General Public 037// License along with this library; if not, write to the Free Software 038// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 039// 040// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 041// jataylor@hairyfatguy.com 042//------------------------------------------------------------------------------ 043 044public abstract class DublinCore extends XMLTag 045{ 046 public static final XMLNamespace DUBLIN_CORE_NAMESPACE = XMLNamespace.getNamespace("dc", "http://purl.org/dc/elements/1.1/"); 047 048 // Tag names 049 public static final XMLName FORMAT = new XMLName("format", DUBLIN_CORE_NAMESPACE); 050 public static final XMLName TITLE = new XMLName("title", DUBLIN_CORE_NAMESPACE); 051 public static final XMLName TYPE = new XMLName("type", DUBLIN_CORE_NAMESPACE); 052 053 private static Map<String, Class> sTagToClassMap = new HashMap<>(); 054 static 055 { 056 sTagToClassMap.put(FORMAT.getLocalName(), DublinCore_Format.class); 057 sTagToClassMap.put(TITLE.getLocalName(), DublinCore_Title.class); 058 sTagToClassMap.put(TYPE.getLocalName(), DublinCore_Type.class); 059 } 060 061 //########################################################################## 062 // CONSTRUCTORS 063 //########################################################################## 064 065 //--------------------------------------------------------------------------- 066 protected DublinCore(XMLName inTagName) 067 { 068 super(inTagName); 069 } 070 071 //--------------------------------------------------------------------------- 072 protected DublinCore(XMLName inTagName, XMLTag inXMLTag) 073 { 074 this(inTagName); 075 076 inXMLTag.verifyTagName(inTagName); 077 078 079 if (CollectionUtil.hasValues(inXMLTag.getAttributes())) 080 { 081 for (XMLAttribute attr : inXMLTag.getAttributes()) 082 { 083 setAttribute(attr.clone()); 084 } 085 } 086 087 List subtagsAndContent = inXMLTag.getContentPlusSubtagList(); 088 if (CollectionUtil.hasValues(subtagsAndContent)) 089 { 090 for (Object object : subtagsAndContent) 091 { 092 if (object instanceof XMLTag) 093 { 094 XMLTag subtag = (XMLTag) object; 095 if (subtag.getNamespace().equals(DublinCore.DUBLIN_CORE_NAMESPACE)) 096 { 097 addSubtag(DublinCore.constructFromXMLTag(subtag)); 098 } 099 else 100 { 101 addSubtag(subtag); 102 } 103 } 104 else 105 { 106 addContent((String)object); 107 } 108 } 109 } 110 111 } 112 113 //########################################################################## 114 // PUBLIC FUNCTIONS 115 //########################################################################## 116 117 //--------------------------------------------------------------------------- 118 public static XMLTag constructFromXMLTag(XMLTag inXMLTag) 119 { 120 Class clazz = sTagToClassMap.get(inXMLTag.getTagName()); 121 if (null == clazz) 122 { 123 throw new SvgException("The tag " + StringUtil.singleQuote(inXMLTag.getTagName()) + " could not be mapped to an Dublin Core class!"); 124 } 125 126 XMLTag node; 127 try 128 { 129 Constructor constructor = clazz.getConstructor(XMLTag.class); 130 131 node = (XMLTag) constructor.newInstance(inXMLTag); 132 } 133 catch (NoSuchMethodException e) 134 { 135 throw new SvgException("The class " + StringUtil.singleQuote(clazz) + " needs a constructor that takes an XMLTag.", e); 136 } 137 catch (Exception e) 138 { 139 throw new SvgException("Problem during invocation of class " + StringUtil.singleQuote(clazz) + "'s constructor!", e); 140 } 141 142 return node; 143 } 144 145}