📄 utils.java
字号:
// The MIT License
//
// Copyright (c) 2004 Evren Sirin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
package org.mindswap.utils;
import java.awt.Component;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.StringReader;
import java.net.URI;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.xml.namespace.QName;
import org.w3c.dom.Attr;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import com.hp.hpl.jena.vocabulary.RDF;
public class Utils
{
public static URI getURI(String s) throws Exception {
URI uri = URI.create(s);
if(uri == null) {
try {
File file = new File(s);
if(file.canRead())
uri = file.toURI();
} catch(Exception e) {
e.printStackTrace();
}
}
if(uri == null)
throw new FileNotFoundException(s + " is not a valid URI!");
return uri;
}
public static String getLocalName(String uri) {
// hashy name
int index = uri.indexOf("#");
// slashy name
if(index == -1) index = uri.lastIndexOf("/");
if(index == -1) return uri;
return uri.substring(index + 1);
}
public static String getNamespace(String uri) {
// hashy name
int index = uri.indexOf("#");
// slashy name
if(index == -1) index = uri.lastIndexOf("/");
if(index == -1) return null;
return uri.substring(0, index);
}
public static String getShortUri(String uri) {
int index = uri.lastIndexOf("/");
if(index == -1) return uri;
return uri.substring(index + 1);
}
public static String toURI(QName qname) {
return qname.getNamespaceURI() + "#" + qname.getLocalPart();
}
public static String getRDFAbbr(QName uri) {
return getRDFAbbr(uri.getNamespaceURI(), uri.getLocalPart());
}
private static String getAbbr(String ns, String localName, String ch) {
return getNamespaceAbbr(ns) + ch + localName;
}
public static String getRDFAbbr(String ns, String localName) {
return getAbbr(ns, localName, ":");
}
public static String getRDFAbbr(String uri) {
String[] t = split(uri, "#");
return getRDFAbbr(t[0], t[1]);
}
public static String getFullUri(String u) {
if(u.indexOf(":") == -1)
return u;
String[] t = split(u, ":");
String nsUri = (String) getHashtableKey(ns, t[0]);
//System.out.println("Search " + t[0] + " in " + ns + " found " + nsUri);
if(nsUri == null)
return u;
else if(!nsUri.endsWith("#"))
nsUri += "#";
return nsUri + t[1];
}
static Hashtable ns = new Hashtable();
static {
ns.put(Consts.xsdURI, "xsd");
ns.put(Consts.rdfURI, "rdf");
ns.put(Consts.rdfsURI, "rdfs");
ns.put(Consts.damlURI, "daml");
ns.put(Consts.owlURI, "owl");
ns.put(Consts.soapURI, "soap-env");
ns.put(Consts.soapEnc, "soap_enc");
}
public static boolean addNS(String uri, String abbr) {
String nsValue = (String) ns.get(uri);
if(nsValue != null || getHashtableKey(ns, abbr) != null)
return false;
ns.put(uri, abbr);
return true;
}
public static String getNamespaceAbbr(String uri) {
String nsValue = (String) ns.get(uri);
if(nsValue == null) {
nsValue = "ns" + (ns.size()+1);
ns.put(uri, nsValue);
}
return nsValue;
}
public static Hashtable getNamespaces() {
return ns;
}
public static void printNamespaces() {
Iterator e = ns.entrySet().iterator();
while(e.hasNext()) {
Map.Entry entry = (Map.Entry) e.next();
String nsURI = (String) entry.getKey();
String nsValue = (String) entry.getValue();
System.out.println(nsValue + " -> " + nsURI);
}
}
public static Object getHashtableKey(Hashtable h, Object value) {
Iterator e = h.entrySet().iterator();
while(e.hasNext()) {
Map.Entry entry = (Map.Entry) e.next();
if(entry.getValue().equals(value))
return entry.getKey();
}
return null;
}
public static void centerFrame(java.awt.Window frame) {
java.awt.Dimension screenSize =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
java.awt.Dimension size = frame.getSize();
screenSize.height = screenSize.height/2;
screenSize.width = screenSize.width/2;
size.height = size.height/2;
size.width = size.width/2;
int y = screenSize.height - size.height;
int x = screenSize.width - size.width;
frame.setLocation(x, y);
}
public static String replace(String str, String s1, String s2) {
StringBuffer buffer = new StringBuffer();
int start = 0;
int index = str.indexOf(s1);
while(index != -1) {
buffer.append(str.substring(start, index));
buffer.append(s2);
start = index + s1.length();
index = str.indexOf(s1, start);
}
buffer.append(str.substring(start));
return buffer.toString();
}
public static String[] split(String s, String token) {
Vector result = new Vector();
int length = token.length();
int pos = s.indexOf(token);
int lastPos = 0;
while (pos >= 0) {
result.addElement(s.substring(lastPos, pos));
lastPos = pos + length;
pos = s.indexOf(token, lastPos);
}
if(lastPos < s.length())
result.addElement(s.substring(lastPos));
String[] r = new String[result.size()];
result.copyInto(r);
return r;
}
public static void printTime(String msg) {
System.out.println("Time: (" + System.currentTimeMillis() + ") " + msg);
}
public static String toString(Object[] array) {
String s = "[Array ";
if(array != null && array.length > 0) {
s += array[0];
for(int i = 1; i < array.length; i++)
s += "," + array[i];
}
s += "]";
return s;
}
public final static ActionListener windowCloserAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComponent c = (JComponent) e.getSource();
Window w = (Window) c.getTopLevelAncestor();
w.dispose();
}
};
public final static WindowAdapter systemExitAction = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
public static void showMessage(String str) {
str = decodeString(str);
JFrame info = new JFrame("Result");
JTextArea t = new JTextArea(str, 15, 40);
t.setEditable(false);
t.setLineWrap(true);
t.setWrapStyleWord(true);
JButton ok = new JButton("Close");
ok.addActionListener(windowCloserAction);
info.getContentPane().setLayout(new BoxLayout(info.getContentPane(), BoxLayout.Y_AXIS));
info.getContentPane().add(new JScrollPane(t));
info.getContentPane().add(ok);
ok.setAlignmentX(Component.CENTER_ALIGNMENT);
info.pack();
//info.setResizable(false);
centerFrame(info);
info.show();
}
public static Node getAsNode(String in) {
try {
org.apache.xerces.parsers.DOMParser parser =
new org.apache.xerces.parsers.DOMParser();
parser.setErrorHandler(new ErrorHandler() {
public void warning(SAXParseException exception) throws SAXException {
}
public void error(SAXParseException exception) throws SAXException {
}
public void fatalError(SAXParseException exception) throws SAXException {
}
});
parser.parse(new org.xml.sax.InputSource(
new StringReader(in)));
return parser.getDocument().getDocumentElement();
} catch(Exception e) {
//System.out.println("Invalid XML " + in + " " + e);
//e.printStackTrace();
}
return null;
}
public static boolean getBoolean(String str) {
return (str == null) ? false :
str.toLowerCase().equals("true") ||
str.equals("1");
}
public static String toString(boolean b) {
return b ? "true" : "false";
}
public static String decodeString(String str) {
String string = new String(str);
string = Utils.replace(string,"脙漏", "
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -