本文整理汇总了Java中org.openide.xml.XMLUtil.parse方法的典型用法代码示例。如果您正苦于以下问题:Java XMLUtil.parse方法的具体用法?Java XMLUtil.parse怎么用?Java XMLUtil.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.openide.xml.XMLUtil
的用法示例。
在下文中一共展示了XMLUtil.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readCustomScript
import org.openide.xml.XMLUtil; //导入方法依赖的package包/类
/**
* Read a generated script if it exists, else create a skeleton.
* Imports jdk.xml if appropriate.
* @param scriptPath e.g. {@link #FILE_SCRIPT_PATH} or {@link #GENERAL_SCRIPT_PATH}
*/
Document readCustomScript(String scriptPath) throws IOException, SAXException {
// XXX if there is TAX support for rewriting XML files, use that here...
FileObject script = helper.getProjectDirectory().getFileObject(scriptPath);
Document doc;
if (script != null) {
InputStream is = script.getInputStream();
try {
doc = XMLUtil.parse(new InputSource(is), false, true, null, null);
} finally {
is.close();
}
} else {
doc = XMLUtil.createDocument("project", /*XXX:"antlib:org.apache.tools.ant"*/null, null, null); // NOI18N
Element root = doc.getDocumentElement();
String projname = ProjectUtils.getInformation(project).getDisplayName();
root.setAttribute("name", NbBundle.getMessage(JavaActions.class, "LBL_generated_script_name", projname));
}
if (helper.getProjectDirectory().getFileObject(JdkConfiguration.JDK_XML) != null) {
JdkConfiguration.insertJdkXmlImport(doc);
}
return doc;
}
示例2: forProject
import org.openide.xml.XMLUtil; //导入方法依赖的package包/类
public ProjectHudsonJobCreator forProject(Project project) {
FileObject projectXml = project.getProjectDirectory().getFileObject("nbproject/project.xml"); // NOI18N
if (projectXml != null && projectXml.isData()) {
try {
Document doc = XMLUtil.parse(new InputSource(projectXml.getURL().toString()), false, true, null, null);
String type = XPathFactory.newInstance().newXPath().evaluate(
"/*/*[local-name(.)='type']", doc.getDocumentElement()); // NOI18N
for (AntBasedJobCreator handler : Lookup.getDefault().lookupAll(AntBasedJobCreator.class)) {
if (handler.type().equals(type)) {
return new JobCreator(project, handler.forProject(project));
}
}
} catch (Exception x) {
Logger.getLogger(JobCreator.class.getName()).log(Level.FINE, "Could not check type of " + projectXml, x);
}
}
return null;
}
示例3: parseLaunchConfigurations
import org.openide.xml.XMLUtil; //导入方法依赖的package包/类
private void parseLaunchConfigurations() throws IOException, ProjectImporterException {
List<LaunchConfiguration> configs = new ArrayList<LaunchConfiguration>();
File[] launches = new File(workspace.getDirectory(), ".metadata/.plugins/org.eclipse.debug.core/.launches").listFiles(new FilenameFilter() { // NOI18N
public boolean accept(File dir, String name) {
return name.endsWith(".launch"); // NOI18N
}
});
if (launches != null) {
for (File launch : launches) {
Document doc;
try {
doc = XMLUtil.parse(new InputSource(Utilities.toURI(launch).toString()), false, false, null, null);
} catch (SAXException x) {
throw new ProjectImporterException("Could not parse " + launch, x);
}
Element launchConfiguration = doc.getDocumentElement();
String type = launchConfiguration.getAttribute("type"); // NOI18N
Map<String,String> attrs = new HashMap<String,String>();
NodeList nl = launchConfiguration.getElementsByTagName("stringAttribute"); // NOI18N
for (int i = 0; i < nl.getLength(); i++) {
Element stringAttribute = (Element) nl.item(i);
attrs.put(stringAttribute.getAttribute("key"), stringAttribute.getAttribute("value")); // NOI18N
}
configs.add(new LaunchConfiguration(launch.getName().replaceFirst("\\.launch$", ""), type, // NOI18N
attrs.get("org.eclipse.jdt.launching.PROJECT_ATTR"), // NOI18N
attrs.get("org.eclipse.jdt.launching.MAIN_TYPE"), // NOI18N
attrs.get("org.eclipse.jdt.launching.PROGRAM_ARGUMENTS"), // NOI18N
attrs.get("org.eclipse.jdt.launching.VM_ARGUMENTS"))); // NOI18N
}
}
workspace.setLaunchConfigurations(configs);
}
示例4: testWriteXml
import org.openide.xml.XMLUtil; //导入方法依赖的package包/类
public void testWriteXml() throws Exception {
JDBCDriver driver = JDBCDriver.create("bar_driver", "Bar Driver", "org.bar.BarDriver", new URL[] { new URL("file:///bar1.jar"), new URL("file:///bar2.jar") });
JDBCDriverConvertor.create(driver);
FileObject fo = Util.getDriversFolder().getFileObject("org_bar_BarDriver.xml");
ErrorHandlerImpl errHandler = new ErrorHandlerImpl();
Document doc = null;
InputStream input = fo.getInputStream();
try {
doc = XMLUtil.parse(new InputSource(input), true, true, errHandler, EntityCatalog.getDefault());
} finally {
input.close();
}
assertFalse("JDBCDriverConvertor generates invalid XML acc to the DTD!", errHandler.error);
Document goldenDoc = null;
input = getClass().getResourceAsStream("bar-driver.xml");
try {
goldenDoc = XMLUtil.parse(new InputSource(input), true, true, null, EntityCatalog.getDefault());
} finally {
input.close();
}
assertTrue(DOMCompare.compareDocuments(doc, goldenDoc));
}
示例5: filterProjectXML
import org.openide.xml.XMLUtil; //导入方法依赖的package包/类
private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileUtil.copy(str, baos);
Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null);
NodeList nl = doc.getDocumentElement().getElementsByTagName("name");
if (nl != null) {
for (int i = 0; i < nl.getLength(); i++) {
Element el = (Element) nl.item(i);
if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) {
NodeList nl2 = el.getChildNodes();
if (nl2.getLength() > 0) {
nl2.item(0).setNodeValue(name);
}
break;
}
}
}
try (OutputStream out = fo.getOutputStream()) {
XMLUtil.write(doc, out, "UTF-8");
}
} catch (IOException | DOMException | SAXException ex) {
Exceptions.printStackTrace(ex);
writeFile(str, fo);
}
}
示例6: parseData
import org.openide.xml.XMLUtil; //导入方法依赖的package包/类
/**
* Load a project.xml from a project.
* @param basedir a putative project base directory
* @return its primary configuration data (if there is an NBM project here), else null
*/
static Element parseData(File basedir) throws IOException {
File projectXml = new File(basedir, PROJECT_XML);
// #61579: tboudreau claims File.exists is much cheaper on some systems
//System.err.println("parseData: " + basedir);
if (!projectXml.exists() || !projectXml.isFile()) {
return null;
}
Document doc;
try {
xmlFilesParsed++;
timeSpentInXmlParsing -= System.currentTimeMillis();
doc = XMLUtil.parse(new InputSource(Utilities.toURI(projectXml).toString()), false, true, null, null);
timeSpentInXmlParsing += System.currentTimeMillis();
} catch (SAXException e) {
throw (IOException) new IOException(projectXml + ": " + e.toString()).initCause(e); // NOI18N
}
Element docel = doc.getDocumentElement();
Element type = XMLUtil.findElement(docel, "type", "http://www.netbeans.org/ns/project/1"); // NOI18N
if (!XMLUtil.findText(type).equals("org.netbeans.modules.apisupport.project")) { // NOI18N
return null;
}
Element cfg = XMLUtil.findElement(docel, "configuration", "http://www.netbeans.org/ns/project/1"); // NOI18N
Element data = XMLUtil.findElement(cfg, "data", NbModuleProject.NAMESPACE_SHARED); // NOI18N
if (data != null) {
return data;
} else {
data = XMLUtil.findElement(cfg, "data", NbModuleProject.NAMESPACE_SHARED_2); // NOI18N
if (data != null) {
return XMLUtil.translateXML(data, NbModuleProject.NAMESPACE_SHARED);
} else {
return null;
}
}
}
示例7: readFormToDocument
import org.openide.xml.XMLUtil; //导入方法依赖的package包/类
/**
* Parse form file into DOM document. If the form is a xml 1.1 file, try to
* parse it first as 1.0, only if it fails then try to process it as 1.1.
* The reason is that xml 1.1 parser fails to parse larger files correctly
* (e.g. in TableCustomizer or GridBagCustomizer forms some property names
* are read mangled, while read fine as xml 1.0).
* @param formFile
* @return
* @throws IOException
* @throws SAXException
*/
static Document readFormToDocument(FileObject formFile) throws IOException, SAXException {
InputStream xmlInputStream = new XML10InputStream(formFile.getInputStream());
try {
return XMLUtil.parse(new InputSource(xmlInputStream),
false, false, null, null);
} catch (Exception ex) {
LOGGER.log(Level.INFO, "Could not read form as xml 1.0, will try as xml 1.1."); // NOI18N
LOGGER.log(Level.FINE, "", ex); // NOI18N
}
return XMLUtil.parse(new InputSource(formFile.getInputStream()),
false, false, null, null);
}
示例8: filterProjectXML
import org.openide.xml.XMLUtil; //导入方法依赖的package包/类
private static void filterProjectXML(FileObject fo, ZipInputStream str, String name) throws IOException {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileUtil.copy(str, baos);
Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(baos.toByteArray())), false, false, null, null);
NodeList nl = doc.getDocumentElement().getElementsByTagName("name");
if (nl != null) {
for (int i = 0; i < nl.getLength(); i++) {
Element el = (Element) nl.item(i);
if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName())) {
NodeList nl2 = el.getChildNodes();
if (nl2.getLength() > 0) {
nl2.item(0).setNodeValue(name);
}
break;
}
}
}
OutputStream out = fo.getOutputStream();
try {
XMLUtil.write(doc, out, "UTF-8");
} finally {
out.close();
}
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
writeFile(str, fo);
}
}
示例9: from
import org.openide.xml.XMLUtil; //导入方法依赖的package包/类
public static HintPreferencesProviderImpl from(@NonNull URI settings) {
Reference<HintPreferencesProviderImpl> ref = uri2Cache.get(settings);
HintPreferencesProviderImpl cachedResult = ref != null ? ref.get() : null;
if (cachedResult != null) return cachedResult;
Document doc = null;
File file = Utilities.toFile(settings); //XXX: non-file:// scheme
if (file.canRead()) {
try(InputStream in = new BufferedInputStream(new FileInputStream(file))) {
doc = XMLUtil.parse(new InputSource(in), false, false, null, EntityCatalog.getDefault());
} catch (SAXException | IOException ex) {
LOG.log(Level.FINE, null, ex);
}
}
if (doc == null) {
doc = XMLUtil.createDocument("configuration", null, "-//NetBeans//DTD Tool Configuration 1.0//EN", "http://www.netbeans.org/dtds/ToolConfiguration-1_0.dtd");
}
synchronized (uri2Cache) {
ref = uri2Cache.get(settings);
cachedResult = ref != null ? ref.get() : null;
if (cachedResult != null) return cachedResult;
uri2Cache.put(settings, new CleaneableSoftReference(cachedResult = new HintPreferencesProviderImpl(settings, doc), settings));
}
return cachedResult;
}
示例10: assertValidate
import org.openide.xml.XMLUtil; //导入方法依赖的package包/类
private static void assertValidate(String xml) throws Exception {
XMLUtil.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("UTF-8"))), false, true, XMLUtil.defaultErrorHandler(), new EntityResolver() {
public @Override InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(new ByteArrayInputStream(new byte[0]));
/* XXX when #192595 is implemented, can move DTDs here from core.windows, set validate=true above, and use:
InputSource r = EntityCatalog.getDefault().resolveEntity(publicId, systemId);
if (r != null) {
return r;
} else {
throw new IOException("network connection to " + systemId);
}
*/
}
});
}
示例11: testWriteXml
import org.openide.xml.XMLUtil; //导入方法依赖的package包/类
private void testWriteXml(String password, boolean savePassword,
String goldenFileName) throws Exception {
DatabaseConnection conn = new DatabaseConnection("org.bar.BarDriver",
"bar_driver", "jdbc:bar:localhost", "schema", "user", password,
savePassword);
DatabaseConnectionConvertor.create(conn);
FileObject fo = Util.getConnectionsFolder().getChildren()[0];
ErrorHandlerImpl errHandler = new ErrorHandlerImpl();
Document doc = null;
InputStream input = fo.getInputStream();
try {
doc = XMLUtil.parse(new InputSource(input), true, true, errHandler, EntityCatalog.getDefault());
} finally {
input.close();
}
assertFalse("DatabaseConnectionConvertor generates invalid XML acc to the DTD!", errHandler.error);
Document goldenDoc = null;
input = getClass().getResourceAsStream(goldenFileName);
errHandler = new ErrorHandlerImpl();
try {
goldenDoc = XMLUtil.parse(new InputSource(input), true, true, errHandler, EntityCatalog.getDefault());
} finally {
input.close();
}
assertTrue(DOMCompare.compareDocuments(doc, goldenDoc));
assertFalse("DatabaseConnectionConvertor generates invalid XML acc to the DTD!", errHandler.error);
}
示例12: readProjectFacets
import org.openide.xml.XMLUtil; //导入方法依赖的package包/类
public static Facets readProjectFacets(File projectDir, Set<String> natures) throws IOException {
if (!natures.contains("org.eclipse.wst.common.project.facet.core.nature")) { // NOI18N
return null;
}
File f = new File(projectDir, ".settings/org.eclipse.wst.common.project.facet.core.xml"); // NOI18N
if (!f.exists()) {
return null;
}
Document doc;
try {
doc = XMLUtil.parse(new InputSource(Utilities.toURI(f).toString()), false, true, XMLUtil.defaultErrorHandler(), null);
} catch (SAXException e) {
IOException ioe = (IOException) new IOException(f + ": " + e.toString()).initCause(e); // NOI18N
throw ioe;
}
Element root = doc.getDocumentElement();
if (!"faceted-project".equals(root.getLocalName())) { // NOI18N
return null;
}
List<Facets.Facet> facets = new ArrayList<Facets.Facet>();
List<Element> elements = XMLUtil.findSubElements(root);
for (Element element : elements) {
if (!"installed".equals(element.getNodeName())) { // NOI18N
continue;
}
String facet = element.getAttribute("facet"); // NOI18N
String version = element.getAttribute("version"); // NOI18N
if (facet != null && version != null) {
facets.add(new Facets.Facet(facet, version));
}
}
return new Facets(facets);
}
示例13: modifyBuildXml
import org.openide.xml.XMLUtil; //导入方法依赖的package包/类
/**
* Update SE buildscript dependencies on FX buildscript
* @param proj
* @return
* @throws IOException
*/
private static boolean modifyBuildXml(Project proj) throws IOException {
boolean res = false;
final FileObject buildXmlFO = getBuildXml(proj);
if (buildXmlFO == null) {
LOGGER.warning("The project build script does not exist, the project cannot be extended by JFX."); //NOI18N
return res;
}
Document xmlDoc = null;
try {
xmlDoc = XMLUtil.parse(new InputSource(buildXmlFO.toURL().toExternalForm()), false, true, null, null);
} catch (SAXException ex) {
Exceptions.printStackTrace(ex);
}
if(!addExtension(proj)) {
LOGGER.log(Level.INFO,
"Trying to include JFX build snippet in project type that doesn't support AntBuildExtender API contract."); // NOI18N
}
//TODO this piece shall not proceed when the upgrade to j2se-project/4 was cancelled.
//how to figure..
Element docElem = xmlDoc.getDocumentElement();
NodeList nl = docElem.getElementsByTagName("target"); // NOI18N
boolean changed = false;
nl = docElem.getElementsByTagName("import"); // NOI18N
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
if (e.getAttribute("file") != null && JFX_BUILD_IMPL_PATH.equals(e.getAttribute("file"))) { // NOI18N
e.getParentNode().removeChild(e);
changed = true;
break;
}
}
if (changed) {
final Document fdoc = xmlDoc;
try {
ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
@Override
public Void run() throws Exception {
FileLock lock = buildXmlFO.lock();
try {
OutputStream os = buildXmlFO.getOutputStream(lock);
try {
XMLUtil.write(fdoc, os, "UTF-8"); // NOI18N
} finally {
os.close();
}
} finally {
lock.releaseLock();
}
return null;
}
});
} catch (MutexException mex) {
throw (IOException) mex.getException();
}
}
return res;
}
示例14: createCustomDefs
import org.openide.xml.XMLUtil; //导入方法依赖的package包/类
private static Map<String,Map<String,Class>> createCustomDefs(Map<String,ClassLoader> cDCLs) throws IOException {
Map<String,Map<String,Class>> m = new HashMap<String,Map<String,Class>>();
Map<String,Class> tasks = new HashMap<String,Class>();
Map<String,Class> types = new HashMap<String,Class>();
// XXX #36776: should eventually support <macrodef>s here
m.put("task", tasks); // NOI18N
m.put("type", types); // NOI18N
for (Map.Entry<String,ClassLoader> entry : cDCLs.entrySet()) {
String cnb = entry.getKey();
ClassLoader l = entry.getValue();
String resource = cnb.replace('.', '/') + "/antlib.xml"; // NOI18N
URL antlib = l.getResource(resource);
if (antlib == null) {
throw new IOException("Could not find " + resource + " in ant/nblib/" + cnb.replace('.', '-') + ".jar"); // NOI18N
}
Document doc;
try {
doc = XMLUtil.parse(new InputSource(antlib.toExternalForm()), false, true, /*XXX needed?*/null, null);
} catch (SAXException e) {
throw (IOException)new IOException(e.toString()).initCause(e);
}
Element docEl = doc.getDocumentElement();
if (!docEl.getLocalName().equals("antlib")) { // NOI18N
throw new IOException("Bad root element for " + antlib + ": " + docEl); // NOI18N
}
NodeList nl = docEl.getChildNodes();
Map<String,String> newTaskDefs = new HashMap<String,String>();
Map<String,String> newTypeDefs = new HashMap<String,String>();
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i);
if (n.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
Element def = (Element)n;
boolean type;
if (def.getNodeName().equals("taskdef")) { // NOI18N
type = false;
} else if (def.getNodeName().equals("typedef")) { // NOI18N
type = true;
} else {
LOG.warning("Warning: unrecognized definition " + def + " in " + antlib);
continue;
}
String name = def.getAttribute("name"); // NOI18N
if (name == null) {
// Not a hard error since there might be e.g. <taskdef resource="..."/> here
// which we do not parse but which is permitted in antlib by Ant.
LOG.warning("Warning: skipping definition " + def + " with no 'name' in " + antlib);
continue;
}
String classname = def.getAttribute("classname"); // NOI18N
if (classname == null) {
// But this is a hard error.
throw new IOException("No 'classname' attr on def of " + name + " in " + antlib); // NOI18N
}
// XXX would be good to handle at least onerror attr too
String nsname = "antlib:" + cnb + ":" + name; // NOI18N
(type ? newTypeDefs : newTaskDefs).put(nsname, classname);
}
loadDefs(newTaskDefs, tasks, l);
loadDefs(newTypeDefs, types, l);
}
return m;
}
示例15: parsePhases
import org.openide.xml.XMLUtil; //导入方法依赖的package包/类
private static Map<String,List<String>> parsePhases(String u, String packaging) throws Exception {
Document doc = XMLUtil.parse(new InputSource(u), false, false, XMLUtil.defaultErrorHandler(), null);
for (Element componentsEl : XMLUtil.findSubElements(doc.getDocumentElement())) {
for (Element componentEl : XMLUtil.findSubElements(componentsEl)) {
if (XMLUtil.findText(XMLUtil.findElement(componentEl, "role", null)).trim().equals("org.apache.maven.lifecycle.mapping.LifecycleMapping")
&& XMLUtil.findText(XMLUtil.findElement(componentEl, "implementation", null)).trim().equals("org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping")
&& XMLUtil.findText(XMLUtil.findElement(componentEl, "role-hint", null)).trim().equals(packaging)) {
for (Element configurationEl : XMLUtil.findSubElements(componentEl)) {
if (!configurationEl.getTagName().equals("configuration")) {
continue;
}
Element phases = XMLUtil.findElement(configurationEl, "phases", null);
if (phases == null) {
for (Element lifecyclesEl : XMLUtil.findSubElements(configurationEl)) {
if (!lifecyclesEl.getTagName().equals("lifecycles")) {
continue;
}
for (Element lifecycleEl : XMLUtil.findSubElements(lifecyclesEl)) {
if (XMLUtil.findText(XMLUtil.findElement(lifecycleEl, "id", null)).trim().equals("default")) {
phases = XMLUtil.findElement(lifecycleEl, "phases", null);
break;
}
}
}
}
if (phases != null) {
Map<String,List<String>> result = new LinkedHashMap<String,List<String>>();
for (Element phase : XMLUtil.findSubElements(phases)) {
List<String> plugins = new ArrayList<String>();
for (String plugin : XMLUtil.findText(phase).split(",")) {
String[] gavMojo = plugin.trim().split(":", 4);
plugins.add(gavMojo[0] + ':' + gavMojo[1] + ':' + (gavMojo.length == 4 ? gavMojo[3] : gavMojo[2])); // version is not used here
}
result.put(phase.getTagName(), plugins);
}
LOG.log(Level.FINE, "for {0} found in {1}: {2}", new Object[] {packaging, u, result});
return result;
}
}
}
}
}
return null;
}