/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License.
*/ package org.netbeans.modules.xml.util;
// last catalog directory privatestatic File lastDirectory;
/** Default and only one instance of this class. */ publicstaticfinal Util THIS = new Util(); publicstaticfinal String NO_NAME_SPACE = "NO_NAME_SPACE"; //NOI18N
/** Nobody can create instance of it, just me. */ private Util () {
}
/** * Prompts user for a Schema file. * @param extensions a space separated list of file extensions * @return filename or null if operation was cancelled.
*/ publicstatic File selectSchemaFile(final String extensions) {
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileFilter() { publicboolean accept(File f) { if (f.isDirectory()) returntrue;
StringTokenizer token = new StringTokenizer(extensions, " "); // NOI18N while (token.hasMoreElements()) { if (f.getName().endsWith(token.nextToken())) returntrue;
} returnfalse;
} public String getDescription() { return Util.THIS.getString(Util.class, "PROP_schema_mask"); // NOI18N
}
});
if (lastDirectory != null) {
chooser.setCurrentDirectory(lastDirectory);
}
chooser.setDialogTitle(Util.THIS.getString(Util.class, "PROP_schema_dialog_name")); while (chooser.showDialog(WindowManager.getDefault().getMainWindow(),
Util.THIS.getString(Util.class, "PROP_schema_select_button"))
== JFileChooser.APPROVE_OPTION)
{
File f = chooser.getSelectedFile();
lastDirectory = chooser.getCurrentDirectory(); if (f != null && f.isFile()) {
StringTokenizer token = new StringTokenizer(extensions, " "); // NOI18N while (token.hasMoreElements()) { if (f.getName().endsWith(token.nextToken())) return f;
}
}
publicstatic Map getFiles2NSMappingInProj(File rootFile, String docType){
List fileList = getFilesWithExtension(rootFile, docType, new ArrayList());
Map result = new HashMap();
String xpathQuery = "//xsd:schema/@targetNamespace";
for(int i=0; i < fileList.size();i++){
File file = (File)fileList.get(i);
if(Thread.currentThread().isInterrupted()) //if interrupted by the client dump the result and immediately return break;
List targetNSList = null; try {
targetNSList = runXPathQuery(file, xpathQuery);
String targetNS = null;
FileObject fobj = FileUtil.toFileObject(file); if(targetNSList.size() > 0){ //just take the first and ignore rest
targetNS = (String)targetNSList.get(0);
} else{
targetNS = NO_NAME_SPACE;
} if( (targetNS == NO_NAME_SPACE)) //this is wsdl and it must have NS so ignore this file continue;
result.put(fobj, targetNS);
} catch (Exception ex) { //ex.printStackTrace(); //ignore this route
}
} return result;
}
publicstatic Map<InputSource, String> getCatalogSchemaNSMappings() {
UserCatalog cat = UserCatalog.getDefault();
Iterator it = cat.getPublicIDs();
Map<InputSource, String> result = new HashMap<InputSource, String>(); while (it.hasNext()) {
String uri = (String)it.next(); if (uri.startsWith(PREFIX_SCHEMA)) {
uri = uri.substring(PREFIX_SCHEMA.length()); try {
InputSource src = cat.getEntityResolver().resolveEntity(null, uri); if (src == null) { continue;
}
String sysId = src.getSystemId(); // TODO: should work for other protocols, too if (!sysId.startsWith(PROTOCOL_FILE)) { continue;
}
FileObject fo = toFileObject(src); if (fo != null) {
result.put(src, uri);
}
} catch (SAXException ex) {
LOG.log(Level.FINE, "Resolution failed", ex); // NOI18N
} catch (IOException ex) {
LOG.log(Level.FINE, "Resolution failed", ex); // NOI18N
}
}
} return result;
}
publicstatic List getFilesWithExtension(File startFile, String fileExtension, List curList) { if(Thread.currentThread().isInterrupted()) //if interrupted by the client dump the result and immediately return return curList; if(curList == null)
curList = new ArrayList(); if(startFile.isFile()){ int index = startFile.getName().lastIndexOf("."); if(index != -1){
String extn = startFile.getName().substring(index+1); if((extn != null) && (extn.equalsIgnoreCase(fileExtension)))
curList.add(startFile);
}
} if(startFile.isDirectory()){
File[] children = startFile.listFiles(); if(children != null){ for(int i=0; i < children.length; i++ ){
File child = (File) children[i];
getFilesWithExtension(child, fileExtension, curList);
}
}
} return curList;
}
publicstatic List runXPathQuery(File parsedFile, String xpathExpr) throws Exception{
List result = new ArrayList();
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(getNamespaceContext());
List relativeToPathStack = new ArrayList(); // build the path stack info to compare it afterwards
file = file.getCanonicalFile(); while (file!=null) {
filePathStack.add(0, file);
file = file.getParentFile();
}
relativeTo = relativeTo.getCanonicalFile(); while (relativeTo!=null) {
relativeToPathStack.add(0, relativeTo);
relativeTo = relativeTo.getParentFile();
} // compare as long it goes int count = 0;
file = (File)filePathStack.get(count);
relativeTo = (File)relativeToPathStack.get(count); while ( (count < filePathStack.size()-1) && (count <relativeToPathStack.size()-1) && file.equals(relativeTo)) {
count++;
file = (File)filePathStack.get(count);
relativeTo = (File)relativeToPathStack.get(count);
} if (file.equals(relativeTo)) count++; // up as far as necessary
StringBuffer relString = new StringBuffer(); for (int i = count; i < relativeToPathStack.size(); i++) { //hard code to front slash otherwise code completion doesnt work
relString.append(".."+"/");
} // now back down to the file for (int i = count; i <filePathStack.size()-1; i++) { //hard code to front slash otherwise code completion doesnt work
relString.append(((File)filePathStack.get(i)).getName()+"/");
}
relString.append(((File)filePathStack.get(filePathStack.size()-1)).getName()); // just to test // File relFile = new File(origRelativeTo.getAbsolutePath()+File.separator+relString.toString()); // if (!relFile.getCanonicalFile().equals(origFile.getCanonicalFile())) { // throw new IOException("Failed to find relative path."); // } return relString.toString();
}
privatestatic Map namespaces = new HashMap(); privatestatic Map prefixes = new HashMap();
public HashNamespaceResolver(Map nsTable) {
namespaces = nsTable;
prefixes = new HashMap();
Set set = namespaces.entrySet();
Iterator it = set.iterator(); while(it.hasNext()){ //for (Entry<String,String> e : namespaces.entrySet()) {
Entry e = (Entry)it.next();
prefixes.put(e.getValue(), e.getKey());
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.