KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > dom > generic > GenericDocBuilderGenerator


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: GenericDocBuilderGenerator.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.dom.generic;
25
26 import org.enhydra.xml.xmlc.XMLCException;
27 import org.enhydra.xml.xmlc.codegen.JavaClass;
28 import org.enhydra.xml.xmlc.codegen.JavaCode;
29 import org.enhydra.xml.xmlc.codegen.JavaLang;
30 import org.enhydra.xml.xmlc.codegen.JavaMethod;
31 import org.enhydra.xml.xmlc.compiler.ElementTable;
32 import org.enhydra.xml.xmlc.dom.AccessorGenerator;
33 import org.enhydra.xml.xmlc.dom.DocBuilderGenerator;
34 import org.enhydra.xml.xmlc.dom.XMLCDocument;
35 import org.w3c.dom.Document JavaDoc;
36
37 /**
38  * Class to generate code to build the document tree using only the W3C DOM
39  * interface. The document building code is generated in several functions to
40  * avoid the limit on the maximum size of a method.
41  */

42 abstract public class GenericDocBuilderGenerator implements DocBuilderGenerator {
43     /*
44      * Maximum creation-cost per build method. One unit is approximately the
45      * cost to create one node.
46      */

47     private static final int MAX_CREATE_COST_PER_BUILD_METHOD = 512;
48
49     /*
50      * Name of variable in init function containing Document.
51      */

52     private static final String JavaDoc DOCUMENT_VAR_NAME = "document";
53
54     /**
55      * Base name of DOM building methods.
56      */

57     private static final String JavaDoc SUBDOCUMENT_BUILD_METHOD = "buildSubDocument";
58
59     /**
60      * Generate the document builder method.
61      * @see DocBuilderGenerator#createBuildDocumentMethod
62      */

63     public void createBuildDocumentMethod(XMLCDocument xmlcDoc,
64                                           AccessorGenerator accessorGenerator,
65                                           ElementTable elementTable,
66                                           JavaClass docClass,
67                                           JavaMethod buildDocumentMethod) throws XMLCException {
68         // Create code to create DOM.
69
DOMBuilderGenerator domBuilderGenerator
70             = new DOMBuilderGenerator(SUBDOCUMENT_BUILD_METHOD,
71                                       xmlcDoc.getDocument(),
72                                       Document.class.getName(),
73                                       new NodeCreateGenerator(xmlcDoc),
74                                       accessorGenerator,
75                                       elementTable,
76                                       docClass,
77                                       MAX_CREATE_COST_PER_BUILD_METHOD,
78                                       false);
79         // Generate the document building method.
80
JavaCode body = buildDocumentMethod.getCode();
81         body.add(xmlcDoc.getDocument().getClass().getName() + " "
82                  + DOCUMENT_VAR_NAME + " = ");
83         domBuilderGenerator.createMethodCall(body);
84
85         // Save document information.
86
body.addln("setDocument(" + DOCUMENT_VAR_NAME + ","
87                    + JavaLang.createStringConst(xmlcDoc.getDomFactory().getMIMEType()) + ", "
88                    + JavaLang.createStringConst(xmlcDoc.getEncoding()) + ");");
89         body.addln();
90     }
91 }
92
Popular Tags