View Javadoc

1   package org.codehaus.mojo.javascript.assembler;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import javax.xml.parsers.DocumentBuilder;
24  import javax.xml.parsers.DocumentBuilderFactory;
25  
26  import org.apache.commons.jxpath.JXPathContext;
27  import org.codehaus.plexus.logging.LogEnabled;
28  import org.codehaus.plexus.logging.Logger;
29  import org.w3c.dom.Document;
30  import org.w3c.dom.Node;
31  
32  /**
33   * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
34   */
35  public class JsBuilderAssemblerReader
36      implements AssemblerReader, LogEnabled
37  {
38      private Logger logger;
39  
40      /**
41       * {@inheritDoc}
42       * 
43       * @see org.codehaus.plexus.logging.LogEnabled#enableLogging(org.codehaus.plexus.logging.Logger)
44       */
45      public void enableLogging( Logger logger )
46      {
47          this.logger = logger;
48      }
49  
50      private static final String OUTPUT = "$output/";
51  
52      /**
53       * {@inheritDoc}
54       * 
55       * @see org.codehaus.mojo.javascript.assembler.AssemblerReader#getAssembler(java.io.File)
56       */
57      public Assembler getAssembler( File file )
58          throws Exception
59      {
60          logger.info( "Reading assembler descriptor " + file.getAbsolutePath() );
61          DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
62          Document dom = builder.parse( file );
63  
64          Assembler assembler = new Assembler();
65          JXPathContext xpath = JXPathContext.newContext( dom );
66          xpath.setLenient( true );
67          String src = (String) xpath.getValue( "//directory/@name" );
68          List nodes = xpath.selectNodes( "//target" );
69          for ( Iterator iterator = nodes.iterator(); iterator.hasNext(); )
70          {
71              Node node = (Node) iterator.next();
72              Script script = new Script();
73              assembler.addScript( script );
74              JXPathContext nodeContext = JXPathContext.newContext( node );
75              String fileName = (String) nodeContext.getValue( "@file" );
76              fileName = fileName.replace( '\\', '/' );
77              if ( fileName.startsWith( OUTPUT ) )
78              {
79                  fileName = fileName.substring( OUTPUT.length() );
80              }
81              script.setFileName( fileName );
82  
83              for ( Iterator iter = nodeContext.iterate( "//include/@name" ); iter.hasNext(); )
84              {
85                  String include = ( (String) iter.next() ).replace( '\\', '/' );
86                  if ( src != null && src.length() > 0 )
87                  {
88                      include = include.substring( src.length() + 1 );
89                  }
90                  script.addInclude( include );
91              }
92          }
93          return assembler;
94      }
95  }