98 lines
3.0 KiB
Java
98 lines
3.0 KiB
Java
/**
|
|
* Copyright (C) 2011-2013 Nicolas Vinot <aeris@imirhil.fr>
|
|
* Copyright (C) 2017 Christian Pierre MOMON <cmomon@april.org>
|
|
*
|
|
* This file is part of (April) Hebdobot.
|
|
*
|
|
* Hebdobot is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Hebdobot is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with Hebdobot. If not, see <http://www.gnu.org/licenses/>
|
|
*/
|
|
package org.april.hebdobot.xml;
|
|
|
|
import java.io.InputStream;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
|
|
import javax.xml.XMLConstants;
|
|
import javax.xml.bind.JAXBContext;
|
|
import javax.xml.bind.JAXBException;
|
|
import javax.xml.bind.Unmarshaller;
|
|
import javax.xml.transform.stream.StreamSource;
|
|
import javax.xml.validation.SchemaFactory;
|
|
|
|
import org.jibble.pircbot.User;
|
|
import org.xml.sax.SAXException;
|
|
|
|
/**
|
|
* The Class UserAlias.
|
|
*/
|
|
public class UserAlias
|
|
{
|
|
private final Map<String, String> aliases = new HashMap<String, String>();
|
|
|
|
/**
|
|
* Instantiates a new user alias.
|
|
*
|
|
* @param source
|
|
* the source
|
|
*/
|
|
public UserAlias(final InputStream source)
|
|
{
|
|
try
|
|
{
|
|
final Unmarshaller unmarshaller = JAXBContext.newInstance(Users.class).createUnmarshaller();
|
|
unmarshaller.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
|
|
.newSchema(UserAlias.class.getResource("/org/april/hebdobot/users.xsd")));
|
|
|
|
for (final User user : unmarshaller.unmarshal(new StreamSource(source), Users.class).getValue().getUser())
|
|
{
|
|
final String realName = user.getRealName();
|
|
for (final String nick : user.getNick())
|
|
{
|
|
this.aliases.put(nick, realName);
|
|
}
|
|
}
|
|
}
|
|
catch (final SAXException exception)
|
|
{
|
|
exception.printStackTrace();
|
|
throw new RuntimeException(exception);
|
|
}
|
|
catch (final JAXBException exception)
|
|
{
|
|
exception.printStackTrace();
|
|
throw new RuntimeException(exception);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets the real name.
|
|
*
|
|
* @param nick
|
|
* the nick
|
|
* @return the real name
|
|
*/
|
|
public String getRealName(final String nick)
|
|
{
|
|
for (final Entry<String, String> entry : this.aliases.entrySet())
|
|
{
|
|
if (nick.toLowerCase().contains(entry.getKey().toLowerCase()))
|
|
{
|
|
return entry.getValue() + " ( " + nick + " )";
|
|
}
|
|
}
|
|
return nick;
|
|
}
|
|
}
|