Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.obr.plugin; |
| 20 | |
| 21 | |
| 22 | import java.io.File; |
| 23 | import java.io.FileNotFoundException; |
| 24 | import java.io.FileOutputStream; |
| 25 | import java.io.IOException; |
| 26 | import java.net.URI; |
| 27 | import java.text.SimpleDateFormat; |
| 28 | import java.util.Date; |
| 29 | import java.util.Properties; |
| 30 | |
| 31 | import javax.xml.parsers.DocumentBuilder; |
| 32 | import javax.xml.parsers.DocumentBuilderFactory; |
| 33 | import javax.xml.parsers.ParserConfigurationException; |
| 34 | import javax.xml.transform.Result; |
| 35 | import javax.xml.transform.Transformer; |
| 36 | import javax.xml.transform.TransformerConfigurationException; |
| 37 | import javax.xml.transform.TransformerException; |
| 38 | import javax.xml.transform.TransformerFactory; |
| 39 | import javax.xml.transform.dom.DOMSource; |
| 40 | import javax.xml.transform.stream.StreamResult; |
| 41 | |
| 42 | import org.apache.maven.plugin.MojoExecutionException; |
| 43 | import org.apache.maven.plugin.logging.Log; |
| 44 | import org.apache.maven.project.MavenProject; |
| 45 | import org.w3c.dom.Document; |
| 46 | import org.w3c.dom.Element; |
| 47 | import org.w3c.dom.NamedNodeMap; |
| 48 | import org.w3c.dom.Node; |
| 49 | import org.w3c.dom.NodeList; |
| 50 | import org.xml.sax.SAXException; |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * this class parse the old repository.xml file build the bundle resource description and update the repository. |
| 55 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 56 | */ |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 57 | public class ObrUpdate |
| 58 | { |
| 59 | /** |
| 60 | * generate the date format to insert it in repository descriptor file. |
| 61 | */ |
| 62 | static SimpleDateFormat m_format = new SimpleDateFormat( "yyyyMMddHHmmss.SSS" ); |
| 63 | |
| 64 | /** |
| 65 | * logger for this plugin. |
| 66 | */ |
| 67 | private Log m_logger; |
| 68 | |
| 69 | /** |
| 70 | * name and path to the repository descriptor file. |
| 71 | */ |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 72 | private URI m_repositoryXml; |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * name and path to the obr.xml file. |
| 76 | */ |
Stuart McCulloch | 80a8734 | 2008-01-27 11:04:46 +0000 | [diff] [blame] | 77 | private URI m_obrXml; |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * name and path to the bundle jar file. |
| 81 | */ |
Stuart McCulloch | 37a882e | 2008-01-28 08:26:29 +0000 | [diff] [blame] | 82 | private URI m_bundleJar; |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * maven project description. |
| 86 | */ |
| 87 | private MavenProject m_project; |
| 88 | |
| 89 | /** |
| 90 | * user configuration information. |
| 91 | */ |
| 92 | private Config m_userConfig; |
| 93 | |
| 94 | /** |
| 95 | * used to build another xml document. |
| 96 | */ |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 97 | private DocumentBuilder m_documentBuilder; |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * root on parent document. |
| 101 | */ |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 102 | private Document m_repositoryDoc; |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * used to store bundle information. |
| 106 | */ |
| 107 | private ResourcesBundle m_resourceBundle; |
| 108 | |
| 109 | /** |
Stuart McCulloch | 80a8734 | 2008-01-27 11:04:46 +0000 | [diff] [blame] | 110 | * base URI used to relativize bundle URIs. |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 111 | */ |
Stuart McCulloch | 80a8734 | 2008-01-27 11:04:46 +0000 | [diff] [blame] | 112 | private URI m_baseURI; |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 113 | |
| 114 | |
| 115 | /** |
| 116 | * initialize information. |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 117 | * @param repositoryXml path to the repository descriptor file |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 118 | * @param obrXml path and filename to the obr.xml file |
| 119 | * @param project maven project description |
Stuart McCulloch | 37a882e | 2008-01-28 08:26:29 +0000 | [diff] [blame] | 120 | * @param bundleJar path to the bundle jar file |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 121 | * @param mavenRepositoryPath path to the local maven repository |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 122 | * @param userConfig user information |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 123 | * @param logger plugin logger |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 124 | */ |
Stuart McCulloch | 37a882e | 2008-01-28 08:26:29 +0000 | [diff] [blame] | 125 | public ObrUpdate( URI repositoryXml, URI obrXml, MavenProject project, URI bundleJar, String mavenRepositoryPath, |
| 126 | Config userConfig, Log logger ) |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 127 | { |
Stuart McCulloch | 37a882e | 2008-01-28 08:26:29 +0000 | [diff] [blame] | 128 | m_bundleJar = bundleJar; |
| 129 | m_repositoryXml = repositoryXml; |
Stuart McCulloch | 473e994 | 2008-01-28 05:23:50 +0000 | [diff] [blame] | 130 | m_obrXml = obrXml; |
Stuart McCulloch | bb9bd38 | 2008-01-25 08:14:10 +0000 | [diff] [blame] | 131 | m_project = project; |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 132 | m_logger = logger; |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 133 | |
Stuart McCulloch | bb9bd38 | 2008-01-25 08:14:10 +0000 | [diff] [blame] | 134 | m_userConfig = userConfig; |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 135 | |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 136 | m_resourceBundle = new ResourcesBundle( logger ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 137 | |
Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 138 | if ( userConfig.isRemoteFile() ) |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 139 | { |
Stuart McCulloch | 80a8734 | 2008-01-27 11:04:46 +0000 | [diff] [blame] | 140 | m_baseURI = ObrUtils.toFileURI( mavenRepositoryPath ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 141 | } |
| 142 | else |
| 143 | { |
Stuart McCulloch | 80a8734 | 2008-01-27 11:04:46 +0000 | [diff] [blame] | 144 | m_baseURI = m_repositoryXml; |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /** |
| 150 | * update the repository descriptor file. parse the old repository descriptor file, get the old reference of the bundle or determine the id for a new bundle, extract information from bindex set the new information in descriptor file and save it. |
| 151 | * @throws MojoExecutionException if the plugin failed |
| 152 | */ |
| 153 | public void updateRepository() throws MojoExecutionException |
| 154 | { |
Stuart McCulloch | 37a882e | 2008-01-28 08:26:29 +0000 | [diff] [blame] | 155 | m_logger.debug( " (f) repositoryXml = " + m_repositoryXml ); |
| 156 | m_logger.debug( " (f) bundleJar = " + m_bundleJar ); |
| 157 | m_logger.debug( " (f) obrXml = " + m_obrXml ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 158 | |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 159 | m_documentBuilder = initDocumentBuilder(); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 160 | |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 161 | if ( m_documentBuilder == null ) |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 162 | { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | // get the file size |
Stuart McCulloch | 37a882e | 2008-01-28 08:26:29 +0000 | [diff] [blame] | 167 | File bundleFile = new File( m_bundleJar ); |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 168 | if ( bundleFile.exists() ) |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 169 | { |
Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 170 | URI resourceURI = m_userConfig.getRemoteBundle(); |
| 171 | if ( null == resourceURI ) |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 172 | { |
Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 173 | resourceURI = m_bundleJar; |
| 174 | if ( m_userConfig.isPathRelative() ) |
| 175 | { |
| 176 | resourceURI = ObrUtils.getRelativeURI( m_baseURI, resourceURI ); |
| 177 | } |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 178 | } |
Stuart McCulloch | 80a8734 | 2008-01-27 11:04:46 +0000 | [diff] [blame] | 179 | |
| 180 | m_resourceBundle.setSize( String.valueOf( bundleFile.length() ) ); |
Stuart McCulloch | 37a882e | 2008-01-28 08:26:29 +0000 | [diff] [blame] | 181 | m_resourceBundle.setUri( resourceURI.toASCIIString() ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 182 | } |
| 183 | else |
| 184 | { |
Stuart McCulloch | 37a882e | 2008-01-28 08:26:29 +0000 | [diff] [blame] | 185 | m_logger.error( "file doesn't exist: " + m_bundleJar ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 186 | return; |
| 187 | } |
| 188 | |
| 189 | // parse repository |
| 190 | if ( parseRepositoryXml() == -1 ) |
| 191 | { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | // parse the obr.xml file |
| 196 | if ( m_obrXml != null ) |
| 197 | { |
Stuart McCulloch | 2deb052 | 2008-02-05 04:11:10 +0000 | [diff] [blame] | 198 | m_logger.info( "Adding " + m_obrXml ); |
| 199 | |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 200 | // URL url = getClass().getResource("/SchemaObr.xsd"); |
| 201 | // TODO validate obr.xml file |
| 202 | |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 203 | Document obrXmlDoc = parseFile( m_obrXml, m_documentBuilder ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 204 | if ( obrXmlDoc == null ) |
| 205 | { |
| 206 | return; |
| 207 | } |
Stuart McCulloch | 2deb052 | 2008-02-05 04:11:10 +0000 | [diff] [blame] | 208 | |
Stuart McCulloch | ebfe125 | 2008-02-04 08:32:32 +0000 | [diff] [blame] | 209 | Node obrXmlRoot = obrXmlDoc.getDocumentElement(); |
Stuart McCulloch | 2deb052 | 2008-02-05 04:11:10 +0000 | [diff] [blame] | 210 | |
| 211 | // add contents to resource bundle |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 212 | sortObrXml( obrXmlRoot ); |
| 213 | } |
| 214 | |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 215 | ExtractBindexInfo bindexExtractor; |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 216 | try |
| 217 | { |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 218 | // use bindex to extract bundle information |
Stuart McCulloch | 37a882e | 2008-01-28 08:26:29 +0000 | [diff] [blame] | 219 | bindexExtractor = new ExtractBindexInfo( m_repositoryXml, m_bundleJar.getPath() ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 220 | } |
| 221 | catch ( MojoExecutionException e ) |
| 222 | { |
| 223 | m_logger.error( "unable to build Bindex informations" ); |
| 224 | e.printStackTrace(); |
Stuart McCulloch | 2deb052 | 2008-02-05 04:11:10 +0000 | [diff] [blame] | 225 | |
Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 226 | throw new MojoExecutionException( "BindexException" ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 229 | m_resourceBundle.construct( m_project, bindexExtractor ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 230 | |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 231 | Element rootElement = m_repositoryDoc.getDocumentElement(); |
| 232 | if ( !walkOnTree( rootElement ) ) |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 233 | { |
| 234 | // the correct resource node was not found, we must create it |
| 235 | // we compute the new id |
| 236 | String id = m_resourceBundle.getId(); |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 237 | searchRepository( rootElement, id ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // the repository.xml file have been modified, so we save it |
| 241 | m_logger.info( "Writing OBR metadata" ); |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 242 | writeToFile( m_repositoryXml, rootElement ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | |
| 246 | /** |
| 247 | * init the document builder from xerces. |
| 248 | * @return DocumentBuilder ready to create new document |
| 249 | */ |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 250 | private DocumentBuilder initDocumentBuilder() |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 251 | { |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 252 | DocumentBuilder documentBuilder = null; |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 253 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 254 | try |
| 255 | { |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 256 | documentBuilder = factory.newDocumentBuilder(); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 257 | } |
| 258 | catch ( ParserConfigurationException e ) |
| 259 | { |
| 260 | m_logger.error( "unable to create a new xml document" ); |
| 261 | e.printStackTrace(); |
| 262 | } |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 263 | return documentBuilder; |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 264 | |
| 265 | } |
| 266 | |
| 267 | |
| 268 | /** |
| 269 | * Parse the repository descriptor file. |
| 270 | * |
| 271 | * @return 0 if the bundle is already in the descriptor, else -1 |
| 272 | * @throws MojoExecutionException if the plugin failed |
| 273 | */ |
| 274 | private int parseRepositoryXml() throws MojoExecutionException |
| 275 | { |
| 276 | |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 277 | File fout = new File( m_repositoryXml ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 278 | if ( !fout.exists() ) |
| 279 | { |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 280 | Document doc = m_documentBuilder.newDocument(); |
Stuart McCulloch | 80a8734 | 2008-01-27 11:04:46 +0000 | [diff] [blame] | 281 | |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 282 | // create xml tree |
| 283 | Date d = new Date(); |
| 284 | d.setTime( System.currentTimeMillis() ); |
| 285 | Element root = doc.createElement( "repository" ); |
| 286 | root.setAttribute( "lastmodified", m_format.format( d ) ); |
| 287 | root.setAttribute( "name", "MyRepository" ); |
| 288 | try |
| 289 | { |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 290 | writeToFile( m_repositoryXml, root ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 291 | } |
| 292 | catch ( MojoExecutionException e ) |
| 293 | { |
| 294 | e.printStackTrace(); |
Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 295 | throw new MojoExecutionException( "IOException" ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | // now we parse the repository.xml file |
Stuart McCulloch | 80a8734 | 2008-01-27 11:04:46 +0000 | [diff] [blame] | 300 | m_repositoryDoc = parseFile( m_repositoryXml, m_documentBuilder ); |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 301 | if ( m_repositoryDoc == null ) |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 302 | { |
| 303 | return -1; |
| 304 | } |
| 305 | |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | |
| 310 | /** |
| 311 | * transform a xml file to a xerces Document. |
| 312 | * @param filename path to the xml file |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 313 | * @param documentBuilder DocumentBuilder get from xerces |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 314 | * @return Document which describe this file |
| 315 | */ |
Stuart McCulloch | 80a8734 | 2008-01-27 11:04:46 +0000 | [diff] [blame] | 316 | private Document parseFile( URI filename, DocumentBuilder documentBuilder ) |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 317 | { |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 318 | if ( documentBuilder == null ) |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 319 | { |
| 320 | return null; |
| 321 | } |
| 322 | // The document is the root of the DOM tree. |
| 323 | m_logger.info( "Parsing " + filename ); |
| 324 | Document doc = null; |
| 325 | try |
| 326 | { |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 327 | doc = documentBuilder.parse( new File( filename ) ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 328 | } |
| 329 | catch ( SAXException e ) |
| 330 | { |
| 331 | e.printStackTrace(); |
| 332 | return null; |
| 333 | } |
| 334 | catch ( IOException e ) |
| 335 | { |
| 336 | m_logger.error( "cannot open file: " + filename ); |
| 337 | e.printStackTrace(); |
| 338 | return null; |
| 339 | } |
| 340 | return doc; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /** |
| 345 | * put the information from obr.xml into ressourceBundle object. |
| 346 | * @param node Node to the OBR.xml file |
| 347 | */ |
| 348 | private void sortObrXml( Node node ) |
| 349 | { |
| 350 | if ( node.getNodeName().compareTo( "require" ) == 0 ) |
| 351 | { |
| 352 | Require newRequireNode = new Require(); |
| 353 | NamedNodeMap list = node.getAttributes(); |
| 354 | try |
| 355 | { |
| 356 | newRequireNode.setExtend( list.getNamedItem( "extend" ).getNodeValue() ); |
| 357 | newRequireNode.setMultiple( list.getNamedItem( "multiple" ).getNodeValue() ); |
| 358 | newRequireNode.setOptional( list.getNamedItem( "optional" ).getNodeValue() ); |
| 359 | newRequireNode.setFilter( list.getNamedItem( "filter" ).getNodeValue() ); |
| 360 | newRequireNode.setName( list.getNamedItem( "name" ).getNodeValue() ); |
| 361 | } |
| 362 | catch ( NullPointerException e ) |
| 363 | { |
| 364 | m_logger |
| 365 | .error( "the obr.xml file seems to be invalid in a \"require\" tag (one or more attributes are missing)" ); |
| 366 | // e.printStackTrace(); |
| 367 | } |
| 368 | newRequireNode.setValue( XmlHelper.getTextContent( node ) ); |
| 369 | m_resourceBundle.addRequire( newRequireNode ); |
| 370 | } |
| 371 | else if ( node.getNodeName().compareTo( "capability" ) == 0 ) |
| 372 | { |
| 373 | Capability newCapability = new Capability(); |
| 374 | try |
| 375 | { |
| 376 | newCapability.setName( node.getAttributes().getNamedItem( "name" ).getNodeValue() ); |
| 377 | } |
| 378 | catch ( NullPointerException e ) |
| 379 | { |
| 380 | m_logger.error( "attribute \"name\" is missing in obr.xml in a \"capability\" tag" ); |
| 381 | e.printStackTrace(); |
| 382 | } |
| 383 | NodeList list = node.getChildNodes(); |
| 384 | for ( int i = 0; i < list.getLength(); i++ ) |
| 385 | { |
| 386 | PElement p = new PElement(); |
| 387 | Node n = list.item( i ); |
| 388 | Node item = null; |
| 389 | // System.err.println(n.getNodeName()); |
| 390 | if ( n.getNodeName().compareTo( "p" ) == 0 ) |
| 391 | { |
| 392 | |
| 393 | p.setN( n.getAttributes().getNamedItem( "n" ).getNodeValue() ); |
| 394 | item = n.getAttributes().getNamedItem( "t" ); |
| 395 | if ( item != null ) |
| 396 | { |
| 397 | p.setT( item.getNodeValue() ); |
| 398 | } |
| 399 | item = n.getAttributes().getNamedItem( "v" ); |
| 400 | if ( item != null ) |
| 401 | { |
| 402 | p.setV( item.getNodeValue() ); |
| 403 | } |
| 404 | |
| 405 | newCapability.addP( p ); |
| 406 | } |
| 407 | } |
| 408 | m_resourceBundle.addCapability( newCapability ); |
| 409 | } |
| 410 | else if ( node.getNodeName().compareTo( "category" ) == 0 ) |
| 411 | { |
| 412 | Category newCategory = new Category(); |
| 413 | newCategory.setId( node.getAttributes().getNamedItem( "id" ).getNodeValue() ); |
| 414 | m_resourceBundle.addCategory( newCategory ); |
| 415 | } |
| 416 | else |
| 417 | { |
| 418 | NodeList list = node.getChildNodes(); |
| 419 | for ( int i = 0; i < list.getLength(); i++ ) |
| 420 | { |
| 421 | sortObrXml( list.item( i ) ); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | |
| 427 | /** |
| 428 | * write a Node in a xml file. |
| 429 | * @param outputFilename URI to the output file |
| 430 | * @param treeToBeWrite Node root of the tree to be write in file |
| 431 | * @throws MojoExecutionException if the plugin failed |
| 432 | */ |
| 433 | private void writeToFile( URI outputFilename, Node treeToBeWrite ) throws MojoExecutionException |
| 434 | { |
| 435 | // init the transformer |
| 436 | Transformer transformer = null; |
| 437 | TransformerFactory tfabrique = TransformerFactory.newInstance(); |
| 438 | try |
| 439 | { |
| 440 | transformer = tfabrique.newTransformer(); |
| 441 | } |
| 442 | catch ( TransformerConfigurationException e ) |
| 443 | { |
| 444 | m_logger.error( "Unable to write to file: " + outputFilename.toString() ); |
| 445 | e.printStackTrace(); |
| 446 | throw new MojoExecutionException( "TransformerConfigurationException" ); |
| 447 | } |
| 448 | Properties proprietes = new Properties(); |
| 449 | proprietes.put( "method", "xml" ); |
| 450 | proprietes.put( "version", "1.0" ); |
| 451 | proprietes.put( "encoding", "ISO-8859-1" ); |
| 452 | proprietes.put( "standalone", "yes" ); |
| 453 | proprietes.put( "indent", "yes" ); |
| 454 | proprietes.put( "omit-xml-declaration", "no" ); |
| 455 | transformer.setOutputProperties( proprietes ); |
| 456 | |
| 457 | DOMSource input = new DOMSource( treeToBeWrite ); |
| 458 | |
| 459 | File fichier = new File( outputFilename ); |
Stuart McCulloch | 80a8734 | 2008-01-27 11:04:46 +0000 | [diff] [blame] | 460 | fichier.getParentFile().mkdirs(); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 461 | FileOutputStream flux = null; |
| 462 | try |
| 463 | { |
| 464 | flux = new FileOutputStream( fichier ); |
| 465 | } |
| 466 | catch ( FileNotFoundException e ) |
| 467 | { |
| 468 | m_logger.error( "Unable to write to file: " + fichier.getName() ); |
| 469 | e.printStackTrace(); |
| 470 | throw new MojoExecutionException( "FileNotFoundException" ); |
| 471 | } |
| 472 | Result output = new StreamResult( flux ); |
| 473 | try |
| 474 | { |
| 475 | transformer.transform( input, output ); |
| 476 | } |
| 477 | catch ( TransformerException e ) |
| 478 | { |
| 479 | e.printStackTrace(); |
| 480 | throw new MojoExecutionException( "TransformerException" ); |
| 481 | } |
| 482 | |
| 483 | try |
| 484 | { |
| 485 | flux.flush(); |
| 486 | flux.close(); |
| 487 | } |
| 488 | catch ( IOException e ) |
| 489 | { |
| 490 | e.printStackTrace(); |
| 491 | throw new MojoExecutionException( "IOException" ); |
| 492 | } |
| 493 | |
| 494 | } |
| 495 | |
| 496 | |
| 497 | /** |
| 498 | * walk on the tree until the targeted node was found. |
| 499 | * @param node targeted node |
| 500 | * @return true if the requiered node was found else false. |
| 501 | */ |
| 502 | private boolean walkOnTree( Node node ) |
| 503 | { |
| 504 | |
| 505 | if ( node.getNodeName().compareTo( "resource" ) == 0 ) |
| 506 | { |
| 507 | return resource( node ); |
| 508 | } |
Stuart McCulloch | ebfe125 | 2008-02-04 08:32:32 +0000 | [diff] [blame] | 509 | |
| 510 | // look at the repository node (first in the file) |
| 511 | if ( node.getNodeName().compareTo( "repository" ) == 0 ) |
| 512 | { |
| 513 | Date d = new Date(); |
| 514 | d.setTime( System.currentTimeMillis() ); |
| 515 | NamedNodeMap nList = node.getAttributes(); |
| 516 | Node n = nList.getNamedItem( "lastmodified" ); |
| 517 | n.setNodeValue( m_format.format( d ) ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Stuart McCulloch | ebfe125 | 2008-02-04 08:32:32 +0000 | [diff] [blame] | 520 | NodeList list = node.getChildNodes(); |
| 521 | if ( list.getLength() > 0 ) |
| 522 | { |
| 523 | for ( int i = 0; i < list.getLength(); i++ ) |
| 524 | { |
| 525 | if ( walkOnTree( list.item( i ) ) ) |
| 526 | { |
| 527 | return true; |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | return false; |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | |
| 536 | /** |
| 537 | * put the resource bundle in the tree. |
| 538 | * @param node Node on the xml file |
| 539 | * @param id id of the bundle ressource |
| 540 | */ |
| 541 | private void searchRepository( Node node, String id ) |
| 542 | { |
| 543 | if ( node.getNodeName().compareTo( "repository" ) == 0 ) |
| 544 | { |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 545 | node.appendChild( m_resourceBundle.getNode( m_repositoryDoc ) ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 546 | return; |
| 547 | } |
Stuart McCulloch | ebfe125 | 2008-02-04 08:32:32 +0000 | [diff] [blame] | 548 | |
Stuart McCulloch | a132a14 | 2008-02-05 11:18:20 +0000 | [diff] [blame] | 549 | m_logger.info( "Second branch..." ); |
Stuart McCulloch | ebfe125 | 2008-02-04 08:32:32 +0000 | [diff] [blame] | 550 | NodeList list = node.getChildNodes(); |
| 551 | if ( list.getLength() > 0 ) |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 552 | { |
Stuart McCulloch | ebfe125 | 2008-02-04 08:32:32 +0000 | [diff] [blame] | 553 | for ( int i = 0; i < list.getLength(); i++ ) |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 554 | { |
Stuart McCulloch | ebfe125 | 2008-02-04 08:32:32 +0000 | [diff] [blame] | 555 | searchRepository( list.item( i ), id ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | |
| 561 | /** |
| 562 | * compare two node and update the array which compute the smallest free id. |
| 563 | * @param node : node |
| 564 | * @return true if the node is the same bundle than the ressourceBundle, else false. |
| 565 | */ |
| 566 | private boolean resource( Node node ) |
| 567 | { |
| 568 | |
| 569 | // this part save all the id free if we need to add resource |
| 570 | String id = node.getAttributes().getNamedItem( "id" ).getNodeValue(); |
| 571 | NamedNodeMap map = node.getAttributes(); |
| 572 | |
| 573 | if ( m_resourceBundle.isSameBundleResource( map.getNamedItem( "symbolicname" ).getNodeValue(), map |
| 574 | .getNamedItem( "version" ).getNodeValue() ) ) |
| 575 | { |
| 576 | m_resourceBundle.setId( String.valueOf( id ) ); |
Stuart McCulloch | b721aae | 2008-01-27 08:16:22 +0000 | [diff] [blame] | 577 | node.getParentNode().replaceChild( m_resourceBundle.getNode( m_repositoryDoc ), node ); |
Stuart McCulloch | b657c2f | 2008-01-25 08:10:25 +0000 | [diff] [blame] | 578 | return true; |
| 579 | } |
| 580 | return false; |
| 581 | } |
| 582 | } |