/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
 *
 * Licensed 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.jkiss.dbeaver.registry.updater;

import org.jkiss.dbeaver.runtime.WebUtils;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.xml.XMLException;
import org.jkiss.utils.xml.XMLUtils;
import org.osgi.framework.Version;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Version descriptor
 */
public class VersionDescriptor {

    private String programName;
    private Version programVersion;
    private String updateTime;
    private String baseURL;
    private String releaseNotes;

    private final List<DistributionDescriptor> distributions = new ArrayList<>();
    private final List<UpdateSiteDescriptor> updateSites = new ArrayList<>();

    public VersionDescriptor(String fileAddr)
        throws IOException
    {
        try (InputStream inputStream = WebUtils.openConnection(fileAddr).getInputStream()) {
            Document document = XMLUtils.parseDocument(inputStream);
            parseVersionInfo(document);
        } catch (XMLException e) {
            throw new IOException("XML parse error", e);
        }
    }

    public String getProgramName()
    {
        return programName;
    }

    public Version getProgramVersion()
    {
        return programVersion;
    }

    public String getUpdateTime()
    {
        return updateTime;
    }

    public String getBaseURL()
    {
        return baseURL;
    }

    public String getReleaseNotes()
    {
        return releaseNotes;
    }

    public Collection<DistributionDescriptor> getDistributions()
    {
        return distributions;
    }

    public Collection<UpdateSiteDescriptor> getUpdateSites()
    {
        return updateSites;
    }

    private void parseVersionInfo(Document document)
    {
        Element root = document.getDocumentElement();
        programName = XMLUtils.getChildElementBody(root, "name");
        programVersion = Version.parseVersion(XMLUtils.getChildElementBody(root, "number"));
        updateTime = XMLUtils.getChildElementBody(root, "date");
        baseURL = XMLUtils.getChildElementBody(root, "base-url");
        Element releaseNotesElem = XMLUtils.getChildElement(root, "release-notes");
        this.releaseNotes = releaseNotesElem == null ? "" : CommonUtils.toString(releaseNotesElem.getTextContent()).trim();

        for (Element dist : XMLUtils.getChildElementList(root, "distribution")) {
            distributions.add(new DistributionDescriptor(dist));
        }

        for (Element dist : XMLUtils.getChildElementList(root, "site")) {
            updateSites.add(new UpdateSiteDescriptor(dist));
        }
    }
}

@@ -787,27 +787,31 @@
 s.utils.
-CommonUtils
+xml.SAXListener
 ;%0D%0Aimpor
@@ -832,28 +832,25 @@
 ils.xml.
-XMLException
+SAXReader
 ;%0D%0Aimpor
@@ -874,21 +874,25 @@
 .xml.XML
-Utils
+Exception
 ;%0D%0Aimpor
@@ -937,53 +937,26 @@
 org.
-w3c.dom.Document;%0D%0Aimport org.w3c.dom.Element
+xml.sax.Attributes
 ;%0D%0A%0D
@@ -1691,103 +1691,36 @@
     
-Document document = XMLUtils.parseDocument(inputStream);%0D%0A            parseVersionInfo(document
+parseVersionInfo(inputStream
 );%0D%0A
@@ -2495,742 +2495,1203 @@
 nfo(
-Document document)%0D%0A    %7B%0D%0A        Element root = document.getDocumentElement();%0D%0A        programName = XMLUtils.getChildElementBody(root, %22name%22);%0D%0A        programVersion = Version.parseVersion(XMLUtils.getChildElementBody(root, %22number%22));%0D%0A        updateTime = XMLUtils.getChildElementBody(root, %22date%22);%0D%0A        baseURL = XMLUtils.getChildElementBody(root, %22base-url%22);%0D%0A        Element releaseNotesElem = XMLUtils.getChildElement(root, %22release-notes%22);%0D%0A        this.releaseNotes = releaseNotesElem == null ? %22%22 : CommonUtils.toString(releaseNotesElem.getTextC
+InputStream inputStream) throws IOException, XMLException %7B%0D%0A        SAXReader parser = new SAXReader(inputStream);%0D%0A        SAXListener dsp = new SAXListener() %7B%0D%0A            private String lastTag;%0D%0A            private StringBuilder textBuffer = new StringBuilder();%0D%0A            @Override%0D%0A            public void saxStartElement(SAXReader reader, String namespaceURI, String localName, Attributes atts) throws XMLException %7B%0D%0A                lastTag = localName;%0D%0A                textBuffer.setLength(0);%0D%0A            %7D%0D%0A            @Override%0D%0A            public void saxText(SAXReader reader, String data) throws XMLException %7B%0D%0A                textBuffer.append(data);%0D%0A            %7D%0D%0A            @Override%0D%0A            public void saxEndElement(SAXReader reader, String namespaceURI, String localName) throws XMLException %7B%0D%0A                final String text = textBuffer.toString();%0D%0A                switch (lastTag) %7B%0D%0A                    case %22name%22: programName = text; break;%0D%0A                    case %22number%22: programVersion = Version.parseVersi
 on
+(
 te
-nt()).trim();%0D%0A%0D%0A        for (Element dist : XMLUtils.getChildElementList(root, %22distribution%22)) %7B%0D%0A            distributions.add(new DistributionDescriptor(dist))
+xt); break;%0D%0A                    case %22date%22: updateTime = text; break;%0D%0A                    case %22base-url%22: baseURL = text; break
 ;%0D%0A 
@@ -3701,21 +3701,19 @@
     
-%7D%0D%0A%0D%0A
+   
         
 for 
@@ -3712,133 +3712,117 @@
     
-for (Element dist : XMLUtils.getChildElementList(root, %22site%22)) %7B%0D%0A            updateSites.add(new UpdateSiteDescriptor(dist)
+ case %22release-notes%22: releaseNotes = text; break;%0D%0A                %7D%0D%0A                textBuffer.setLength(0
 );%0D%0A
@@ -3829,21 +3829,67 @@
         
+    
 %7D%0D%0A    
+  
-%7D
+  %7D;%0D%0A        parser.parse(dsp);%0D%0A    %7D%0D%0A
 %0D%0A%7D%0D%0A

Version info reader fix (use sax parser)