summaryrefslogtreecommitdiffstats
path: root/tags/cpp-1.0-incubating-M2-RC1/sca/tools/scagen/src/org/apache/tuscany/sca/cpp/tools/services/ServicesGenerator.java
blob: 956db364a19528b439e37c15b5e014dde25e4a92 (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/**
 *
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.
 */

/* @version $Rev$ $Date$ */

package org.apache.tuscany.sca.cpp.tools.services;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.tuscany.sca.cpp.tools.common.CParsingTool;
import org.apache.tuscany.sca.cpp.tools.common.Headers;
import org.apache.tuscany.sca.cpp.tools.common.Options;
import org.apache.tuscany.sca.cpp.tools.common.Parameter;
import org.apache.tuscany.sca.cpp.tools.common.Signature;
import org.apache.tuscany.sca.cpp.tools.common.Utils;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

/**
 * This class is the main class that handles the function that parses a C++
 * interface header file into a DOM that holds all the semantic information
 * about the interface - method names, parameters and return values. It then
 * uses XSLT to generate different "views" of this data plus the parameter map
 * from other sources that are the C++ programs for the proxy and wrapper
 * implementations and headers.
 */
public class ServicesGenerator extends CParsingTool {

    private static final String OPERATION_NAME_ATTR = "operationNameAttr";

    private static final String HEADER_NAME_ATTR = "headerNameAttr";

    private static final String SCA_OPERATION = "scaOperation";

    private static final String SCA_SERVICE = "scaService";

    private static final String SCA_OPERATION_RETURN_TYPE = "scaOperationReturnType";

    private static final String SCA_OPERATION_PARAMETER = "scaOperationParameter";

    private static final String SCA_OPERATION_PARAMETER_NAME_ATTR = "scaOperationParameterNameAttr";

    private static final String SCA_OPERATION_PARAMETER_CONST_ATTR = "scaOperationParameterConstAttr";

    private static final String SCA_INTERFACE_NAME_ATTR = "scaInterfaceNameAttr";

    private static final String CPP_HEADER = "cppHeader";

    private static boolean TESTING = true;

    private static TransformerFactory txmf = null;

    private static ServicesGenerator instance = null;

    public ServicesGenerator(String[] args) throws Exception {
        super(args);
        txmf = TransformerFactory.newInstance();
    }

    /**
     * 
     * @param parameters
     * @param forReference
     *            true if we are creating a proxy for a reference (rather than
     *            for a service)
     * @throws Exception
     * 
     * The design is
     * <ul>
     * <li>handleInterfaceHeader
     * <li>createDOMofMethods
     * <li>createProxyCPPFromDom(outputDir, dom);
     * <li>createProxyHeaderFromDom(outputDir, dom);
     * </ul>
     * 
     * plus if we are not generating for a reference element
     * <ul>
     * <li>createWrapperCPPFromDom(outputDir, dom);
     * <li>createWrapperHeaderFromDom(outputDir, dom);
     * <ul>
     * Each of the create.... methods calls createOutputFromDom with a different
     * style sheet.
     *  
     */
    public static void handleInterfaceHeader(Map parameters,
            boolean forReference) throws Exception {
        boolean failed = false;

        String type = null;
        if (forReference) {
            type = "reference";
        } else {
            type = "service";
        }
        String interfaceHeaderFilename = (String) parameters
                .get("/componentType/" + type + "/interface.cpp/@header");

        String componentTypeFileHeaderName = interfaceHeaderFilename;

        String sca_composite_root =  (String) Options.getOption("-dir");
        parameters.put("composite_root", sca_composite_root);

        if (sca_composite_root != null && interfaceHeaderFilename != null
                && interfaceHeaderFilename.length() > 0) {
            String separator;
            // Stick in a "/" (File.separator) if required.
            if ((interfaceHeaderFilename.substring(0, 1).equals("/") || sca_composite_root
                    .substring(sca_composite_root.length() - 1,
                            sca_composite_root.length()).equals("/"))
                    || (interfaceHeaderFilename.substring(0, 1).equals("\\") || sca_composite_root
                            .substring(sca_composite_root.length() - 1,
                                    sca_composite_root.length()).equals("\\"))

            ) {
                separator = "";
            } else {
                separator = File.separator;
            }
            interfaceHeaderFilename = sca_composite_root + separator
                    + interfaceHeaderFilename;
        }

        File outputDir = (File) parameters.get("targetDirectoryFile");

        String[] args = new String[] { "-source", interfaceHeaderFilename,
                "-target", outputDir.getPath() };
        ServicesGenerator gen = new ServicesGenerator(args);
        File file;
        try {
            if (null == interfaceHeaderFilename) {
                gen.printUsage();
                System.exit(-1);
            }

            file = new File(interfaceHeaderFilename);
            if (!file.isFile()) {
                if (file.isDirectory()) {
                    Utils
                            .rude("This tool works at the header file level and not for directories like "
                                    + file);
                }
                Utils.rude("Bad file or directory " + file);
            }
            File source = file;
            if (!outputDir.exists() && !outputDir.mkdir())
                Utils.rude("Failed to create directory " + outputDir);

            // The class below is the one that will go through the header
            // file(s)
            Headers headers = new Headers();

            if (null != interfaceHeaderFilename) {
                Utils.postEvent(Utils.DEPLOYMENT_ARTEFACT_ENCOUNTERED, file
                        .getAbsolutePath());
                Utils.postEvent(Utils.EVENT_TYPE_FILE_PARSED,
                        "Scagen processing C++ interface header "
                                + file.getAbsolutePath());
                headers.actOnFile(file, outputDir, 1);
                Utils.outputDebugString("Parsing files...");
            }

            String nameOfSorR = null;
            String referenceName = null;
            String serviceName = null;

            if (forReference) {

                Object rn = parameters.get("/componentType/reference/@name");

                if (rn instanceof String) {
                    referenceName = (String) rn;
                    nameOfSorR = referenceName;
                }
            } else {

                Object sn = parameters.get("/componentType/service/@name");

                if (sn instanceof String) {
                    serviceName = (String) sn;
                    nameOfSorR = serviceName;
                }
            }

            String compositeXmlFileHeader = null;
            String compositeXmlFileHeaderNoExt = null;
            Object compositeh = parameters
                    .get("/composite/component/implementation.cpp/@header");

            if (compositeh == null) {
                compositeh = parameters
                        .get("/compositeFragment/component/implementation.cpp/@header");
            }

            if (compositeh instanceof String) {
                File f = new File((String) compositeh);
                compositeXmlFileHeader = (String) compositeh;

                String fname = f.getName();
                compositeXmlFileHeaderNoExt = fname.substring(0, fname
                        .lastIndexOf('.'));

            }

            String implClassNameAttrFromCompositeFile = (String) parameters
            	.get("implClass");
            String implClassNamespaceAttrFromCompositeFile = (String) parameters
            	.get("implNamespace");
            
            if(implClassNamespaceAttrFromCompositeFile == null || implClassNamespaceAttrFromCompositeFile.length() == 0)
            {
                implClassNamespaceAttrFromCompositeFile = "";
            }
            else
            {
                implClassNamespaceAttrFromCompositeFile += "::";
            }
            
            String interfaceClassNameAttrFromComponentTypeFile;
            if (forReference) {
                interfaceClassNameAttrFromComponentTypeFile = (String) parameters
                        .get("/componentType/reference/interface.cpp/@class");
            } else {
                interfaceClassNameAttrFromComponentTypeFile = (String) parameters
                        .get("/componentType/service/interface.cpp/@class");
            }

            List methods = headers.getAllMethods();
            
            // Pull out one of the methods' namespace attributes.
            String intfNamespace = null;
            if (methods.size() > 0) {
                Signature method = (Signature) methods.get(0);
                intfNamespace = method.getNamespace();
            }

            if (interfaceClassNameAttrFromComponentTypeFile != null) {
                methods = filterToPublicMethodsOfGivenClass(methods,
                        interfaceClassNameAttrFromComponentTypeFile, true);
            } else {
                //We want to filter to methods of the class whose
                //name matches the header file name.
                //      String intfClassName = (String) parameters.get("intfClass");
                //
                String headerFileBase = file.getName().replaceAll(
                        "\\.h|\\.hpp|\\.h++", "");

                methods = filterToPublicMethodsOfGivenClass(methods,
                        headerFileBase, false);
            }

            Document dom = createDOMofMethods(methods, source, serviceName,
                    referenceName, nameOfSorR, null,
                    componentTypeFileHeaderName, compositeXmlFileHeader,
                    compositeXmlFileHeaderNoExt, intfNamespace,
                    interfaceClassNameAttrFromComponentTypeFile,
                    implClassNameAttrFromCompositeFile, implClassNamespaceAttrFromCompositeFile);
           
//            // Print out the generated DOM
//            StringWriter sw = new StringWriter();
//            OutputFormat outputFormat = new OutputFormat("xml", "UTF-8", true);
//            XMLSerializer serializer = new XMLSerializer(sw, outputFormat);
//            serializer.serialize(dom);
//            System.out.println("DOM:\n"+sw.toString()); 
            
            
            createProxyCPPFromDom(outputDir, dom);
            createProxyHeaderFromDom(outputDir, dom);

            if (!forReference) {
                createWrapperCPPFromDom(outputDir, dom);
                createWrapperHeaderFromDom(outputDir, dom);
            }

        } catch (Exception exception) {
            exception.printStackTrace();
            failed = true;
        }

        if (failed) {
            Utils.outputDebugString("Finished! (but encountered problems)");
            System.exit(-2);
        }
    }

    /**
     * This methods takes a list of methods and filters them to only contain the
     * public methods of the given class
     * 
     * @param methods
     *            a list of methods
     * @param className
     *            we will return a list of only this classes methods from the
     *            methods parameter
     * @param attrSpecified
     *            if the user does not specify one we ignore namespaces
     * @return
     */
    private static List filterToPublicMethodsOfGivenClass(List methods,
            String className, boolean useNamespace) {
        if (methods != null && className != null && className.length() > 0) {
            for (Iterator iter = methods.listIterator(); iter.hasNext();) {
                Signature method = (Signature) iter.next();
                String scope = method.getScope();
                String sig = method.getOriginal();
                String namespace = method.getNamespace();

                String qualifiedClassName;
                if (useNamespace && null != namespace && 0 < namespace.length()) {
                    qualifiedClassName = namespace + "::"
                            + method.getTrimClassName();
                } else {
                    qualifiedClassName = method.getTrimClassName();
                }

                // If we are not public or the classname does not match
                // remove...
                if (!qualifiedClassName.equals(className)
                        || -1 == scope.indexOf("public")) {
                    iter.remove();
                }
            }
        }
        return methods;
    }

    /**
     * Use an XSLT transformation to create a Proxy "view" of the DOM
     * information
     * 
     * @param outputDir
     *            where to put the C++
     * @param dom
     *            the DOM of semantic method information
     * @throws TransformerException
     */
    private static void createProxyCPPFromDom(File outputDir, Document dom)
            throws TransformerException {

        //Create the Filename from the Service Name in the DOM
        if (dom == null) {
            return;
        }

        String serviceOrReferenceName = "noSorRNameDefined";

        String implClass = "nocompositeXmlFileHeaderDefined";

        Element topNode = dom.getDocumentElement();
        if (null != topNode) {
            Attr attr = topNode.getAttributeNode("nameOfSorR");
            if (attr != null) {
                serviceOrReferenceName = attr.getNodeValue();
            }

            attr = topNode.getAttributeNode("implClass");
            if (attr != null) {
                implClass = attr.getNodeValue();
            }
        }

        File proxyCPP = new File(outputDir, implClass + "_"
                + serviceOrReferenceName + "_Proxy.cpp");
        if (null != proxyCPP) {

            Utils.postEvent(Utils.DEPLOYMENT_ARTEFACT_GENERATED, proxyCPP
                    .getAbsolutePath());
            Utils.postEvent(Utils.EVENT_TYPE_FILE_CREATE,
                    "Scagen creating SCA for C++ proxy implementation "
                            + proxyCPP.getAbsolutePath());
        }

        createOutputFromDom(dom, proxyCPP,
                "org/apache/tuscany/sca/cpp/tools/services/xsl/SCA4CPPIntfProxyCPP.xsl");

    }

    /**
     * Use an XSLT transformation to create a Wrapper "view" of the DOM
     * information
     * 
     * @param outputDir
     *            where to put the C++
     * @param dom
     *            the DOM of semantic method information
     * @throws TransformerException
     */
    private static void createWrapperCPPFromDom(File outputDir, Document dom)
            throws TransformerException {

        //Create the Filename from the Service Name in the DOM
        if (dom == null) {
            return;
        }

        String serviceName = "noServiceDefined";

        String implClass = "nocompositeXmlFileHeaderDefined";

        Element topNode = dom.getDocumentElement();
        if (null != topNode) {
            Attr attr = topNode.getAttributeNode("serviceName");
            if (attr != null) {
                serviceName = attr.getNodeValue();
            }

            attr = topNode.getAttributeNode("implClass");
            if (attr != null) {
                implClass = attr.getNodeValue();
            }
        }

        File wrapperCPP = new File(outputDir, implClass + "_" + serviceName
                + "_Wrapper.cpp");
        if (null != wrapperCPP) {
            Utils.postEvent(Utils.DEPLOYMENT_ARTEFACT_GENERATED, wrapperCPP
                    .getAbsolutePath());
            Utils.postEvent(Utils.EVENT_TYPE_FILE_CREATE,
                    "Scagen creating SCA for C++ wrapper implementation "
                            + wrapperCPP.getAbsolutePath());
        }
        createOutputFromDom(dom, wrapperCPP,
                "org/apache/tuscany/sca/cpp/tools/services/xsl/SCA4CPPIntfWrapperCPP.xsl");

    }

    /**
     * Use an XSLT transformation to create a Proxy header "view" of the DOM
     * information
     * 
     * @param outputDir
     *            where to put the C++
     * @param dom
     *            the DOM of sematic method information
     * @throws TransformerException
     */
    private static void createProxyHeaderFromDom(File outputDir, Document dom)
            throws TransformerException {

        //Create the Filename from the Service Name in the DOM
        if (dom == null) {
            return;
        }

        String serviceOrReferenceName = "noServiceDefined";
        String intfClass = "noInterfaceClassNameOrDefaultDefined";
        String implClass = "noImplementationClassOrDefaultDefined";

        Element topNode = dom.getDocumentElement();
        if (null != topNode) {
            Attr attr = topNode.getAttributeNode("nameOfSorR");
            if (attr != null) {
                serviceOrReferenceName = attr.getNodeValue();
            }
            attr = topNode.getAttributeNode("intfClass");
            if (attr != null) {
                intfClass = attr.getNodeValue();
            }
            attr = topNode.getAttributeNode("implClass");
            if (attr != null) {
                implClass = attr.getNodeValue();
            }
        }

        File proxyHeader = new File(outputDir, implClass + "_"
                + serviceOrReferenceName + "_Proxy.h");
        if (null != proxyHeader) {
            Utils.postEvent(Utils.DEPLOYMENT_ARTEFACT_GENERATED, proxyHeader
                    .getAbsolutePath());
            Utils.postEvent(Utils.EVENT_TYPE_FILE_CREATE,
                    "Scagen creating SCA for C++ proxy header "
                            + proxyHeader.getAbsolutePath());
        }

        createOutputFromDom(dom, proxyHeader,
                "org/apache/tuscany/sca/cpp/tools/services/xsl/SCA4CPPIntfProxyHeader.xsl");

    }

    /**
     * Use an XSLT transformation to create a Wrapper header "view" of the DOM
     * information
     * 
     * @param outputDir
     *            where to put the C++
     * @param dom
     *            the DOM of sematic method information
     * @throws TransformerException
     */
    private static void createWrapperHeaderFromDom(File outputDir, Document dom)
            throws TransformerException {

        //Create the Filename from the Service Name in the DOM
        if (dom == null) {
            return;
        }

        String serviceName = "noServiceDefined";

        String implClass = "nocompositeXmlFileHeaderDefined";

        Element topNode = dom.getDocumentElement();
        if (null != topNode) {
            Attr attr = topNode.getAttributeNode("serviceName");
            if (attr != null) {
                serviceName = attr.getNodeValue();
            }

            attr = topNode.getAttributeNode("implClass");
            if (attr != null) {
                implClass = attr.getNodeValue();
            }
        }

        File wrapperHeader = new File(outputDir, implClass + "_" + serviceName
                + "_Wrapper.h");

        if (null != wrapperHeader) {
            Utils.postEvent(Utils.DEPLOYMENT_ARTEFACT_GENERATED, wrapperHeader
                    .getAbsolutePath());
            Utils.postEvent(Utils.EVENT_TYPE_FILE_CREATE,
                    "Scagen creating SCA for C++ wrapper header "
                            + wrapperHeader.getAbsolutePath());
        }
        createOutputFromDom(dom, wrapperHeader,
                "org/apache/tuscany/sca/cpp/tools/services/xsl/SCA4CPPIntfWrapperHeader.xsl");

    }

    /**
     * This method will return a class loader that can see the resources that
     * are parts of "this" package.
     */
    private static ClassLoader getALoader() {

        LittleClass lc;
        try {
            lc = new LittleClass();
            return lc.getClass().getClassLoader();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * This method will generically process the DOM using a stylesheet passed
     * in.
     * 
     * @param dom
     *            the source of data (the model)
     * @param outputFile
     *            where to put the result
     * @param xslTransformationFileName
     *            the xsl file containing the "view"
     * @throws TransformerException
     */
    private static void createOutputFromDom(Document dom, File outputFile,
            String xslTransformationFileName) throws TransformerException {
        
        if(Options.noGenerate()){
            return;
        }
        
        InputStream stream = getALoader().getResourceAsStream(
                xslTransformationFileName);
        StreamSource ss = new StreamSource(stream);
        StreamResult out = new StreamResult(outputFile);
        try {
            Transformer xslt = txmf.newTransformer(ss);

            // Unless a width is set, there will be only line breaks but no
            // indentation.
            // The IBM JDK and the Sun JDK don't agree on the property name,
            // so we set them both.
            //
            try {
                xslt.setOutputProperty(
                        "{http://xml.apache.org/xalan}indent-amount", "2");
            } catch (Throwable t) {
                //OK to ignore this, depends on XSLT engine and one will fail
            }
            try {
                xslt.setOutputProperty(
                        "{http://xml.apache.org/xslt}indent-amount", "2");
            } catch (Throwable t) {
                //OK to ignore this, depends on XSLT engine and one will fail
            }

            DOMSource from = new DOMSource(dom);

            xslt.transform(from, out);

        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
            throw (e);
        } catch (TransformerException e) {
            e.printStackTrace();
            throw (e);
        } finally {
            try {
                stream.close();
            } catch (IOException e1) {
                // unlikely but if
                // we can't close it, we can't close it
            }
        }

    }

    //TODO clear up the parameter list below to make it smaller if
    // possible
    /**
     * This method will create A DOM containing all the semantic information
     * that it can extract from a C++ header file.
     * 
     * @param methods
     *            a list of methods that are going into the DOM
     * @param source
     *            the header file the methods came from
     * @param serviceName
     *            the name of the service
     * @referenceName the name of the reference
     * @nameOfSorR the non null one of the two parameters above
     * @headerClassName the name of the header class
     * @param compositeXmlFileImplHeaderNameWithPathAndExt
     *            the source filename
     * @param compositeXmlFileHeaderNoExtorPath
     *            the shortname of the source file
     * @param implClass
     *            the implementation class
     * @param intfClass
     *            the interface we are turning into a service
     * @return
     */
    private static Document createDOMofMethods(List methods, File source,
            String serviceName, String referenceName, String nameOfSorR,
            String headerClassName,
            String componentTypeXmlFileIntfHeaderNameWithPathAndExt,
            String compositeXmlFileImplHeaderNameWithPathAndExt,
            String compositeXmlFileHeaderNoExtorPath, String intfNamespace,
            String intfClass, String implClass, String implNamespace) {

        if (methods == null) {
            return null;
        }

        // Create a DOM of the methods
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        Document document = null;
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.newDocument();

            Element root = (Element) document.createElement(CPP_HEADER);

            root.setAttribute(HEADER_NAME_ATTR, source.getPath());

            document.appendChild(root);

            Element intf = (Element) document.createElement(SCA_SERVICE);
            root.appendChild(intf);

            // Go through all the signatures we have collected...
            Signature s = null;
            for (Iterator iter = methods.iterator(); iter.hasNext();) {
                s = (Signature) iter.next();

                // Each signature is an operation
                Element op = document.createElement(SCA_OPERATION);
                op.setAttribute(OPERATION_NAME_ATTR, s.getMethodName());
                intf.appendChild(op);

                Parameter[] parms = s.getParameters();
                if (parms != null) {

                    for (int i = 0; i < parms.length; i++) {
                        Element parm = (Element) document
                                .createElement(SCA_OPERATION_PARAMETER);
                        String type_string = parms[i].getTypeWithoutConst()
                                .toString();

                        type_string = type_string.replaceAll(":: ", "::");

                        Text text = document.createTextNode(type_string);
                        parm.appendChild(text);
                        parm.setAttribute(SCA_OPERATION_PARAMETER_NAME_ATTR,
                                parms[i].getName());

                        if (parms[i].getTypeWithoutConst().intern() != parms[i]
                                .getType().intern()) {
                            parm.setAttribute(
                                    SCA_OPERATION_PARAMETER_CONST_ATTR, "true");
                        }

                        op.appendChild(parm);
                        // TO DO only really stores the value
                        // unsafely/temporarily
                        // which is fine while we handle everything at the
                        // end of parsing the "leaf" that represents that actual
                        // interface/service but the below will cause the second
                        // service
                        // processed to overwite the first one in the DOM. I we
                        // wish
                        // to do some overall processing at the end we will have
                        // to
                        // use a better more XPath like key that varies by
                        // instance
                        // of the service.
                        intf.setAttribute(SCA_INTERFACE_NAME_ATTR, s
                                .getTrimClassName());
                    }
                }

                Element rc = document.createElement(SCA_OPERATION_RETURN_TYPE);
                rc.appendChild(document.createTextNode(s.getReturnType()
                        .toString().replaceAll(":: ", "::")));
                op.appendChild(rc);

                root.appendChild(intf);
            }

            // Set the name of the Service
            // here, if we are not passed one we use
            // the classname from the last header function signature...
            if (serviceName == null && s != null) {
                serviceName = s.getTrimClassName();
            }

            // this is used for the proxy file name but we need to
            // use the reference name if this is pulled in due to it
            // being a reference!!!
            if (serviceName != null)
                root.setAttribute("serviceName", serviceName);
            if (referenceName != null)
                root.setAttribute("referenceName", referenceName);
            if (nameOfSorR != null)
                root.setAttribute("nameOfSorR", nameOfSorR);

            if (implClass != null) {
                root.setAttribute("implClass", implClass);
            } else {
                root.setAttribute("implClass", compositeXmlFileHeaderNoExtorPath);
            }

            if (implClass != null) {
                root.setAttribute("implNamespace", implNamespace);
            }

            // default class name to the name of the header...
            if (headerClassName == null && source != null) {
                headerClassName = source.getName().replaceAll(
                        "\\.h|\\.hpp|\\.h++", "");
            }
            root.setAttribute("headerClassName", headerClassName);

            if (null == intfClass) {
                intfClass = headerClassName;
            }

            if (null != intfNamespace) {
                root.setAttribute("namespace", intfNamespace);
                if (null != intfClass && !intfClass.startsWith(intfNamespace)) {
                    intfClass = intfNamespace + "::" + intfClass;
                }
            }

            root.setAttribute("intfClass", intfClass);

            if (componentTypeXmlFileIntfHeaderNameWithPathAndExt == null) {
                componentTypeXmlFileIntfHeaderNameWithPathAndExt = "componentTypeHeader";
            }
            root.setAttribute("componentTypeHeader",
                    componentTypeXmlFileIntfHeaderNameWithPathAndExt);

            if (compositeXmlFileImplHeaderNameWithPathAndExt == null) {
                compositeXmlFileImplHeaderNameWithPathAndExt = "compositeXmlFileImplHeader";
            }
            root.setAttribute("compositeXmlFileHeader",
                    compositeXmlFileImplHeaderNameWithPathAndExt);

            if (compositeXmlFileHeaderNoExtorPath == null) {
                compositeXmlFileHeaderNoExtorPath = "compositeXmlFileHeaderNoExt";
            }
            root.setAttribute("compositeXmlFileHeaderNoExt",
                    compositeXmlFileHeaderNoExtorPath);

        } catch (ParserConfigurationException pce) {
            // Parser with specified options can't be built
            pce.printStackTrace();
        }

        return document;

    }

}