forked from I2P_Developers/i2p.i2p
* Moved PetName and PetNameDB to core.
* Implement PetNameNamingService.
This commit is contained in:
@@ -4,6 +4,8 @@ import java.io.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.client.naming.PetName;
|
||||
import net.i2p.client.naming.PetNameDB;
|
||||
import net.i2p.data.*;
|
||||
import net.i2p.syndie.data.*;
|
||||
import net.i2p.syndie.sml.*;
|
||||
|
@@ -1,172 +0,0 @@
|
||||
package net.i2p.syndie;
|
||||
|
||||
import java.util.*;
|
||||
import net.i2p.data.DataHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class PetName {
|
||||
private String _name;
|
||||
private String _network;
|
||||
private String _protocol;
|
||||
private List _groups;
|
||||
private boolean _isPublic;
|
||||
private String _location;
|
||||
|
||||
public PetName() {
|
||||
this(null, null, null, null);
|
||||
}
|
||||
public PetName(String name, String network, String protocol, String location) {
|
||||
_name = name;
|
||||
_network = network;
|
||||
_protocol = protocol;
|
||||
_location = location;
|
||||
_groups = new ArrayList();
|
||||
_isPublic = false;
|
||||
}
|
||||
/**
|
||||
* @param dbLine name:network:protocol:isPublic:group1,group2,group3:location
|
||||
*/
|
||||
public PetName(String dbLine) {
|
||||
_groups = new ArrayList();
|
||||
StringTokenizer tok = new StringTokenizer(dbLine, ":\n", true);
|
||||
int tokens = tok.countTokens();
|
||||
System.out.println("Tokens: " + tokens);
|
||||
if (tokens < 7) {
|
||||
return;
|
||||
}
|
||||
String s = tok.nextToken();
|
||||
if (":".equals(s)) {
|
||||
_name = null;
|
||||
} else {
|
||||
_name = s;
|
||||
s = tok.nextToken(); // skip past the :
|
||||
}
|
||||
s = tok.nextToken();
|
||||
if (":".equals(s)) {
|
||||
_network = null;
|
||||
} else {
|
||||
_network = s;
|
||||
s = tok.nextToken(); // skip past the :
|
||||
}
|
||||
s = tok.nextToken();
|
||||
if (":".equals(s)) {
|
||||
_protocol = null;
|
||||
} else {
|
||||
_protocol = s;
|
||||
s = tok.nextToken(); // skip past the :
|
||||
}
|
||||
s = tok.nextToken();
|
||||
if (":".equals(s)) {
|
||||
_isPublic = false;
|
||||
} else {
|
||||
if ("true".equals(s))
|
||||
_isPublic = true;
|
||||
else
|
||||
_isPublic = false;
|
||||
s = tok.nextToken(); // skip past the :
|
||||
}
|
||||
s = tok.nextToken();
|
||||
if (":".equals(s)) {
|
||||
// noop
|
||||
} else {
|
||||
StringTokenizer gtok = new StringTokenizer(s, ",");
|
||||
while (gtok.hasMoreTokens())
|
||||
_groups.add(gtok.nextToken().trim());
|
||||
s = tok.nextToken(); // skip past the :
|
||||
}
|
||||
while (tok.hasMoreTokens()) {
|
||||
if (_location == null)
|
||||
_location = tok.nextToken();
|
||||
else
|
||||
_location = _location + tok.nextToken();
|
||||
}
|
||||
}
|
||||
|
||||
public String getName() { return _name; }
|
||||
public String getNetwork() { return _network; }
|
||||
public String getProtocol() { return _protocol; }
|
||||
public String getLocation() { return _location; }
|
||||
public boolean getIsPublic() { return _isPublic; }
|
||||
public int getGroupCount() { return _groups.size(); }
|
||||
public String getGroup(int i) { return (String)_groups.get(i); }
|
||||
|
||||
public void setName(String name) { _name = name; }
|
||||
public void setNetwork(String network) { _network = network; }
|
||||
public void setProtocol(String protocol) { _protocol = protocol; }
|
||||
public void setLocation(String location) { _location = location; }
|
||||
public void setIsPublic(boolean pub) { _isPublic = pub; }
|
||||
public void addGroup(String name) {
|
||||
if ( (name != null) && (name.length() > 0) && (!_groups.contains(name)) )
|
||||
_groups.add(name);
|
||||
}
|
||||
public void removeGroup(String name) { _groups.remove(name); }
|
||||
public void setGroups(String groups) {
|
||||
if (groups != null) {
|
||||
_groups.clear();
|
||||
StringTokenizer tok = new StringTokenizer(groups, ", \t");
|
||||
while (tok.hasMoreTokens())
|
||||
addGroup(tok.nextToken().trim());
|
||||
} else {
|
||||
_groups.clear();
|
||||
}
|
||||
}
|
||||
public boolean isMember(String group) {
|
||||
for (int i = 0; i < getGroupCount(); i++)
|
||||
if (getGroup(i).equals(group))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer buf = new StringBuffer(256);
|
||||
if (_name != null) buf.append(_name.trim());
|
||||
buf.append(':');
|
||||
if (_network != null) buf.append(_network.trim());
|
||||
buf.append(':');
|
||||
if (_protocol != null) buf.append(_protocol.trim());
|
||||
buf.append(':').append(_isPublic).append(':');
|
||||
if (_groups != null) {
|
||||
for (int i = 0; i < _groups.size(); i++) {
|
||||
buf.append(((String)_groups.get(i)).trim());
|
||||
if (i + 1 < _groups.size())
|
||||
buf.append(',');
|
||||
}
|
||||
}
|
||||
buf.append(':');
|
||||
if (_location != null) buf.append(_location.trim());
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if ( (obj == null) || !(obj instanceof PetName) ) return false;
|
||||
PetName pn = (PetName)obj;
|
||||
return DataHelper.eq(_name, pn._name) &&
|
||||
DataHelper.eq(_location, pn._location) &&
|
||||
DataHelper.eq(_network, pn._network) &&
|
||||
DataHelper.eq(_protocol, pn._protocol);
|
||||
}
|
||||
public int hashCode() {
|
||||
int rv = 0;
|
||||
rv += DataHelper.hashCode(_name);
|
||||
rv += DataHelper.hashCode(_location);
|
||||
rv += DataHelper.hashCode(_network);
|
||||
rv += DataHelper.hashCode(_protocol);
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
test("a:b:c:true:e:f");
|
||||
test("a:::true::d");
|
||||
test("a:::true::");
|
||||
test("a:b::true::");
|
||||
test(":::trye::");
|
||||
test("a:b:c:true:e:http://foo.bar");
|
||||
}
|
||||
private static void test(String line) {
|
||||
PetName pn = new PetName(line);
|
||||
String val = pn.toString();
|
||||
System.out.println("OK? " + val.equals(line) + ": " + line + " [" + val + "]");
|
||||
}
|
||||
}
|
@@ -1,74 +0,0 @@
|
||||
package net.i2p.syndie;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class PetNameDB {
|
||||
/** name (String) to PetName mapping */
|
||||
private Map _names;
|
||||
|
||||
public PetNameDB() {
|
||||
_names = Collections.synchronizedMap(new HashMap());
|
||||
}
|
||||
|
||||
public PetName get(String name) { return (PetName)_names.get(name); }
|
||||
public boolean exists(String name) { return _names.containsKey(name); }
|
||||
public void set(String name, PetName pn) { _names.put(name, pn); }
|
||||
public void remove(String name) { _names.remove(name); }
|
||||
public Set getNames() { return new HashSet(_names.keySet()); }
|
||||
public List getGroups() {
|
||||
List rv = new ArrayList();
|
||||
for (Iterator iter = new HashSet(_names.values()).iterator(); iter.hasNext(); ) {
|
||||
PetName name = (PetName)iter.next();
|
||||
for (int i = 0; i < name.getGroupCount(); i++)
|
||||
if (!rv.contains(name.getGroup(i)))
|
||||
rv.add(name.getGroup(i));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
public String getNameByLocation(String location) {
|
||||
if (location == null) return null;
|
||||
synchronized (_names) {
|
||||
for (Iterator iter = _names.values().iterator(); iter.hasNext(); ) {
|
||||
PetName name = (PetName)iter.next();
|
||||
if ( (name.getLocation() != null) && (name.getLocation().trim().equals(location.trim())) )
|
||||
return name.getName();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void load(String location) throws IOException {
|
||||
File f = new File(location);
|
||||
if (!f.exists()) return;
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
in = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
|
||||
String line = null;
|
||||
while ( (line = in.readLine()) != null) {
|
||||
PetName name = new PetName(line);
|
||||
if (name.getName() != null)
|
||||
_names.put(name.getName(), name);
|
||||
}
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
public void store(String location) throws IOException {
|
||||
Writer out = null;
|
||||
try {
|
||||
out = new OutputStreamWriter(new FileOutputStream(location), "UTF-8");
|
||||
for (Iterator names = getNames().iterator(); names.hasNext(); ) {
|
||||
PetName name = get((String)names.next());
|
||||
if (name != null)
|
||||
out.write(name.toString() + "\n");
|
||||
}
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
}
|
@@ -4,6 +4,7 @@ import java.io.UnsupportedEncodingException;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.client.naming.PetNameDB;
|
||||
import net.i2p.data.*;
|
||||
|
||||
/**
|
||||
|
@@ -4,6 +4,7 @@ import java.io.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.client.naming.PetName;
|
||||
import net.i2p.data.*;
|
||||
import net.i2p.syndie.*;
|
||||
import net.i2p.syndie.data.*;
|
||||
|
@@ -4,6 +4,8 @@ import java.io.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.client.naming.PetName;
|
||||
import net.i2p.client.naming.PetNameDB;
|
||||
import net.i2p.data.*;
|
||||
import net.i2p.syndie.*;
|
||||
import net.i2p.syndie.data.*;
|
||||
|
@@ -3,6 +3,7 @@ package net.i2p.syndie.web;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.client.naming.PetName;
|
||||
import net.i2p.syndie.*;
|
||||
import net.i2p.syndie.data.BlogURI;
|
||||
import net.i2p.syndie.sml.HTMLPreviewRenderer;
|
||||
|
@@ -5,6 +5,7 @@ import java.text.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import net.i2p.I2PAppContext;
|
||||
import net.i2p.client.naming.PetNameDB;
|
||||
import net.i2p.data.*;
|
||||
import net.i2p.util.EepGet;
|
||||
import net.i2p.util.EepGetScheduler;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<%@page contentType="text/html; charset=UTF-8" import="net.i2p.syndie.web.ArchiveViewerBean, net.i2p.syndie.*" %>
|
||||
<%@page contentType="text/html; charset=UTF-8" import="net.i2p.syndie.web.ArchiveViewerBean, net.i2p.syndie.*, net.i2p.client.naming.PetName" %>
|
||||
<% request.setCharacterEncoding("UTF-8"); %>
|
||||
<jsp:useBean scope="session" class="net.i2p.syndie.User" id="user" /><%
|
||||
if (user.getAuthenticated() && (null != request.getParameter("action")) ) {
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="net.i2p.data.Base64, net.i2p.syndie.web.*, net.i2p.syndie.sml.*, net.i2p.syndie.data.*, net.i2p.syndie.*, org.mortbay.servlet.MultiPartRequest, java.util.*, java.io.*" %><%
|
||||
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="net.i2p.data.Base64, net.i2p.syndie.web.*, net.i2p.syndie.sml.*, net.i2p.syndie.data.*, net.i2p.syndie.*, net.i2p.client.naming.PetName, net.i2p.client.naming.PetNameDB, org.mortbay.servlet.MultiPartRequest, java.util.*, java.io.*" %><%
|
||||
request.setCharacterEncoding("UTF-8"); %><jsp:useBean scope="session" class="net.i2p.syndie.User" id="user"
|
||||
/><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
|
||||
<html>
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="net.i2p.data.Base64, net.i2p.syndie.web.*, net.i2p.syndie.sml.*, net.i2p.syndie.data.*, net.i2p.syndie.*, org.mortbay.servlet.MultiPartRequest, java.util.*" %><%
|
||||
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="net.i2p.data.Base64, net.i2p.client.naming.PetName, net.i2p.client.naming.PetNameDB, net.i2p.syndie.web.*, net.i2p.syndie.sml.*, net.i2p.syndie.data.*, net.i2p.syndie.*, org.mortbay.servlet.MultiPartRequest, java.util.*" %><%
|
||||
request.setCharacterEncoding("UTF-8");
|
||||
%><jsp:useBean scope="session" class="net.i2p.syndie.User" id="user"
|
||||
/><jsp:useBean scope="session" class="net.i2p.syndie.web.PostBean" id="post"
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="net.i2p.syndie.web.*, net.i2p.syndie.*, net.i2p.syndie.sml.*, java.util.*" %><%
|
||||
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="net.i2p.client.naming.PetName, net.i2p.syndie.web.*, net.i2p.syndie.*, net.i2p.syndie.sml.*, java.util.*" %><%
|
||||
request.setCharacterEncoding("UTF-8");
|
||||
%><jsp:useBean scope="session" class="net.i2p.syndie.web.RemoteArchiveBean" id="remote"
|
||||
/><jsp:useBean scope="session" class="net.i2p.syndie.User" id="user"
|
||||
|
Reference in New Issue
Block a user