Changeset 12594


Ignore:
Timestamp:
2006-08-30T10:20:19+12:00 (18 years ago)
Author:
shaoqun
Message:

added code that uses MARCXML mapping file

Location:
trunk/gsdl
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/src/java/org/nzdl/gsdl/ApplyXSLT.java

    r12395 r12594  
    1010import javax.xml.transform.stream.StreamSource;
    1111
     12import javax.xml.parsers.*;
     13import javax.xml.transform.dom.*;
     14import org.w3c.dom.*;
     15
     16
     17
    1218/**
    1319 *  Use the TraX interface to perform a transformation in the simplest manner possible
     
    2127    public static final String INPUT_END = new String ("<?Done?>");
    2228
     29    private static final String RECORD_ELEMENT = "record";
     30    private static final String CONTROLFIELD_ELEMENT = "controlfield";
     31    private static final String SUBFIELD_ELEMENT = "subfield";
     32    private static final String LEADER_ELEMENT = "leader";
     33
    2334    private final int BEFORE_READING = 0;
    2435    private final int IS_READING = 1;
    2536    private String xsl_file;
     37    private String mapping_file;
    2638
    2739    public ApplyXSLT(String xsl_file)
    2840    {
    2941    this.xsl_file = xsl_file;
     42    }
     43
     44    public ApplyXSLT(String xsl_file,String mapping_file){
     45    this.xsl_file = xsl_file;
     46    this.mapping_file = mapping_file;
    3047    }
    3148
     
    4158
    4259    try{
    43         while (br.ready())
    44         {
    45                    
    46             String this_line = br.readLine();
    47                      
    48                        
    49             if(system_status == BEFORE_READING)
    50             {
    51                
    52                 if(this_line.compareTo(DOC_START) == 0)
    53                 {
    54                     output_file = br.readLine(); // read the next line as the output file name
    55                     system_status = IS_READING;
    56                     a_doc = "";
    57                 }
    58                 else if(this_line.compareTo(INPUT_END) == 0)
    59                 {
    60                     return true;
    61                 }
    62                 else
    63                 {
    64                      
    65                     System.err.println("Undefined process status:" + this_line);
    66                     system_status = BEFORE_READING;
    67                 }
    68                
     60        while (br.ready()) {
     61                           
     62        String this_line = br.readLine();
     63        if(system_status == BEFORE_READING){
     64            if(this_line.compareTo(DOC_START) == 0){
     65            output_file = br.readLine(); // read the next line as the output file name
     66            system_status = IS_READING;
     67            a_doc = "";
     68            }
     69            else if(this_line.compareTo(INPUT_END) == 0){
     70            return true;
     71            }
     72            else{
     73            System.err.println("Undefined process status:" + this_line);
     74            system_status = BEFORE_READING;
     75            }
     76           
     77        }
     78        else if(system_status == IS_READING){
     79            if(this_line.compareTo(DOC_END) == 0){
     80            boolean result = false;
     81            if (mapping_file !=null && !mapping_file.equals("")){
     82                result = translateXMLWithMapping(a_doc,output_file);               
    6983            }
    70             else if(system_status == IS_READING)
    71             {
    72                 if(this_line.compareTo(DOC_END) == 0)
    73                 {
    74                    
    75                     if(translateXML(a_doc,xsl_file,output_file) == false)
    76                     {
    77                         System.err.println("Translation Failed!!");
    78                         return false;
    79                     }
    80                     system_status = BEFORE_READING;
    81                    
    82                 }
    83                 else
    84                 {
    85                        a_doc = a_doc + this_line + "\n";
    86                                        
    87                 }
     84            else{
     85                result = translateXML(a_doc,output_file);
    8886            }
    89             else
    90             {
    91              
    92                 System.err.println ("Undefined system status in ApplyXSLT.java main().");
    93                 System.exit(-1);
     87           
     88            if (!result){
     89                System.err.println("Translation Failed!!");
     90                return false;
    9491            }
    95                
     92           
     93            system_status = BEFORE_READING;
     94           
     95            }
     96            else{
     97            a_doc = a_doc + this_line + "\n";
     98           
     99            }
    96100        }
     101        else{         
     102            System.err.println ("Undefined system status in ApplyXSLT.java main().");
     103            System.exit(-1);
     104            }
     105       
     106        }
    97107    }catch (Exception e)
    98108        {
     
    102112    return false;
    103113    }
    104 
    105 
    106     private boolean translateXML(String full_doc, String xsl_file, String output_file)
     114   
     115   
     116    private boolean translateXML(String full_doc, String output_file)
    107117    throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException
    108118    {
    109        
     119   
    110120    StringReader str = new StringReader(full_doc) ;
    111121   
    112     TransformerFactory tFactory = TransformerFactory.newInstance();
     122    TransformerFactory tFactory = TransformerFactory.newInstance();
    113123    Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file));
    114124    transformer.transform(new StreamSource(str), new StreamResult(new FileOutputStream(output_file)));
     
    116126    }
    117127
     128    private boolean translateXMLWithMapping(String full_doc, String output_file)
     129    throws IOException,TransformerException, TransformerConfigurationException, FileNotFoundException
     130    {   
     131    StringReader str = new StringReader(full_doc) ;
     132
     133    try{
     134        TransformerFactory tFactory = TransformerFactory.newInstance();
     135        Transformer transformer = tFactory.newTransformer(new StreamSource(xsl_file));
     136       
     137        Document mapping_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(mapping_file);
     138        Element mapping =mapping_doc.getDocumentElement();
     139       
     140        transformer.setParameter("mapping",mapping);
     141       
     142        Document output_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
     143       
     144        transformer.transform(new StreamSource(str), new DOMResult(output_doc));
     145       
     146        calculateRecordsLength(output_doc);
     147       
     148        transformer = tFactory.newTransformer();
     149       
     150        transformer.transform(new DOMSource(output_doc), new StreamResult(new FileOutputStream(output_file)));
     151
     152    }
     153    catch(Exception e){
     154        e.printStackTrace();
     155        return false;
     156    }
     157
     158    return true;
     159    }
     160
     161    private void calculateRecordsLength(Document output_doc){
     162    NodeList records = output_doc.getDocumentElement().getElementsByTagName(RECORD_ELEMENT);
     163
     164    for(int i=0;i<records.getLength();i++){
     165        Element record = (Element)records.item(i);
     166        calculateRecordLength(record);
     167    }
     168    }
     169
     170    private void calculateRecordLength(Element record){
     171    int total_length =0;   
     172    NodeList controlfileds = record.getElementsByTagName(CONTROLFIELD_ELEMENT);
     173    for(int i=0;i<controlfileds.getLength();i++){
     174        Element controlfiled = (Element)controlfileds.item(i);
     175        total_length +=getElementTextValue(controlfiled).length();
     176    }
     177   
     178    NodeList subfileds = record.getElementsByTagName(SUBFIELD_ELEMENT);
     179    for(int i=0;i<subfileds.getLength();i++){
     180        Element subfiled = (Element)subfileds.item(i);
     181        total_length +=getElementTextValue(subfiled).length();
     182    }
     183
     184    String record_length = total_length+"";
     185    //fill in a extra digit as record length needs to be five characters long
     186    if (total_length < 10000){
     187        record_length = "0"+record_length;
     188        if (total_length < 1000){
     189        record_length = "0"+record_length; 
     190        }
     191        if (total_length < 100){
     192        record_length = "0"+record_length; 
     193        }
     194        if (total_length < 10){
     195         record_length = "0"+record_length;
     196        }
     197       
     198    }
     199
     200    NodeList leaders = record.getElementsByTagName(LEADER_ELEMENT);
     201   
     202    //only one leader element
     203        if (leaders.getLength() >0){
     204        Element leader_element = (Element)leaders.item(0);
     205        removeFirstTextNode(leader_element);
     206        leader_element.insertBefore(leader_element.getOwnerDocument().createTextNode(record_length),leader_element.getFirstChild());
     207    }
     208
     209    }
     210
     211    private void removeFirstTextNode(Element element){
     212    //remove the first text node
     213    NodeList children_nodelist = element.getChildNodes();
     214    for (int i = 0; i < children_nodelist.getLength(); i++) {
     215        Node child_node = children_nodelist.item(i);
     216        if (child_node.getNodeType() == Node.TEXT_NODE) {
     217        element.removeChild(child_node);
     218        return;
     219        }
     220    }
     221
     222    }
     223
     224    private String getElementTextValue(Element element)
     225    {
     226    String text ="";   
     227
     228    // Find the node child
     229    NodeList children_nodelist = element.getChildNodes();
     230    for (int i = 0; i < children_nodelist.getLength(); i++) {
     231        Node child_node = children_nodelist.item(i);
     232        if (child_node.getNodeType() == Node.TEXT_NODE) {
     233        text +=child_node.getNodeValue();
     234        }
     235    }
     236
     237        return text;
     238    }
     239 
     240
     241    private void setMappingVariable(Document style_doc){
     242    Node child = style_doc.getDocumentElement().getFirstChild();
     243    while(child != null) {
     244        String name = child.getNodeName();
     245        if (name.equals("xsl:variable")) {
     246        Element variable_element = (Element)child;
     247        if ( variable_element.getAttribute("name").trim().equals("mapping")){
     248            variable_element.setAttribute("select","document('"+mapping_file+"')/Mapping");
     249        }
     250        }
     251        child = child.getNextSibling();
     252    }
     253   
     254    }
     255
     256
    118257    public static void main(String[] args)
    119258    {
    120259    // Checking Arguments
    121     if(args.length != 1)
     260    if(args.length < 1)
    122261        {
    123262        System.err.println("Must provide a XSL file !");
    124263        System.exit(-1);
    125264        }
    126     ApplyXSLT core = new ApplyXSLT(args[0]);
     265    ApplyXSLT core = null;
     266
     267    if (args.length == 1){
     268         core = new ApplyXSLT(args[0]);
     269    }
     270    else if(args.length == 2){
     271         core = new ApplyXSLT(args[0],args[1]);
     272    }
    127273
    128274    if(core.process())
Note: See TracChangeset for help on using the changeset viewer.