package hozo.sparql; import java.beans.XMLDecoder; import java.beans.XMLEncoder; import java.io.InputStream; import java.io.OutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; public class EndpointSettings implements Serializable { /** * */ private static final long serialVersionUID = -6928422841511501314L; public static final int RESULT_TYPE_XML = 0; public static final int RESULT_TYPE_JSON = 1; public static final int RESULT_TYPE_SSE = 2; public static final int RESULT_TYPE_TSV = 3; public static final String RESULT_TYPE_XML_STR = "XML/RDF"; public static final String RESULT_TYPE_JSON_STR = "JSON"; public static final String RESULT_TYPE_SSE_STR = "SPARQL Syntax Expressions"; public static final String RESULT_TYPE_TSV_STR = "TSV"; private static HashMap dataTypeMap; private String endpoint; private boolean useCustomParam = false; private String queryKey = "q"; private String option = "type=json&LIMIT=100"; private String encoding = "UTF-8"; private String namespaces = ""; private int resultType = EndpointSettings.RESULT_TYPE_JSON; private boolean editable = false; private String repositoryURL; private String repository; private String user; private String pass; private boolean isTarget = true; private volatile boolean isChanged = false; static { dataTypeMap = new LinkedHashMap(); dataTypeMap.put(RESULT_TYPE_XML_STR, new Integer(RESULT_TYPE_XML)); dataTypeMap.put(RESULT_TYPE_JSON_STR, new Integer(RESULT_TYPE_JSON)); dataTypeMap.put(RESULT_TYPE_SSE_STR, new Integer(RESULT_TYPE_SSE)); dataTypeMap.put(RESULT_TYPE_TSV_STR, new Integer(RESULT_TYPE_TSV)); } public EndpointSettings(){ } public EndpointSettings(String endpoint){ this.endpoint = endpoint; this.namespaces = makeDefaultNamespaces(endpoint); makeDefaultRepository(endpoint); } private String makeDefaultNamespaces(String endpoint){ String ret = ""; if (endpoint == null){ return ret; } // endpoint最後の"/"を削除 if (endpoint.endsWith("/")){ endpoint = endpoint.substring(0, endpoint.length() - "/".length()); } int index = endpoint.lastIndexOf("/"); if (index > 0){ endpoint = endpoint.substring(0, index); } ret = endpoint + "," + endpoint + "/class," + endpoint + "/instance," + endpoint + "/resource"; return ret; } private void makeDefaultRepository(String endpoint){ int index = endpoint.indexOf("/endpoint/"); if (index >= 0){ this.repositoryURL = endpoint.substring(0, index); this.repository = endpoint.substring(index + "/endpoint/".length()); } } /** * @return endpoint */ public String getEndpoint() { return endpoint; } /** * @param endpoint セットする endpoint */ public void setEndpoint(String endpoint) { if (this.endpoint != null && !this.endpoint.equals(endpoint)){ isChanged = true; } this.endpoint = endpoint; } /** * @return useCustomParam */ public boolean isUseCustomParam() { return useCustomParam; } /** * @param useCustomParam セットする useCustomParam */ public void setUseCustomParam(boolean useCustomParam) { if (this.useCustomParam != useCustomParam){ isChanged = true; } this.useCustomParam = useCustomParam; } /** * @return queryKey */ public String getQueryKey() { return queryKey; } /** * @param queryKey セットする queryKey */ public void setQueryKey(String queryKey) { if (this.queryKey != null && !this.queryKey.equals(queryKey)){ isChanged = true; } this.queryKey = queryKey; } /** * @param namespaces セットする namespaces */ public void setNamespaces(String namespaces) { if (this.namespaces != null && !this.namespaces.equals(namespaces)){ isChanged = true; } this.namespaces = namespaces; } /** * @return namespaces */ public String getNamespaces() { return namespaces; } public String[] getNamespaceList(){ List ret = new ArrayList(); String[] tmp = namespaces.split(","); for (String e : tmp){ ret.add(e.trim()); } return ret.toArray(new String[0]); } /** * @return option */ public String getOption() { return option; } /** * @param option セットする option */ public void setOption(String option) { if (this.option != null && !this.option.equals(option)){ isChanged = true; } this.option = option; } /** * @return encoding */ public String getEncoding() { return encoding; } /** * @param encoding セットする encoding */ public void setEncoding(String encoding) { if (this.encoding != null && !this.encoding.equals(encoding)){ isChanged = true; } this.encoding = encoding; } /** * @param resultType セットする resultType */ public void setResultType(int resultType) { if (this.resultType != resultType){ isChanged = true; } this.resultType = resultType; } /** * @return resultType */ public int getResultType() { return resultType; } /** * @return editable */ public boolean isEditable() { return editable; } /** * @param editable セットする editable */ public void setEditable(boolean editable) { if (this.editable != editable){ isChanged = true; } this.editable = editable; } /** * @return repositoryURL */ public String getRepositoryURL() { return repositoryURL; } /** * @param repositoryURL セットする repositoryURL */ public void setRepositoryURL(String repositoryURL) { if (this.repositoryURL != null && !this.repositoryURL.equals(repositoryURL)){ isChanged = true; } this.repositoryURL = repositoryURL; } /** * @return repository */ public String getRepository() { return repository; } /** * @param repository セットする repository */ public void setRepository(String repository) { if (this.repository != null && !this.repository.equals(repository)){ isChanged = true; } this.repository = repository; } /** * @return user */ public String getUser() { return user; } /** * @param user セットする user */ public void setUser(String user) { if (this.user != null && !this.user.equals(user)){ isChanged = true; } this.user = user; } /** * @return pass */ public String getPass() { return pass; } /** * @param pass セットする pass */ public void setPass(String pass) { if (this.pass != null && !this.pass.equals(pass)){ isChanged = true; } this.pass = pass; } /** * @param isTarget セットする isTarget */ public void setTarget(boolean isTarget) { if (this.isTarget != isTarget){ isChanged = true; } this.isTarget = isTarget; } /** * @return isTarget */ public boolean isTarget() { return isTarget; } /** * 変更状態をリセットする */ public void resetChanged() { this.isChanged = false; } /** * * @return */ public boolean isChanged(){ return this.isChanged; } /* 以下はutilクラスにわけるほうがいい? */ public static String[] getResultDataTypeList(){ return dataTypeMap.keySet().toArray(new String[]{}); } public static Integer getResultDataType(String str){ return dataTypeMap.get(str); } public static String getResultDataType(Integer type){ for (String key : dataTypeMap.keySet()){ if (dataTypeMap.get(key).equals(type)){ return key; } } return null; } public static void outputXML(OutputStream os, EndpointSettings[] settings){ XMLEncoder enc = new XMLEncoder(os); enc.writeObject(settings); enc.close(); } public static EndpointSettings[] inputXML(InputStream is){ XMLDecoder dec = new XMLDecoder(is); EndpointSettings[] ret = (EndpointSettings[])dec.readObject(); return ret; } }