All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.xml.XMLNamespace Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml;

import com.hfg.exception.ProgrammingException;
import com.hfg.util.StringUtil;

import java.util.HashMap;
import java.util.Map;

//------------------------------------------------------------------------------
/**
  Class to encapsulate an XML namespace.
  
  EX:  <name xmlns:foo="http://bar.com/foo"/>
     The prefix is 'foo' and the URI is 'http://bar.com/foo'. 
  
To conserve object space, XMLNamespace objects cannot be constructed directly but are rather constructed through the static factory getNamespace() method.
@author J. Alex Taylor, hairyfatguy.com */ //------------------------------------------------------------------------------ public class XMLNamespace implements Cloneable { //########################################################################### // PRIVATE FIELDS //########################################################################### private String mPrefix; private String mURI; private boolean mIsDefault; private static Map sURIMap = new HashMap<>(50); private static Map sPrefixMap = new HashMap<>(50); //########################################################################### // PUBLIC FIELDS //########################################################################### public static XMLNamespace XHTML = new XMLNamespace("xhtml", "http://www.w3.org/1999/xhtml"); public static XMLNamespace SVG = new XMLNamespace("svg", "http://www.w3.org/2000/svg"); public static XMLNamespace XLINK = new XMLNamespace("xlink", "http://www.w3.org/1999/xlink"); //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- private XMLNamespace(String inURI) { mURI = inURI; } //--------------------------------------------------------------------------- private XMLNamespace(String inPrefix, String inURI) { mPrefix = inPrefix; mURI = inURI; sURIMap.put(inURI, this); if (StringUtil.isSet(inPrefix)) sPrefixMap.put(inPrefix, this); } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- public static XMLNamespace getNamespace(String inURI) { return getNamespace(null, inURI); } //--------------------------------------------------------------------------- public XMLNamespace setIsDefault(boolean inValue) { mIsDefault = inValue; return this; } //--------------------------------------------------------------------------- public boolean isDefault() { return mIsDefault; } //--------------------------------------------------------------------------- @Override public int hashCode() { return getURI().hashCode(); } //--------------------------------------------------------------------------- @Override public boolean equals(Object inObj2) { boolean result = false; if (inObj2 instanceof XMLNamespace) { XMLNamespace namespace2 = (XMLNamespace) inObj2; result = (getURI() != null ? getURI().equals(namespace2.getURI()) : null == namespace2.getURI()); } return result; } //--------------------------------------------------------------------------- public XMLNamespace clone() { XMLNamespace cloneObj; try { cloneObj = (XMLNamespace) super.clone(); } catch (CloneNotSupportedException e) { throw new ProgrammingException("Error cloning XMLNamespace object!", e); } return cloneObj; } //--------------------------------------------------------------------------- public static XMLNamespace getNamespace(String inPrefix, String inURI) { XMLNamespace namespace; if (StringUtil.isSet(inURI)) { namespace = sURIMap.get(inURI); if (namespace != null && null == namespace.getPrefix() && StringUtil.isSet(inPrefix)) { namespace.setPrefix(inPrefix); sPrefixMap.put(inPrefix, namespace); } } else { namespace = sPrefixMap.get(inPrefix); } if (null == namespace) { namespace = new XMLNamespace(inPrefix, inURI); } return namespace; } //--------------------------------------------------------------------------- public static XMLNamespace getNamespaceViaPrefix(String inPrefix) { XMLNamespace namespace = sPrefixMap.get(inPrefix); if (null == namespace) { namespace = new XMLNamespace(inPrefix, null); sPrefixMap.put(inPrefix, namespace); } return namespace; } //--------------------------------------------------------------------------- public String getPrefix() { return mPrefix; } //--------------------------------------------------------------------------- public String getURI() { return mURI; } //--------------------------------------------------------------------------- public void setURI(String inValue) { sURIMap.remove(getURI()); mURI = inValue; sURIMap.put(inValue, this); } //--------------------------------------------------------------------------- public String toString() { return getPrefix(); } //--------------------------------------------------------------------------- private void setPrefix(String inValue) { mPrefix = inValue; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy