⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmldocumentutils.java

📁 sun公司的petstore,一个J2ee的示例应用程序.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  public static void appendChild(Document document, Node root, String nsURI, String name, String value) {        Node node = document.createElementNS(nsURI, name);        node.appendChild(document.createTextNode(value != null ? value : ""));        root.appendChild(node);        return;  }  public static void appendChild(Document document, Node root, String nsURI, String name, long value) {        appendChild(document, root, nsURI, name, Long.toString(value));        return;  }  public static void appendChild(Document document, Node root, String nsURI, String name, float value) {        appendChild(document, root, nsURI, name, Float.toString(value));        return;  }  public static void appendChild(Node root, String name, String nsURI, String value) {        appendChild(root.getOwnerDocument(), root, nsURI, name, value);        return;  }  public static void serialize(Transformer transformer, Document document, String dtdPublicId,                               String dtdSystemId, boolean xsdSupport, String encoding, Result result)        throws XMLDocumentException {      try {            transformer.setOutputProperty(OutputKeys.METHOD, "xml");            if (!xsdSupport) {          if (dtdSystemId != null) {                    transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dtdSystemId);          }          transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, dtdPublicId);            } /*else {            Element root = document.getDocumentElement();            root.setAttributeNS(W3C_XML_SCHEMA_INSTANCE_NS, W3C_XML_SCHEMA_LOCATION_QNAME, dtdPublicId + " " + dtdSystemId);            }*/            transformer.setOutputProperty(OutputKeys.ENCODING, encoding);            transformer.setOutputProperty(OutputKeys.INDENT, "yes");            transformer.transform(new DOMSource(document), result);      } catch (Exception exception) {            exception.printStackTrace(System.err);            throw new XMLDocumentException(exception);      }      return;  }  public static void toXML(Document document, String dtdPublicId, String dtdSystemId, String encoding, Result result)        throws XMLDocumentException {      serialize(createTransformer(), document, dtdPublicId, dtdSystemId, false, encoding, result);      return;  }  public static void toXML(Document document, String dtdPublicId, String dtdSystemId, Result result)        throws XMLDocumentException {      toXML(document, dtdPublicId, dtdSystemId, DEFAULT_ENCODING, result);      return;  }  public static void toXML(Document document, String dtdPublicId, URL entityCatalogURL, Result result)        throws XMLDocumentException {      toXML(document, dtdPublicId, entityCatalogURL, DEFAULT_ENCODING, result);      return;  }  public static void toXML(Document document, String dtdPublicId, URL entityCatalogURL, String encoding, Result result)        throws XMLDocumentException {      try {            CustomEntityResolver entityResolver = new CustomEntityResolver(entityCatalogURL);            String dtdSystemId = entityResolver.mapEntityURI(dtdPublicId);            toXML(document, dtdPublicId, dtdSystemId, encoding, result);      } catch (Exception exception) {            exception.printStackTrace(System.err);            throw new XMLDocumentException(exception);      }      return;  }  public static void toXML(Document document, String dtdPublicId, URL entityCatalogURL, boolean xsdSupport, Result result)        throws XMLDocumentException {      toXML(document, dtdPublicId, entityCatalogURL, xsdSupport, DEFAULT_ENCODING, result);      return;  }  public static void toXML(Document document, String dtdPublicId, URL entityCatalogURL, boolean xsdSupport, String encoding, Result result)        throws XMLDocumentException {      try {            CustomEntityResolver entityResolver = new CustomEntityResolver(entityCatalogURL);            String dtdSystemId = entityResolver.mapEntityURI(dtdPublicId);            serialize(createTransformer(), document, dtdPublicId, dtdSystemId, xsdSupport,  encoding, result);      } catch (Exception exception) {            exception.printStackTrace(System.err);            throw new XMLDocumentException(exception);      }      return;  }  public static Document deserialize(Transformer transformer, Source source,                                     String dtdPublicId, final URL entityCatalogURL, boolean validating, boolean xsdSupport)        throws XMLDocumentException {      Node node;      if (source instanceof DOMSource) {            node = ((DOMSource) source).getNode();      } else {            node = transform(transformer, source, dtdPublicId, entityCatalogURL, validating, xsdSupport);      }      Document document;      if (node != null && node instanceof Document) {            document = (Document) node;      } else {            throw new XMLDocumentException("Document node required.");      }      if (!xsdSupport) {            if (!XMLDocumentUtils.checkDocumentType(document, dtdPublicId)) {          throw new XMLDocumentException("Document not of type: " + dtdPublicId);            }      }      return document;  }  public static Document transform(Transformer transformer, Source source,                                   String dtdPublicId, URL entityCatalogURL, boolean validating, boolean xsdSupport)        throws XMLDocumentException {      DOMResult result = new DOMResult();      transform(transformer, source, result, dtdPublicId, entityCatalogURL, validating, xsdSupport);      return (Document) result.getNode();  }    public static void transform(Transformer transformer, Source source, Result result,                                     String dtdPublicId, URL entityCatalogURL, boolean validating, boolean xsdSupport)        throws XMLDocumentException {      try {            if (!(source instanceof DOMSource)) {          if (source.getSystemId() == null) { // Set the base URI to resolve relative URI                    source.setSystemId(SCHEMAS_DIRECTORY_PATH);          }          CustomEntityResolver entityResolver = new CustomEntityResolver(entityCatalogURL);          SAXSource saxSource = null;          if (source instanceof StreamSource) {                    InputSource inputSource = SAXSource.sourceToInputSource(source);                    if (inputSource == null) {              throw new XMLDocumentException("Can't convert this source.");                    }                    saxSource = new SAXSource(inputSource);          } else if (source instanceof SAXSource) {                    saxSource = (SAXSource) source;          }          XMLReader reader = saxSource.getXMLReader();          if (reader == null) {            reader = createParser(validating, xsdSupport, entityResolver, dtdPublicId).getXMLReader();                    saxSource.setXMLReader(reader);          }          source = saxSource;            }            transformer.transform(source, result);            return;      } catch (Exception exception) {            exception.printStackTrace(System.err);            throw new XMLDocumentException(exception);      }  }  public static Document fromXML(Source source, String dtdPublicId, final URL entityCatalogURL, boolean validating)        throws XMLDocumentException {      return deserialize(createTransformer(), source, dtdPublicId, entityCatalogURL, validating, false);  }  public static Document fromXML(InputSource source, String dtdPublicId, final URL entityCatalogURL, boolean validating)        throws XMLDocumentException {      Document document;      try {            if (source.getSystemId() == null) { // Set the base URI to resolve relative URI          source.setSystemId(SCHEMAS_DIRECTORY_PATH);            }            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();            builderFactory.setValidating(validating);            DocumentBuilder builder = builderFactory.newDocumentBuilder();            builder.setErrorHandler(new ErrorHandler() {          public void warning(SAXParseException exception) {                        System.err.println("[Warning]: " + exception.getMessage());                        return;          }          public void error(SAXParseException exception) {                        System.err.println("[Error]: " + exception.getMessage());                        return;          }          public void fatalError(SAXParseException exception) throws SAXException {                        System.err.println("[Fatal Error]: " + exception.getMessage());                        throw exception;          }                });            builder.setEntityResolver(new CustomEntityResolver(entityCatalogURL));            document = builder.parse(source);      } catch (Exception exception) {            throw new XMLDocumentException(exception);      }      if (!validating || XMLDocumentUtils.checkDocumentType(document, dtdPublicId)) {            return document;      }      throw new XMLDocumentException("Document not of type: " + dtdPublicId);  }  public static DocumentBuilder createDocumentBuilder() throws XMLDocumentException {        try {      DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();      builderFactory.setNamespaceAware(true);      DocumentBuilder builder = builderFactory.newDocumentBuilder();      return builder;        } catch (Exception exception) {      exception.printStackTrace(System.err);      throw new XMLDocumentException(exception);        }  }  public static SAXParser createParser(boolean validating, boolean xsdSupport, URL entityCatalogURL, String schemaURI)    throws XMLDocumentException {      return createParser(validating, xsdSupport, new CustomEntityResolver(entityCatalogURL), schemaURI);  }  public static SAXParser createParser(boolean validating, boolean xsdSupport,                                       CustomEntityResolver entityResolver, String schemaURI)    throws XMLDocumentException {        try {      SAXParserFactory parserFactory = SAXParserFactory.newInstance();      parserFactory.setValidating(validating);      parserFactory.setNamespaceAware(true);      SAXParser parser = parserFactory.newSAXParser();      if (xsdSupport) {        try {          parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);        } catch(SAXNotRecognizedException exception) {          System.err.println(exception);        }        try {          parser.setProperty(JAXP_SCHEMA_SOURCE, entityResolver.mapEntityURI(schemaURI));        } catch(SAXNotRecognizedException exception) {          System.err.println(exception);        }      }      XMLReader reader = parser.getXMLReader();      try {        reader.setFeature(SAX_NS_PREFIXES, true);      } catch (SAXException exception) {}      reader.setErrorHandler(new ErrorHandler() {        public void warning(SAXParseException exception) {          System.err.println("[Warning]: " + exception.getMessage());          return;        }        public void error(SAXParseException exception) {          System.err.println("[Error]: " + exception.getMessage());          return;        }        public void fatalError(SAXParseException exception) throws SAXException {          System.err.println("[Fatal Error]: " + exception.getMessage());          throw exception;        }      });      reader.setEntityResolver(entityResolver);      return parser;        } catch (Exception exception) {      exception.printStackTrace(System.err);      throw new XMLDocumentException(exception);        }  }  public static Document createDocument() throws XMLDocumentException {        return createDocumentBuilder().newDocument();  }  public static Transformer createTransformer() throws XMLDocumentException {        try {      TransformerFactory transformerFactory = TransformerFactory.newInstance();      return transformerFactory.newTransformer();        } catch (Exception exception) {      exception.printStackTrace(System.err);      throw new XMLDocumentException(exception);        }  }  public static boolean checkDocumentType(Document document, String dtdPublicId) {        DocumentType documentType = document.getDoctype();        if (documentType != null) {      String publicId = documentType.getPublicId();      return publicId != null && publicId.equals(dtdPublicId);        }        // System.err.println("Can't check document type: " + dtdPublicId);        return true; // false; Due to problem of IdentityTransformer not creating the DocType nodes  }  /**   * Convert a string based locale into a Locale Object   * <br>   * <br>Strings are formatted:   * <br>   * <br>language_contry_variant   *   **/  public static Locale getLocaleFromString(String localeString) {        if (localeString == null) {      return null;        }        if (localeString.toLowerCase().equals("default")) {      return Locale.getDefault();        }        int languageIndex = localeString.indexOf('_');        if (languageIndex  == -1) {      return null;        }        int countryIndex = localeString.indexOf('_', languageIndex +1);        String country = null;        if (countryIndex  == -1) {      if (localeString.length() > languageIndex) {                country = localeString.substring(languageIndex +1, localeString.length());      } else {                return null;      }        }        int variantIndex = -1;        if (countryIndex != -1) {      countryIndex = localeString.indexOf('_', countryIndex +1);        }        String language = localeString.substring(0, languageIndex);        String variant = null;        if (variantIndex  != -1) {      variant = localeString.substring(variantIndex +1, localeString.length());        }        if (variant != null) {      return new Locale(language, country, variant);        } else {      return new Locale(language, country);        }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -