Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4d5a95c220ad837ebada4d1d234631cff015059f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.core.internal.archetype;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLFilterImpl;

import org.eclipse.osgi.util.NLS;

import org.eclipse.m2e.core.internal.Messages;
import org.eclipse.m2e.core.internal.archetype.ArchetypeCatalogFactory.LocalCatalogFactory;
import org.eclipse.m2e.core.internal.archetype.ArchetypeCatalogFactory.RemoteCatalogFactory;


/**
 * Archetype catalogs writer
 * 
 * @author Eugene Kuleshov
 */
public class ArchetypeCatalogsWriter {
  private static final Logger log = LoggerFactory.getLogger(ArchetypeCatalogsWriter.class);

  private static final String ELEMENT_CATALOGS = "archetypeCatalogs"; //$NON-NLS-1$

  private static final String ELEMENT_CATALOG = "catalog"; //$NON-NLS-1$

  private static final String ATT_CATALOG_TYPE = "type"; //$NON-NLS-1$

  private static final String ATT_CATALOG_LOCATION = "location"; //$NON-NLS-1$

  public static final String ATT_CATALOG_DESCRIPTION = "description"; //$NON-NLS-1$

  private static final String TYPE_LOCAL = "local"; //$NON-NLS-1$

  private static final String TYPE_REMOTE = "remote"; //$NON-NLS-1$

  private static final String TYPE_SYSTEM = "system"; //$NON-NLS-1$

  public static final String ATT_CATALOG_ID = "id";

  public static final String ATT_CATALOG_ENABLED = "enabled";

  public Collection<ArchetypeCatalogFactory> readArchetypeCatalogs(InputStream is,
      Map<String, ArchetypeCatalogFactory> existingCatalogs) throws IOException {
    Collection<ArchetypeCatalogFactory> catalogs = new ArrayList<ArchetypeCatalogFactory>();
    try {
      SAXParserFactory parserFactory = SAXParserFactory.newInstance();
      SAXParser parser = parserFactory.newSAXParser();
      parser.parse(is, new ArchetypeCatalogsContentHandler(catalogs, existingCatalogs));
    } catch(SAXException ex) {
      String msg = Messages.ArchetypeCatalogsWriter_error_parse;
      log.error(msg, ex);
      throw new IOException(NLS.bind(msg, ex.getMessage()));
    } catch(ParserConfigurationException ex) {
      String msg = Messages.ArchetypeCatalogsWriter_error_parse;
      log.error(msg, ex);
      throw new IOException(NLS.bind(msg, ex.getMessage()));
    }
    return catalogs;
  }

  public void writeArchetypeCatalogs(final Collection<ArchetypeCatalogFactory> catalogs, OutputStream os)
      throws IOException {
    try {
      Transformer transformer = TransformerFactory.newInstance().newTransformer();
      transformer.transform(new SAXSource(new XMLArchetypeCatalogsWriter(catalogs), new InputSource()),
          new StreamResult(os));

    } catch(TransformerFactoryConfigurationError ex) {
      throw new IOException(NLS.bind(Messages.ArchetypeCatalogsWriter_error_write, ex.getMessage()));

    } catch(TransformerException ex) {
      throw new IOException(NLS.bind(Messages.ArchetypeCatalogsWriter_error_write, ex.getMessage()));

    }
  }

  static class XMLArchetypeCatalogsWriter extends XMLFilterImpl {

    private final Collection<ArchetypeCatalogFactory> catalogs;

    public XMLArchetypeCatalogsWriter(Collection<ArchetypeCatalogFactory> catalogs) {
      this.catalogs = catalogs;
    }

    public void parse(InputSource input) throws SAXException {
      ContentHandler handler = getContentHandler();
      handler.startDocument();
      handler.startElement(null, ELEMENT_CATALOGS, ELEMENT_CATALOGS, new AttributesImpl());

      for(ArchetypeCatalogFactory factory : this.catalogs) {
        AttributesImpl attrs = new AttributesImpl();
        if(factory.isEditable()) {
          if(factory instanceof LocalCatalogFactory) {
            attrs.addAttribute(null, ATT_CATALOG_TYPE, ATT_CATALOG_TYPE, null, TYPE_LOCAL);
            attrs.addAttribute(null, ATT_CATALOG_LOCATION, ATT_CATALOG_LOCATION, null, factory.getId());
            attrs.addAttribute(null, ATT_CATALOG_DESCRIPTION, ATT_CATALOG_DESCRIPTION, null, factory.getDescription());
          } else if(factory instanceof RemoteCatalogFactory) {
            attrs.addAttribute(null, ATT_CATALOG_TYPE, ATT_CATALOG_TYPE, null, TYPE_REMOTE);
            attrs.addAttribute(null, ATT_CATALOG_LOCATION, ATT_CATALOG_LOCATION, null, factory.getId());
            attrs.addAttribute(null, ATT_CATALOG_DESCRIPTION, ATT_CATALOG_DESCRIPTION, null, factory.getDescription());
          }
        } else {
          attrs.addAttribute(null, ATT_CATALOG_TYPE, ATT_CATALOG_TYPE, null, TYPE_SYSTEM);
          attrs.addAttribute(null, ATT_CATALOG_ID, ATT_CATALOG_ID, null, factory.getId());
        }
        attrs.addAttribute(null, ATT_CATALOG_ENABLED, ATT_CATALOG_ENABLED, null, Boolean.toString(factory.isEnabled()));
        handler.startElement(null, ELEMENT_CATALOG, ELEMENT_CATALOG, attrs);
        handler.endElement(null, ELEMENT_CATALOG, ELEMENT_CATALOG);
      }

      handler.endElement(null, ELEMENT_CATALOGS, ELEMENT_CATALOGS);
      handler.endDocument();
    }
  }

  static class ArchetypeCatalogsContentHandler extends DefaultHandler {

    private Collection<ArchetypeCatalogFactory> catalogs;

    private Map<String, ArchetypeCatalogFactory> existingCatalogs;

    public ArchetypeCatalogsContentHandler(Collection<ArchetypeCatalogFactory> catalogs,
        Map<String, ArchetypeCatalogFactory> existingCatalogs) {
      this.catalogs = catalogs;
      this.existingCatalogs = existingCatalogs == null ? Collections.emptyMap() : existingCatalogs;
    }

    public void startElement(String uri, String localName, String qName, Attributes attributes) {
      if(ELEMENT_CATALOG.equals(qName) && attributes != null) {
        String type = attributes.getValue(ATT_CATALOG_TYPE);
        String enabledStr = attributes.getValue(ATT_CATALOG_ENABLED);
        boolean enabled = enabledStr==null||Boolean.parseBoolean(enabledStr);
        if(TYPE_LOCAL.equals(type)) {
          String path = attributes.getValue(ATT_CATALOG_LOCATION);
          if(path != null) {
            String description = attributes.getValue(ATT_CATALOG_DESCRIPTION);
            catalogs.add(new LocalCatalogFactory(path, description, true, enabled));
          }
        } else if(TYPE_REMOTE.equals(type)) {
          String url = attributes.getValue(ATT_CATALOG_LOCATION);
          if(url != null) {
            String description = attributes.getValue(ATT_CATALOG_DESCRIPTION);
            catalogs.add(new RemoteCatalogFactory(url, description, true, enabled));
          }
        } else {
          String id = attributes.getValue(ATT_CATALOG_ID);
          if(id != null && !id.isEmpty()) {
            ArchetypeCatalogFactory catalog = existingCatalogs.get(id);
            if(catalog != null) {
              catalog.setEnabled(enabled);
            }
          }
        }
      }
    }

  }

}

Back to the top