1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  package com.github.maven_nar.cpptasks.compiler;
21  
22  import org.apache.tools.ant.types.Environment;
23  
24  
25  
26  
27  
28  
29  public abstract class AbstractProcessor implements Processor, Cloneable {
30    
31  
32  
33  
34    public final static int DEFAULT_DISCARD_BID = 1;
35    
36  
37  
38    public final static int DEFAULT_PROCESS_BID = 100;
39  
40    
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52    protected static String getIdentifier(final String[] command, final String fallback) {
53      String identifier = fallback;
54      try {
55        final String[] cmdout = CaptureStreamHandler.run(command);
56        if (cmdout.length > 0) {
57          identifier = cmdout[0];
58        }
59      } catch (final Throwable ex) {
60        identifier = fallback + ":" + ex.toString();
61      }
62      return identifier;
63    }
64  
65    private final String[] headerExtensions;
66    private final String[] sourceExtensions;
67  
68    protected AbstractProcessor(final String[] sourceExtensions, final String[] headerExtensions) {
69      this.sourceExtensions = sourceExtensions.clone();
70      this.headerExtensions = headerExtensions.clone();
71    }
72  
73    
74  
75  
76  
77  
78  
79  
80  
81  
82    @Override
83    public int bid(final String inputFile) {
84      final String lower = inputFile.toLowerCase();
85      for (final String sourceExtension : this.sourceExtensions) {
86        if (lower.endsWith(sourceExtension)) {
87          return DEFAULT_PROCESS_BID;
88        }
89      }
90      for (final String headerExtension : this.headerExtensions) {
91        if (lower.endsWith(headerExtension)) {
92          return DEFAULT_DISCARD_BID;
93        }
94      }
95      return 0;
96    }
97  
98    @Override
99    public Processor changeEnvironment(final boolean newEnvironment, final Environment env) {
100     return this;
101   }
102 
103   @Override
104   protected Object clone() throws CloneNotSupportedException {
105     return super.clone();
106   }
107 
108   public String[] getHeaderExtensions() {
109     return this.headerExtensions.clone();
110   }
111 
112   @Override
113   abstract public String getIdentifier();
114 
115   
116 
117 
118 
119 
120   protected String getOSArch() {
121     return System.getProperty("os.arch");
122   }
123 
124   
125 
126 
127 
128 
129   protected String getOSName() {
130     return System.getProperty("os.name");
131   }
132 
133   public String[] getSourceExtensions() {
134     return this.sourceExtensions.clone();
135   }
136 
137   
138 
139 
140 
141 
142   protected boolean isDarwin() {
143     final String osName = getOSName();
144     return "Mac OS X".equals(osName);
145   }
146 
147   
148   protected boolean isWindows() {
149     final String osName = getOSName();
150     return osName != null && osName.startsWith("Windows");
151   }
152 
153   
154 
155   @Override
156   public final String toString() {
157     return getIdentifier();
158   }
159 }