本文介绍了WSDL Inliner不按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这个任务,找出为什么这个程序不按预期工作。它需要一个WSDL文件,并使用importet文件的实际内容替换xsd:imports,并且应该删除import语句。内联正在工作,但不会删除导入标签?



我有doWork方法:



public void doWork(String wsdlFilePath)throws Exception {
try {
System.out.println(reading wsdlFile+ wsdlFilePath +... );

文档wsdlDoc = readWsdl(wsdlFilePath);
handlesTypes(wsdlDoc,getDirPath(wsdlFilePath));
writeWsdlToOutputFile(wsdlDoc);

} catch(异常e){...

readWsdl(...)将读取给定的wsdl作为文档

 文档readWsdl(String wsdlFilePath)throws Exception {
DocumentBuilder docBuilder = factory.newDocumentBuilder();
printOut(从wsdlFilePath读取wsdl);
InputStream in = null;
文档doc = null;

try {
File wsdlFile = new File(wsdlFilePath);
// this.baseDir = wsdlFile.get getParent()
in = new FileInputStream(wsdlFile);
InputSource is = new InputSource(in);
doc = docBuilder.parse(is);
} catch(Exception e){
// debug
printOut(读取WSDL时的异常+ e,e);
throw new RuntimeException(can not read InputFile:+ wsdlFilePath +,因为+ e,e);
} finally {
try {
in.close();
} catch(异常忽略){
// debug
printOut(关闭WSDL-InputStream时的异常+忽略忽略);
}
}
return doc;
}

然后handleTypes被调用,它将检查文件是否有导入并替换它们

  void handlesTypes(Document wsdlDoc,String wsdlDirPath)throws异常{
元素wsdlElement = wsdlDoc.getDocumentElement();
元素typesEle =(Element)wsdlElement.getElementsByTagNameNS(NS_WSDL,types)。item(0);

NodeList schemas = typesEle.getElementsByTagNameNS(NS_XSD,schema);
printOut(schemas.getLength()+schemas in input wsdl found); (int i = 0; i< schemas.getLength(); i ++){
元素schemaEle =(Element)schemas.item(i);


Schema schemaObj = new Schema(schemaEle,true);
if(isSchemaItselfImported(schemaObj)){
schemaToRemove.add(schemaObj.getXmlElement());

} else {
existingNamespaces.add(schemaObj.getNamespace());
printOut(vorhanden als inline schema war:+ schemaObj.getNamespace());

}

for(Iterator< String> it = schemaObj.getImportedNamespaces()。keySet()。iterator(); it.hasNext();){
String ns = it.next();
if(!schemasToInclude.containsKey(ns)){
String schemaLocation = schemaObj.getImportedNamespaces()。get(ns);
if(schemaLocation == null || schemaLocation.trim()。length()== 0){
throw new RuntimeException(no schemalocation defined for namespace+ ns);
}
String xsdFileWithPath = wsdlDirPath + schemaLocation;
元素importedSchemaEle = readXsd(xsdFileWithPath);
String importedXsdDir = getDirPath(xsdFileWithPath);
readXSDsForImportedSchemas(importedXsdDir,new Schema(importedSchemaEle,true));
printOut(Schema importiert);
}
}
}
printOut(schemasToInclude.size()+所有找到的模式(由wsdl引用或其中一个模式));

for(Element schemaEle:schemaToRemove){
System.out.println(schemaToRemove.size());
typesEle.removeChild(schemaEle);
}



printOut(handleTypes()finished ...);
}


private boolean isSchemaItselfImported(Schema schemaObj){
String nsSchema = schemaObj.getNamespace();
if(schemaObj.getImportedNamespaces()。size()== 1 {
String nsFirstImport = schemaObj.getImportedNamespaces()。keySet()。iterator()。next();
if null == nsSchema
||.equals(nsSchema)|| nsSchema.equals(nsFirstImport)){
return true;
}
}
return false ;
}





元素readXsd(String fileWithPath)throws异常{
printOut(readXsd() >+ fileWithPath);
if(fileWithPath == null || fileWithPath.trim()。length()== 0){
throw new RuntimeException(XSD-Path war leer - Abbruch! ?


DocumentBuilder docBuilder = factory.newDocumentBuilder();
File inputFile = new File(fileWithPath);
if(!inputFile.exists( )){
抛出新的FileNotFoundException(找不到文件+ inputFile.getName()
+',应该已经这里:'
+ inputFile.getCanonicalPath()+');
}

InputStream in = new FileInputStream(fileWithPath);
InputSource is = new InputSource(in);
try {
Document doc = docBuilder.parse(is);
return doc.getDocumentElement();
} catch(Exception e){
throw new Exception(解析文件内容时出错+ inputFile.getCanonicalPath(),e);
} finally {
in.close();
}
}


private void readXSDsForImportedSchemas(String currentXsdDir,Schema schemaObj)
throws异常{
String namespace = schemaObj.getNamespace );

if(schemasToInclude.containsKey(namespace)){
return;
}

schemasToInclude.put(schemaObj.getNamespace(),schemaObj);
if(schemaObj.getImportedNamespaces()。size()> 0){
for(Iterator< String> it = schemaObj.getImportedNamespaces()。keySet()。iterator(); it.hasNext );){
String importedNamespace = it.next();

if(referencesToNamespaceList.containsKey(importedNamespace)){
referencesToNamespaceList.get(importedNamespace).add(namespace);
} else {
列表< String> newNamespaceList = new ArrayList< String>();
newNamespaceList.add(namespace);
referencesToNamespaceList.put(importedNamespace,newNamespaceList);
}
if(!schemasToInclude.containsKey(importedNamespace)){
String schemaLoc = schemaObj.getImportedNamespaces()。get(importedNamespace);
if(schemaLoc == null || schemaLoc.trim()。length()== 0){
throw new RuntimeException(no schemaalocation defined for namespace+ importedNamespace);
}
String importedXsdFileWithPath = currentXsdDir + schemaLoc;
includePfad.add(new File(importedXsdFileWithPath).getCanonicalPath());
元素importedSchemaElement = readXsd(importedXsdFileWithPath);
String importedXsdDir = getDirPath(importedXsdFileWithPath);
readXSDsForImportedSchemas(importedXsdDir,new Schema(importedSchemaElement,true));
includePfad.remove(includePfad.size() - 1);

}

}

}

}

现在我写文件

  void writeWsdlToOutputFile(Document wsdlDoc) throws Exception {
String outputFile = this.outFilename;
System.out.println(write inlined wsdl to:+ outputFile);

TransformerFactory tf = TransformerFactory.newInstance();
源xslt = new StreamSource(new ByteArrayInputStream(XML_XSLT_OUTPUT.getBytes()));
Transformer t = tf.newTransformer(xslt);

FileOutputStream fos = new FileOutputStream(outputFile,false);
t.transform(new DOMSource(wsdlDoc),new StreamResult(fos));
fos.close();
}

问题是当我们在模式中有多个导入的wsdl时,不要在输出文件中删除它们,例如:

 < wsdl:definitions ....> 
< wsdl:types ....>
< xsd:schema ....>
< xsd:import ../& gt
< xsd:import ../& gt
< xsd:import ../& gt
.....

在输出文件中,导入标签仍然存在,即使导入的内容被复制。

解决方案

  1. 检查您的defauntNamespace,targetNamespace和xmlns命名空间值是否相同

  2. 尝试在模式本身中使用复杂的元素类型(一级更高的元素,其中将包括导入的元素),并在定义中给定义在名称空间


    1. I was given the task to find out why this program doesn't work as expected. It takes a WSDL-file and replaces the xsd:imports with the actual content of the importet file and it should delete the import statement. The inline is working but it doesn't delete the import tags?!

      I have the "doWork" method:

       public void doWork(String wsdlFilePath) throws Exception {
          try {
              System.out.println("reading wsdlFile " + wsdlFilePath + "... ");
      
              Document wsdlDoc = readWsdl(wsdlFilePath);
              handlesTypes(wsdlDoc, getDirPath(wsdlFilePath));
              writeWsdlToOutputFile(wsdlDoc);
      
          } catch (Exception e) {...
      

      First it calls readWsdl(...) which will read the given wsdl as a Document

          Document readWsdl(String wsdlFilePath) throws Exception {
          DocumentBuilder docBuilder = factory.newDocumentBuilder();
          printOut("reading wsdl from " + wsdlFilePath);
          InputStream in = null;
          Document doc = null;
      
          try {
              File wsdlFile = new File(wsdlFilePath);
              // this.baseDir = wsdlFile.get getParent()
              in = new FileInputStream(wsdlFile);
              InputSource is = new InputSource(in);
              doc = docBuilder.parse(is);
          } catch (Exception e) {
              // debug
              printOut("Exception while reading WSDL" + e, e);
              throw new RuntimeException("could not read InputFile: " + wsdlFilePath + " because of " + e, e);
          } finally {
              try {
                  in.close();
              } catch (Exception ignore) {
                  // debug
                  printOut("Exception while closing WSDL-InputStream: " + ignore, ignore);
              }
          }
          return doc;
      }
      

      Then handleTypes gets called which will check if the file has imports and replace them

          void handlesTypes(Document wsdlDoc, String wsdlDirPath) throws Exception {
          Element wsdlElement = wsdlDoc.getDocumentElement();
          Element typesEle = (Element) wsdlElement.getElementsByTagNameNS(NS_WSDL, "types").item(0);
      
          NodeList schemas = typesEle.getElementsByTagNameNS(NS_XSD, "schema");
          printOut(schemas.getLength() + " schemas in input wsdl found");
      
          for (int i = 0; i < schemas.getLength(); i++) {
              Element schemaEle = (Element) schemas.item(i);
                          Schema schemaObj = new Schema(schemaEle, true);
              if (isSchemaItselfImported(schemaObj)) {
                  schemaToRemove.add(schemaObj.getXmlElement());
      
              } else {
                                  existingNamespaces.add(schemaObj.getNamespace());
                  printOut("vorhanden als inline schema war: " + schemaObj.getNamespace());
      
              }
      
              for (Iterator<String> it = schemaObj.getImportedNamespaces().keySet().iterator(); it.hasNext();) {
                  String ns = it.next();
                                  if (!schemasToInclude.containsKey(ns)) {
                      String schemaLocation = schemaObj.getImportedNamespaces().get(ns);
                      if (schemaLocation == null || schemaLocation.trim().length() == 0) {
                          throw new RuntimeException("no schemalocation defined for namespace " + ns);
                      }
                      String xsdFileWithPath = wsdlDirPath + schemaLocation;
                      Element importedSchemaEle = readXsd(xsdFileWithPath);
                      String importedXsdDir = getDirPath(xsdFileWithPath);
                      readXSDsForImportedSchemas(importedXsdDir, new Schema(importedSchemaEle, true));
                      printOut("Schema importiert");
                  }
              }
          }
          printOut(schemasToInclude.size() + " schemas over all found (referenced by wsdl or one of the schemas)");
      
          for (Element schemaEle : schemaToRemove) {
              System.out.println(schemaToRemove.size());
              typesEle.removeChild(schemaEle);
          }
      
      
      
          printOut("handleTypes() finished...");
      }
      
      
      private boolean isSchemaItselfImported(Schema schemaObj) {
          String nsSchema = schemaObj.getNamespace();
          if (schemaObj.getImportedNamespaces().size() == 1        {
              String nsFirstImport = schemaObj.getImportedNamespaces().keySet().iterator().next();
              if (null == nsSchema
                      || "".equals(nsSchema)                    || nsSchema.equals(nsFirstImport))            {
                  return true;
              }
          }
          return false;
      }
      
      
      
      
      
      Element readXsd(String fileWithPath) throws Exception {
          printOut("readXsd() -> " + fileWithPath);
          if (fileWithPath == null || fileWithPath.trim().length() == 0) {
              throw new RuntimeException("XSD-Path war leer - Abbruch !?");
          }
      
          DocumentBuilder docBuilder = factory.newDocumentBuilder();
          File inputFile = new File(fileWithPath);
          if (!inputFile.exists()) {
              throw new FileNotFoundException("Failed to find file '" + inputFile.getName()
                      + "', should have been here: '"
                      + inputFile.getCanonicalPath() + "'");
          }
      
          InputStream in = new FileInputStream(fileWithPath);
          InputSource is = new InputSource(in);
          try {
              Document doc = docBuilder.parse(is);
              return doc.getDocumentElement();
          } catch (Exception e) {
              throw new Exception("Error parsing content of file: " + inputFile.getCanonicalPath(), e);
          } finally {
              in.close();
          }
      }
      
      
      private void readXSDsForImportedSchemas(String currentXsdDir, Schema schemaObj)
              throws Exception {
          String namespace = schemaObj.getNamespace();
      
          if (schemasToInclude.containsKey(namespace)) {
                          return;
          }
      
          schemasToInclude.put(schemaObj.getNamespace(), schemaObj);
          if (schemaObj.getImportedNamespaces().size() > 0) {
              for (Iterator<String> it = schemaObj.getImportedNamespaces().keySet().iterator(); it.hasNext();) {
                  String importedNamespace = it.next();
      
                  if (referencesToNamespaceList.containsKey(importedNamespace)) {
                      referencesToNamespaceList.get(importedNamespace).add(namespace);
                  } else {
                      List<String> newNamespaceList = new ArrayList<String>();
                      newNamespaceList.add(namespace);
                      referencesToNamespaceList.put(importedNamespace, newNamespaceList);
                  }
                                  if (!schemasToInclude.containsKey(importedNamespace)) {
                      String schemaLoc = schemaObj.getImportedNamespaces().get(importedNamespace);
                      if (schemaLoc == null || schemaLoc.trim().length() == 0) {
                          throw new RuntimeException("no schemalocation defined for namespace " + importedNamespace);
                      }
                      String importedXsdFileWithPath = currentXsdDir + schemaLoc;
                      includePfad.add(new File(importedXsdFileWithPath).getCanonicalPath());
                      Element importedSchemaElement = readXsd(importedXsdFileWithPath);
                      String importedXsdDir = getDirPath(importedXsdFileWithPath);
                      readXSDsForImportedSchemas(importedXsdDir, new Schema(importedSchemaElement, true));
                      includePfad.remove(includePfad.size() - 1);
      
                  }
      
              }
      
          }
      
      }
      

      Now I write the file

       void writeWsdlToOutputFile(Document wsdlDoc) throws Exception {
              String outputFile = this.outFilename;
              System.out.println("writing inlined wsdl to: " + outputFile);
      
              TransformerFactory tf = TransformerFactory.newInstance();
              Source xslt = new StreamSource(new ByteArrayInputStream(XML_XSLT_OUTPUT.getBytes()));
              Transformer t = tf.newTransformer(xslt);
      
          FileOutputStream fos = new FileOutputStream(outputFile, false);
          t.transform(new DOMSource(wsdlDoc), new StreamResult(fos));
          fos.close();
      }
      

      The Problem is when I us a wsdl with multiple imports in the schema it will not delete them in the outputfile for example:

      <wsdl:definitions ....>
        <wsdl:types....>
          <xsd:schema....>
            <xsd:import../>
            <xsd:import../>
            <xsd:import../>
        .....
      

      In the Outputfile the import tags are still there even though the content of the import got copied.

      解决方案
      1. Check your defauntNamespace, targetNamespace and xmlns namespace values are identical.
      2. Try using complex element types in schema itself (one level higher element that will include elements from import) and give the imports in definition under namespace

      这篇关于WSDL Inliner不按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:12