Home | History | Annotate | Download | only in data
      1 /*
      2  * Copyright (C) 2010 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.clearsilver.jsilver.data;
     18 
     19 import com.google.clearsilver.jsilver.exceptions.JSilverBadSyntaxException;
     20 import com.google.clearsilver.jsilver.resourceloader.ResourceLoader;
     21 
     22 import java.io.FileNotFoundException;
     23 import java.io.IOException;
     24 import java.io.Reader;
     25 
     26 /**
     27  * Loads data in Hierarchical Data Format (HDF) into Data objects.
     28  */
     29 public class HDFDataFactory implements DataFactory {
     30   private final Parser hdfParser;
     31   private final boolean ignoreAttributes;
     32 
     33   public HDFDataFactory(boolean ignoreAttributes) {
     34     this(ignoreAttributes, new NoOpStringInternStrategy());
     35   }
     36 
     37   public HDFDataFactory(boolean ignoreAttributes, StringInternStrategy stringInternStrategy) {
     38     this(NewHdfParser.newFactory(stringInternStrategy), ignoreAttributes);
     39   }
     40 
     41   public HDFDataFactory(ParserFactory hdfParserFactory, boolean ignoreAttributes) {
     42     this.ignoreAttributes = ignoreAttributes;
     43     this.hdfParser = hdfParserFactory.newInstance();
     44   }
     45 
     46   @Override
     47   public Data createData() {
     48     return new DefaultData();
     49   }
     50 
     51   @Override
     52   public void loadData(final String dataFileName, ResourceLoader resourceLoader, Data output)
     53       throws JSilverBadSyntaxException, IOException {
     54     Reader reader = resourceLoader.open(dataFileName);
     55     if (reader == null) {
     56       throw new FileNotFoundException(dataFileName);
     57     }
     58     try {
     59       hdfParser.parse(reader, output, new Parser.ErrorHandler() {
     60         public void error(int line, String lineContent, String fileName, String errorMessage) {
     61           throw new JSilverBadSyntaxException("HDF parsing error : '" + errorMessage + "'",
     62               lineContent, fileName, line, JSilverBadSyntaxException.UNKNOWN_POSITION, null);
     63         }
     64       }, resourceLoader, dataFileName, ignoreAttributes);
     65     } finally {
     66       resourceLoader.close(reader);
     67     }
     68   }
     69 
     70   @Override
     71   public Data loadData(String dataFileName, ResourceLoader resourceLoader) throws IOException {
     72     Data result = createData();
     73     loadData(dataFileName, resourceLoader, result);
     74     return result;
     75   }
     76 
     77   @Override
     78   public Parser getParser() {
     79     return hdfParser;
     80   }
     81 }
     82