|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.tests.xml; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.beans.PropertyVetoException; import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.util.Iterator; import java.util.Random; import javax.swing.text.BadLocationException; import javax.swing.text.StyledDocument; import org.netbeans.api.java.classpath.ClassPath; import org.netbeans.jellytools.Bundle; import org.netbeans.modules.xml.tax.cookies.TreeDocumentCookie; import org.netbeans.tax.TreeDocument; import org.netbeans.tax.TreeException; import org.netbeans.tax.TreeNode; import org.netbeans.tax.io.XMLStringResult; import org.openide.cookies.EditorCookie; import org.openide.cookies.SaveCookie; import org.openide.filesystems.*; import org.openide.filesystems.FileSystem.AtomicAction; import org.openide.loaders.DataFolder; import org.openide.loaders.DataObject; import org.openide.loaders.DataObjectNotFoundException; import org.openide.loaders.XMLDataObject; import org.openide.modules.ModuleInfo; import org.openide.util.Lookup; import org.openide.util.NbBundle; import org.openide.util.Lookup.Template; /** * Provides the basic support for XML API tests. * @author mschovanek */ public abstract class AbstractTestUtil { protected static boolean DEBUG = true; public static final String CATALOG_PACKAGE = "org.netbeans.modules.xml.catalog"; public static final String CORE_PACKAGE = "org.netbeans.modules.xml.core"; public static final String TAX_PACKAGE = "org.netbeans.modules.xml.tax"; public static final String TEXT_PACKAGE = "org.netbeans.modules.xml.text"; public static final String TOOLS_PACKAGE = "org.netbeans.modules.xml.tools"; public static final String TREE_PACKAGE = "org.netbeans.modules.xml.tree"; //-------------------------------------------------------------------------- // * * * X M L * * * //-------------------------------------------------------------------------- /** * Converts the TreeNode to a string. */ public static String nodeToString(TreeNode node) { try { return XMLStringResult.toString(node); } catch (TreeException te) { return null; } } /** * Converts the node to a string. */ public static String nodeToString(Object node) { return node.toString(); } //-------------------------------------------------------------------------- // * * * S T R I N G S * * * //-------------------------------------------------------------------------- /** Search-and-replace string matches to expression "begin.*end" * @param original the original string * @param begin the begin of substring to be find * @param end the end of substring to be find * @param replaceTo the substring to replace it with * @return a new string with 1st occurrence replaced */ public static String replaceString(String original, String begin, String end, String replaceTo) { int bi = original.indexOf(begin); int ei = original.indexOf(end, bi) + end.length(); if (bi < 0 || ei < 0) { return original; } else { return original.substring(0, bi) + replaceTo + original.substring(ei, original.length()); } } /** * Removes first character occurence from string. */ public static String removeChar(String str, char ch) { int index = str.indexOf(ch); if (index > -1) { StringBuffer sb = new StringBuffer(str).deleteCharAt(str.indexOf(ch)); return new String(sb); } else { return str; } } /** * Joins elemets delimited by delim. */ public static String joinElements(String[] elements, String delim) { if (elements == null) { return null; } String path = elements[0]; for (int i = 1; i < elements.length; i++) { path += (delim + elements[i]); } return path; } /** * Returns last element. */ public static final String lastElement(String string, String delim) { int index = string.lastIndexOf(delim); if (index == -1) { return string; } else { return string.substring(index + 1); } } //-------------------------------------------------------------------------- // * * * S T R I N G L O C A L I Z A T I O N * * * //-------------------------------------------------------------------------- /** Get localized string, removes '&' and cuts parameters like{0} from the end. * @param key key of localized value. * @return localized value. */ public final String getStringTrimmed(String key) { return Bundle.getStringTrimmed(getBundel(), key); } /** Get localized string. * @param key key of localized value. * @return localized value. */ public final String getString(String key) { return NbBundle.getMessage(this.getClass(), key); } /** Get localized string by passing parameter. * @param key key of localized value. * @param param argument to use when formating the message * @return localized value. */ public final String getString(String key, Object param) { return NbBundle.getMessage(this.getClass(), key, param); } /** Get localized string by passing parameter. * @param key key of localized value. * @param param1 argument to use when formating the message * @param param2 the second argument to use for formatting * @return localized value. */ public final String getString(String key, Object param1, Object param2) { return NbBundle.getMessage(this.getClass(), key, param1, param2); } /** Get localized character. Usually used on mnemonic. * @param key key of localized value. * @return localized value. */ public final char getChar(String key) { return NbBundle.getMessage(this.getClass(), key).charAt(0); } private String getBundel() { return this.getClass().getPackage().getName() + ".Bundle"; } //-------------------------------------------------------------------------- // * * * D A T A O B J E C T S * * * //-------------------------------------------------------------------------- /** Converts DataObject to String. */ public static String dataObjectToString(DataObject dataObject) throws IOException, BadLocationException { EditorCookie editorCookie = (EditorCookie) dataObject.getCookie(EditorCookie.class); if (editorCookie != null) { StyledDocument document = editorCookie.openDocument(); if (document != null) { return document.getText(0, document.getLength()); } } return null; } /** Saves DataObject */ public static void saveDataObject(DataObject dataObject) throws IOException { SaveCookie cookie = (SaveCookie) dataObject.getCookie(SaveCookie.class); if (cookie == null) throw new IllegalStateException("Cannot save document without SaveCookie."); cookie.save(); } //-------------------------------------------------------------------------- // * * * F I L E S Y S T E M S * * * //-------------------------------------------------------------------------- /** * Mounts local directory */ public static LocalFileSystem mountDirectory(File dir) throws PropertyVetoException, IOException { LocalFileSystem fs = new LocalFileSystem(); fs.setRootDirectory(dir); Repository rep = Repository.getDefault(); FileSystem ffs = rep.findFileSystem(fs.getSystemName()); if (ffs != null) { rep.removeFileSystem(ffs); } rep.addFileSystem(fs); return fs; } /** * Opens the XML Document with the given package, name and extension */ public static TreeDocument openXMLDocument(String aPackage, String name, String ext) throws IOException { DataObject dao = findDataObject(aPackage, name, ext); if (dao == null) { throw new IOException(aPackage + "." + name + "." + ext + " data object not found."); } XMLDataObject xmlDataObject; if (XMLDataObject.class.isInstance(dao)) { xmlDataObject = (XMLDataObject) dao; } else { throw new IOException(aPackage + "." + name + "." + ext + " data object is not XMLDataObject."); } TreeDocumentCookie cookie = (TreeDocumentCookie) xmlDataObject.getCookie(TreeDocumentCookie.class); if (cookie == null) { throw new IOException("Missing TreeDocumentCookie at " + aPackage + "." + name + "." + ext); } TreeDocument document = (TreeDocument) cookie.getDocumentRoot(); if (document == null) { throw new IOException("Ivalid XML data object" + aPackage + "." + name + "." + ext); } return document; } /** * Deletes FileObject. */ public static void deleteFileObject(FileObject fo) throws IOException { DataObject dataObject = DataObject.find(fo); dataObject.getNodeDelegate().destroy(); } /** * Finds DataFolder. */ public static DataFolder findFolder(String aPackage) throws Exception { return (DataFolder) findDataObject(aPackage, null, null); } /** * Finds absolut path for FileObject. */ public static String toAbsolutePath(FileObject fo) { return FileUtil.toFile(fo).getAbsolutePath(); } /** * Finds the DataObject with the given package, name and extension */ public static DataObject findDataObject(String aPackage, String name, String ext) throws DataObjectNotFoundException { FileObject fo = null; fo = Repository.getDefault().find(aPackage, name, ext); if (fo == null) { return null; } else { return DataObject.find(fo); } } /** * Finds the DataObject with the given package, name and extension */ public static FileObject findFileObject(String aPackage, String name, String ext) { return Repository.getDefault().find(aPackage, name, ext); } /** * Finds the DataObject with the given name in test's 'data' folder. The name of a resource is * a "/"-separated path name that identifies the resource relatively to 'data' folder.
|
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.