/*
   * Analyze the schema at the given URI, the schema is parsed and stored in
   * XMLObjectInfos.
   */
  private void analyzeXSD(URI uri) {

    uri = uri.normalize();

    // already seen this xsd, skip it
    if (xmlObjectInfos.containsKey(uri.toString())) return;

    XSDSchema xsdSchema = XSDSchemaImpl.getSchemaForSchema(uri.toString());

    String encoding = null;
    // if schema is not cached, parse it
    if (xsdSchema == null) {
      XSDParser p = new XSDParser(null);
      InputStream is = NetUtils.getURLInputStream(uri.toString());

      if (is != null) {
        p.parse(is);
        xsdSchema = p.getSchema();
        encoding = p.getEncoding();
      }
    } else encoding = xsdSchema.getDocument().getXmlEncoding();

    if (xsdSchema != null) {

      if (encoding == null) encoding = "UTF-8";

      XMLObjectInfo info = new XMLObjectInfo(new Path(uri.getPath()), xsdSchema, encoding);
      xmlObjectInfos.put(uri.toString(), info);
      updatePathPrefix(info);

      // analyze its imports and includes
      analyzeXSD(uri, xsdSchema);
    }
  }
  public String getXSDSchemaAsString() throws Exception {

    if (xsdSchema == null) {
      return ""; //$NON-NLS-1$
    }

    Document document = xsdSchema.getDocument();
    String schema = Util.nodeToString(document);

    // FIXES a bug in the XSD library that puts nillable attributes in
    // elements which are ref
    // That is illegal according to W3C §3.3.3 / 2.2 (and Xerces)
    Pattern p =
        Pattern.compile(
            "(<([a-z]+:)?element.*?)nillable=['|\"].*?['|\"](.*?ref=.*?>)"); //$NON-NLS-1$
    schema = p.matcher(schema).replaceAll("$1 $3"); // $NON-NLS-1$

    return schema;
  }
  /*
   * Writes the xsd schema to the file at the given path, relative to the target folder.
   */
  private String writeXMLObj(
      IPath path, XSDSchema xsdSchema, String encoding, IProgressMonitor monitor)
      throws TransformerConfigurationException, TransformerException, URIException, IOException,
          CoreException {
    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    Document document = xsdSchema.getDocument();
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.ENCODING, encoding);
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

    Element e = xsdSchema.getElement();
    DOMSource domSource = new DOMSource(e);

    String[] targetURI = appendPathToURI(targetFolderURI, path);

    OutputStream os = uriFactory.newURI(targetURI[0]).getOutputStream();
    serializer.transform(domSource, new StreamResult(os));
    os.close();

    return targetURI[1];
  }
Exemplo n.º 4
0
 protected void afterDoAction() {
   if (!omitTrack) {
     commitDocumentToCurrent(schema.getDocument());
   }
 }
Exemplo n.º 5
0
 protected void beforeDoAction() {
   schema = ((ISchemaContentProvider) page.getTreeViewer().getContentProvider()).getXsdSchema();
   if (!omitTrack) {
     commitDocumentToHistory(schema.getDocument());
   }
 }