001/*
002 *  $URL: svn://svn.webarts.bc.ca/open/trunk/projects/WebARTS/ca/bc/webarts/tools/musicbrainz/MusicbrainzArea.java $
003 *  $Author: tgutwin $
004 *  $Revision: 1288 $
005 *  $Date: 2018-07-09 21:24:57 -0700 (Mon, 09 Jul 2018) $
006 */
007/*
008 *
009 *  Written by Tom Gutwin - WebARTS Design.
010 *  Copyright (C) 2014-2016 WebARTS Design, North Vancouver Canada
011 *  http://www.webarts.ca
012 *
013 *  This program is free software; you can redistribute it and/or modify
014 *  it under the terms of the GNU General Public License as published by
015 *  the Free Software Foundation; version 3 of the License, or
016 *  (at your option) any later version.
017 *
018 *  This program is distributed in the hope that it will be useful,
019 *  but WITHOUT ANY WARRANTY; without_ even the implied warranty of
020 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
021 *  GNU General Public License for more details.
022 *
023 *  You should have received a copy of the GNU General Public License
024 *  along with this program; if not, write to the Free Software
025 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
026 */
027
028package ca.bc.webarts.tools.musicbrainz;
029
030import java.io.IOException;
031import java.io.File;
032import java.io.FileNotFoundException;
033import java.lang.Integer;
034import java.net.HttpURLConnection;
035import java.net.MalformedURLException;
036import java.net.URL;
037import java.net.URLEncoder;
038import java.util.Arrays;
039import java.util.Comparator;
040import java.util.Hashtable;
041import java.util.Set;
042import java.util.Vector;
043
044
045import ca.bc.webarts.tools.RestRequester;
046import ca.bc.webarts.widgets.Util;
047import ca.bc.webarts.widgets.Quick;
048
049import org.apache.commons.codec.binary.Base64;
050
051import nu.xom.Attribute;
052import nu.xom.Builder;
053import nu.xom.Document;
054import nu.xom.Element;
055import nu.xom.Elements;
056import nu.xom.Node;
057import nu.xom.ParsingException;
058import nu.xom.ValidityException;
059import nu.xom.Serializer;
060import nu.xom.XPathException;
061
062/**
063  *
064  *
065  * Musicbrainz Area/region Class Object<br>
066  * ~~~~~~~~~~~~~~~~
067  * representing the returned XML from the mb api query
068  *<pre>
069      <?xml version="1.0" encoding="UTF-8"?>
070
071   </pre>
072  *
073  **/
074public class MusicbrainzArea implements Comparable<MusicbrainzArea>
075{
076  String name = "";
077  String id = "";
078  String sortName = "";
079  String iso31661code = "";
080
081  public MusicbrainzArea()
082  {
083  }
084
085
086  public MusicbrainzArea(String areaName)
087  {
088    name = areaName;
089    sortName = name;
090  }
091
092
093  public MusicbrainzArea(nu.xom.Element areaElem )
094  {
095    try
096    {
097      // parse the area info from the Element
098      if (areaElem!=null )
099      {
100        if(areaElem.getLocalName().equalsIgnoreCase("area") )
101        {
102            id=areaElem.getAttributeValue("id");
103
104            nu.xom.Elements areaSubElems = areaElem.getChildElements();
105            if (areaSubElems!=null )
106            {
107              nu.xom.Element currElem = null;
108              for (int ii=0; ii< areaSubElems.size(); ii++)
109              {
110                currElem = areaSubElems.get(ii);
111                if(currElem.getLocalName().equalsIgnoreCase("sort-name") ) sortName = currElem.getValue();
112                if(currElem.getLocalName().equalsIgnoreCase("name") ) name = currElem.getValue();
113              }
114            }
115        }
116        else
117          System.out.print(" !!! "+areaElem.getLocalName());
118      }
119    }
120    catch (Exception ex)
121    {
122      ex.printStackTrace();
123      System.out.println("\nParsing error - Area raw results:");
124      System.out.println(areaElem);
125    }
126  }
127
128
129  public String toString(){return name;}
130
131
132public String getPropsString()
133{
134  String p = "MusicbrainzArea_";
135  String retVal = "#MusicBrainz Area Properties\n";
136  retVal += p+"name="+name+"\n";
137  retVal += p+"sortName="+getSortName()+"\n";
138  retVal += p+"id="+id+"\n";
139  retVal += p+"iso31661code="+iso31661code+"\n";
140
141  return retVal;
142}
143
144
145
146/**
147  * Set Method for class field 'sortName'.
148  *
149  * @param sortName is the value to set this class field to.
150  *
151  **/
152public  void setSortName(String sortName)
153{
154  this.sortName = sortName;
155}  // setSortName Method
156
157
158/**
159  * Get Method for class field 'sortName'.
160  *
161  * @return String - The value the class field 'sortName'.
162  *
163  **/
164public String getSortName()
165{
166  return sortName;
167}  // getSortName Method
168
169
170  /** Comparator for ignore case sort. **/
171  public int compareToIgnoreCase(MusicbrainzArea other)
172  {
173    return this.getSortName().compareToIgnoreCase(other.getSortName());
174  }
175
176
177  /** implements Comparator. **/
178  @Override public int compareTo(MusicbrainzArea other)
179  {
180    return this.getSortName().compareTo(other.getSortName());
181  }
182
183
184  /** A Comparator that can be used to sort Artist vectors. **/
185  public static Comparator <MusicbrainzArea> ArtistComparator = new Comparator<MusicbrainzArea>()
186    {
187      @Override public int compare(MusicbrainzArea one, MusicbrainzArea two)
188      {
189        return one.getSortName().compareTo(two.getSortName());
190      }
191    };
192
193
194  /** A case in-sensitive Comparator that can be used to sort MusicbrainzArtist vectors. **/
195  public static Comparator <MusicbrainzArea> AreaComparatorIgnoreCase = new Comparator<MusicbrainzArea>()
196    {
197      @Override public int compare(MusicbrainzArea one, MusicbrainzArea two)
198      {
199        //return one.name(true).compareTo(two.name(true));
200        return one.getSortName().compareToIgnoreCase(two.getSortName());
201      }
202    };
203}