/src/main/java/com/sforce/ws/wsdl/WsdlFactory.java

https://github.com/morrisonsplc/salesforce-connector · Java · 81 lines · 37 code · 7 blank · 37 comment · 0 complexity · efe7837f4b7bc946f19fd95c6d3f2689 MD5 · raw file

  1. /*
  2. * Copyright (c) 2005, salesforce.com, inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification, are permitted provided
  6. * that the following conditions are met:
  7. *
  8. * Redistributions of source code must retain the above copyright notice, this list of conditions and the
  9. * following disclaimer.
  10. *
  11. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
  12. * the following disclaimer in the documentation and/or other materials provided with the distribution.
  13. *
  14. * Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or
  15. * promote products derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  19. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  20. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  21. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. package com.sforce.ws.wsdl;
  27. import com.sforce.ws.parser.XmlInputStream;
  28. import java.net.URL;
  29. import java.net.MalformedURLException;
  30. import java.io.InputStream;
  31. import java.io.IOException;
  32. import java.io.FileInputStream;
  33. import java.io.ByteArrayInputStream;
  34. /**
  35. * Use this factory to create WSDL data model.
  36. *
  37. * @author http://cheenath.com
  38. * @version 1.0
  39. * @since 1.0 Nov 5, 2005
  40. */
  41. public class WsdlFactory {
  42. /**
  43. * @param url url of the wsdl
  44. * @return parsed definitions
  45. * @throws WsdlParseException failed to parse wsdl
  46. */
  47. public static Definitions create(String url) throws WsdlParseException {
  48. InputStream in;
  49. try {
  50. try {
  51. in = new URL(url).openStream();
  52. } catch (MalformedURLException e) {
  53. in = new FileInputStream(url);
  54. }
  55. }catch (IOException e) {
  56. throw new WsdlParseException(e);
  57. }
  58. XmlInputStream parser = new XmlInputStream();
  59. WsdlParser wsdlParser = new WsdlParser(parser);
  60. Definitions definitions = new Definitions();
  61. wsdlParser.setInput(in, "UTF-8");
  62. definitions.read(wsdlParser);
  63. return definitions;
  64. }
  65. public static Definitions createFromString(String wsdl) throws WsdlParseException {
  66. XmlInputStream parser = new XmlInputStream();
  67. WsdlParser wsdlParser = new WsdlParser(parser);
  68. Definitions definitions = new Definitions();
  69. ByteArrayInputStream bio = new ByteArrayInputStream(wsdl.getBytes());
  70. wsdlParser.setInput(bio, "UTF-8");
  71. definitions.read(wsdlParser);
  72. return definitions;
  73. }
  74. }