001    package org.maltparser.core.syntaxgraph.writer;
002    
003    
004    import org.maltparser.core.exception.MaltChainedException;
005    import org.maltparser.core.flow.FlowChartInstance;
006    import org.maltparser.core.flow.item.ChartItem;
007    import org.maltparser.core.flow.spec.ChartItemSpecification;
008    import org.maltparser.core.io.dataformat.DataFormatException;
009    import org.maltparser.core.io.dataformat.DataFormatInstance;
010    import org.maltparser.core.options.OptionManager;
011    import org.maltparser.core.syntaxgraph.TokenStructure;
012    
013    /**
014    *
015    *
016    * @author Johan Hall
017    */
018    public class WriteChartItem extends ChartItem {
019            private String outputFormatName;
020            private String outputFileName;
021            private String outputCharSet;
022            private String writerOptions;
023            private Class<? extends SyntaxGraphWriter> graphWriterClass;
024            
025            private String nullValueStrategy;
026            private String rootLabels;
027            
028            private SyntaxGraphWriter writer;
029            private String sourceName;
030            private String optiongroupName;
031            private DataFormatInstance outputDataFormatInstance;
032            private TokenStructure cachedGraph = null;
033            
034            public WriteChartItem() { super(); }
035            
036            public void initialize(FlowChartInstance flowChartinstance, ChartItemSpecification chartItemSpecification) throws MaltChainedException {
037                    super.initialize(flowChartinstance, chartItemSpecification);
038                    
039                    for (String key : chartItemSpecification.getChartItemAttributes().keySet()) {
040                            if (key.equals("source")) {
041                                    sourceName = chartItemSpecification.getChartItemAttributes().get(key);
042                            } else if (key.equals("optiongroup")) {
043                                    optiongroupName = chartItemSpecification.getChartItemAttributes().get(key);
044                            }
045                    }
046                    
047                    if (sourceName == null) {
048                            sourceName = getChartElement("write").getAttributes().get("source").getDefaultValue();
049                    } else if (optiongroupName == null) {
050                            optiongroupName = getChartElement("write").getAttributes().get("optiongroup").getDefaultValue();
051                    }
052                    
053                    setOutputFormatName(OptionManager.instance().getOptionValue(getOptionContainerIndex(), optiongroupName, "format").toString());
054                    setOutputFileName(OptionManager.instance().getOptionValue(getOptionContainerIndex(), optiongroupName, "outfile").toString());
055                    setOutputCharSet(OptionManager.instance().getOptionValue(getOptionContainerIndex(), optiongroupName, "charset").toString());
056                    setWriterOptions(OptionManager.instance().getOptionValue(getOptionContainerIndex(), optiongroupName, "writer_options").toString());
057                    setSyntaxGraphWriterClass((Class<?>)OptionManager.instance().getOptionValue(getOptionContainerIndex(), optiongroupName, "writer"));
058    
059                    setNullValueStrategy(OptionManager.instance().getOptionValue(getOptionContainerIndex(), "singlemalt", "null_value").toString());
060                    setRootLabels(OptionManager.instance().getOptionValue(getOptionContainerIndex(), "graph", "root_label").toString());
061    
062                    initOutput(getNullValueStrategy(), getRootLabels());
063                    initWriter(getSyntaxGraphWriterClass(), getOutputFileName(), getOutputCharSet(), getWriterOptions());
064            }
065            
066            public boolean preprocess() throws MaltChainedException {
067                    return true;
068            }
069            
070            public boolean process(boolean continueNextSentence) throws MaltChainedException {
071                    if (cachedGraph == null) {
072                            cachedGraph = (TokenStructure)flowChartinstance.getFlowChartRegistry(org.maltparser.core.syntaxgraph.TokenStructure.class, sourceName);
073                            writer.writeProlog();
074                    }
075                    writer.writeSentence(cachedGraph);
076                    if (!continueNextSentence) {
077                            writer.writeEpilog();
078                    }
079                    return continueNextSentence;
080            }
081            
082            public boolean postprocess() throws MaltChainedException {
083                    return true;
084            }
085            
086            public void terminate() throws MaltChainedException {
087                    if (writer != null) {
088                            writer.close();
089                            writer = null;
090                    }
091                    outputDataFormatInstance = null;
092                    cachedGraph = null;
093            }
094            
095            public String getOutputFormatName() {
096                    if (outputFormatName == null) {
097                            return "/appdata/dataformat/conllx.xml";
098                    }
099                    return outputFormatName;
100            }
101    
102            public void setOutputFormatName(String outputFormatName) {
103                    this.outputFormatName = outputFormatName;
104            }
105    
106            public String getOutputFileName() {
107                    if (outputFileName == null) {
108                            return "/dev/stdout";
109                    }
110                    return outputFileName;
111            }
112    
113            public void setOutputFileName(String outputFileName) {
114                    this.outputFileName = outputFileName;
115            }
116    
117            public String getOutputCharSet() {
118                    if (outputCharSet == null) {
119                            return "UTF-8";
120                    }
121                    return outputCharSet;
122            }
123    
124            public void setOutputCharSet(String outputCharSet) {
125                    this.outputCharSet = outputCharSet;
126            }
127    
128            public String getWriterOptions() {
129                    if (writerOptions == null) {
130                            return "";
131                    }
132                    return writerOptions;
133            }
134    
135            public void setWriterOptions(String writerOptions) {
136                    this.writerOptions = writerOptions;
137            }
138    
139            public Class<? extends SyntaxGraphWriter> getSyntaxGraphWriterClass() {
140                    return graphWriterClass;
141            }
142    
143            public void setSyntaxGraphWriterClass(Class<?> graphWriterClass) throws MaltChainedException {
144                    try {
145                            if (graphWriterClass != null) {
146                                    this.graphWriterClass = graphWriterClass.asSubclass(org.maltparser.core.syntaxgraph.writer.SyntaxGraphWriter.class);
147                            }
148                    } catch (ClassCastException e) {
149                            throw new DataFormatException("The class '"+graphWriterClass.getName()+"' is not a subclass of '"+org.maltparser.core.syntaxgraph.writer.SyntaxGraphWriter.class.getName()+"'. ", e);
150                    }
151            }
152    
153            public String getNullValueStrategy() {
154                    if (nullValueStrategy == null) {
155                            return "one";
156                    }
157                    return nullValueStrategy;
158            }
159    
160            public void setNullValueStrategy(String nullValueStrategy) {
161                    this.nullValueStrategy = nullValueStrategy;
162            }
163    
164            public String getRootLabels() {
165                    if (nullValueStrategy == null) {
166                            return "ROOT";
167                    }
168                    return rootLabels;
169            }
170    
171            public void setRootLabels(String rootLabels) {
172                    this.rootLabels = rootLabels;
173            }
174            
175            
176            public void initOutput(String nullValueStategy, String rootLabels) throws MaltChainedException {
177                    if (flowChartinstance.getDataFormatInstances().size() == 0 || flowChartinstance.getDataFormatManager().getInputDataFormatSpec() != flowChartinstance.getDataFormatManager().getOutputDataFormatSpec()) {
178                            outputDataFormatInstance = flowChartinstance.getDataFormatManager().getOutputDataFormatSpec().createDataFormatInstance(flowChartinstance.getSymbolTables(), nullValueStategy, rootLabels);
179                            if (!flowChartinstance.getDataFormatInstances().containsKey(flowChartinstance.getDataFormatManager().getOutputDataFormatSpec().getDataFormatName())) {
180                                    flowChartinstance.getDataFormatInstances().put(flowChartinstance.getDataFormatManager().getOutputDataFormatSpec().getDataFormatName(), outputDataFormatInstance);
181                            }
182                    } else {
183                            outputDataFormatInstance = flowChartinstance.getDataFormatInstances().get(flowChartinstance.getDataFormatManager().getInputDataFormatSpec().getDataFormatName());
184                    }
185            }
186            
187            public void initWriter(Class<? extends SyntaxGraphWriter> syntaxGraphWriterClass, String outputFile, String outputCharSet, 
188                            String writerOption) throws MaltChainedException {
189                    try {   
190                            writer = syntaxGraphWriterClass.newInstance();
191                            if (outputFile == null || outputFile.length() == 0 || outputFile.equals("/dev/stdout")) {
192                                    writer.open(System.out, outputCharSet);
193                            } else {
194                                    writer.open(outputFile, outputCharSet);
195                            }
196                            writer.setDataFormatInstance(outputDataFormatInstance);
197                            writer.setOptions(writerOption);
198                    } catch (InstantiationException e) {
199                            throw new DataFormatException("The data writer '"+syntaxGraphWriterClass.getName()+"' cannot be initialized. ", e);
200                    } catch (IllegalAccessException e) {
201                            throw new DataFormatException("The data writer '"+syntaxGraphWriterClass.getName()+"' cannot be initialized. ", e);
202                    }
203            }
204    
205            public Class<? extends SyntaxGraphWriter> getGraphWriterClass() {
206                    return graphWriterClass;
207            }
208    
209            public SyntaxGraphWriter getWriter() {
210                    return writer;
211            }
212    
213            public String getSourceName() {
214                    return sourceName;
215            }
216    
217            public DataFormatInstance getOutputDataFormatInstance() {
218                    return outputDataFormatInstance;
219            }
220            
221            public boolean equals(Object obj) {
222                    if (this == obj)
223                            return true;
224                    if (obj == null)
225                            return false;
226                    if (getClass() != obj.getClass())
227                            return false;
228                    return obj.toString().equals(this.toString());
229            }
230            
231            public int hashCode() {
232                    return 217 + (null == toString() ? 0 : toString().hashCode());
233            }
234            
235            public String toString() {
236                    StringBuilder sb = new StringBuilder();
237                    sb.append("    write ");
238                    sb.append("source:");
239                    sb.append(sourceName);
240                    sb.append(' ');
241                    sb.append("optiongroup:");
242                    sb.append(optiongroupName);
243                    return sb.toString();
244            }
245    }