001/** 002 * Copyright (C) 2007 - 2016, Jens Lehmann 003 * 004 * This file is part of DL-Learner. 005 * 006 * DL-Learner is free software; you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation; either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * DL-Learner is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package org.dllearner.algorithms.pattern; 020 021/* 022 * This file is part of the OWL API. 023 * 024 * The contents of this file are subject to the LGPL License, Version 3.0. 025 * 026 * Copyright (C) 2011, The University of Manchester 027 * 028 * This program is free software: you can redistribute it and/or modify 029 * it under the terms of the GNU General Public License as published by 030 * the Free Software Foundation, either version 3 of the License, or 031 * (at your option) any later version. 032 * 033 * This program is distributed in the hope that it will be useful, 034 * but WITHOUT ANY WARRANTY; without even the implied warranty of 035 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 036 * GNU General Public License for more details. 037 * 038 * You should have received a copy of the GNU General Public License 039 * along with this program. If not, see http://www.gnu.org/licenses/. 040 * 041 * 042 * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 043 * in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above. 044 * 045 * Copyright 2011, University of Manchester 046 * 047 * Licensed under the Apache License, Version 2.0 (the "License"); 048 * you may not use this file except in compliance with the License. 049 * You may obtain a copy of the License at 050 * 051 * http://www.apache.org/licenses/LICENSE-2.0 052 * 053 * Unless required by applicable law or agreed to in writing, software 054 * distributed under the License is distributed on an "AS IS" BASIS, 055 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 056 * See the License for the specific language governing permissions and 057 * limitations under the License. 058 */ 059 060import java.io.IOException; 061import java.io.Writer; 062import java.util.ArrayList; 063import java.util.List; 064import java.util.StringTokenizer; 065 066import org.semanticweb.owlapi.io.OWLRendererException; 067import org.semanticweb.owlapi.io.OWLRendererIOException; 068import org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax; 069import org.semanticweb.owlapi.util.ShortFormProvider; 070 071/** 072 * Author: Matthew Horridge<br> 073 * The University Of Manchester<br> 074 * Bio-Health Informatics Group<br> 075 * Date: 25-Apr-2007<br><br> 076 */ 077@SuppressWarnings("javadoc") 078public class AbstractRenderer { 079 080 private ShortFormProvider shortFormProvider; 081 082 private int lastNewLinePos = -1; 083 084 private int currentPos; 085 086 private Writer writer; 087 088 private List<Integer> tabs; 089 090 private boolean useTabbing = true; 091 092 private boolean useWrapping = true; 093 094 public AbstractRenderer(Writer writer, ShortFormProvider shortFormProvider) { 095 this.writer = writer; 096 this.shortFormProvider = shortFormProvider; 097 tabs = new ArrayList<>(); 098 pushTab(0); 099 } 100 101 public void setUseTabbing(boolean useTabbing) { 102 this.useTabbing = useTabbing; 103 } 104 105 public void setUseWrapping(boolean useWrapping) { 106 this.useWrapping = useWrapping; 107 } 108 109 public boolean isUseWrapping() { 110 return useWrapping; 111 } 112 113 public boolean isUseTabbing() { 114 return useTabbing; 115 } 116 117// public void setShortFormProvider(ShortFormProvider shortFormProvider) { 118// this.shortFormProvider = shortFormProvider; 119// } 120 121 public void flush() throws OWLRendererException { 122 try { 123 writer.flush(); 124 } 125 catch (IOException e) { 126 throw new OWLRendererIOException(e); 127 } 128 } 129 130 protected void pushTab(int size) { 131 tabs.add(0, size); 132 } 133 134 protected void incrementTab(int increment) { 135 int base = 0; 136 if(!tabs.isEmpty()) { 137 base = tabs.get(0); 138 } 139 tabs.add(0, base + increment); 140 } 141 142 protected void popTab() { 143 tabs.remove(0); 144 } 145 146 protected void writeTab() { 147 int tab = tabs.get(0); 148 for(int i = 0; i < tab; i++) { 149 write(" "); 150 } 151 } 152 153 protected int getIndent() { 154 return currentPos - lastNewLinePos - 2; 155 } 156 157 protected void write(String s) { 158 if(s == null) { 159 return; 160 } 161 int indexOfNewLine = s.indexOf('\n'); 162 if(indexOfNewLine != -1) { 163 lastNewLinePos = currentPos + indexOfNewLine; 164 } 165 currentPos += s.length(); 166 try { 167 writer.write(s); 168 } 169 catch (IOException e) { 170 e.printStackTrace(); 171 } 172 } 173 174 protected void write(char ch) { 175 write(Character.toString(ch)); 176 } 177 178 protected void write(String s, int lineLen) { 179 StringTokenizer tokenizer = new StringTokenizer(s, " \n\t-", true); 180 int currentLineLength = 0; 181 while(tokenizer.hasMoreTokens()) { 182 String curToken = tokenizer.nextToken(); 183 write(curToken); 184 if(curToken.equals("\n")) { 185 writeTab(); 186 } 187 currentLineLength += curToken.length(); 188 if(currentLineLength > lineLen && curToken.trim().length() != 0 && tokenizer.hasMoreTokens()) { 189 writeNewLine(); 190 currentLineLength = 0; 191 } 192 } 193 } 194 195 protected void writeSpace() { 196 write(" "); 197 } 198 199 protected void write(ManchesterOWLSyntax keyword) { 200 write(" ", keyword, keyword.isSectionKeyword() ? ": " : " "); 201 } 202 203 protected void writeFrameKeyword(ManchesterOWLSyntax keyword) { 204 write("", keyword, ": "); 205 } 206 207 protected void writeSectionKeyword(ManchesterOWLSyntax keyword) { 208 write(" ", keyword, ": "); 209 } 210 211 protected void writeNewLine() { 212 write("\n"); 213 if (useTabbing) { 214 writeTab(); 215 } 216 } 217 218 protected void write(String prefix, ManchesterOWLSyntax keyword, String suffix) { 219 write(prefix); 220 write(keyword.toString()); 221 write(suffix); 222 } 223 224 protected ShortFormProvider getShortFormProvider() { 225 return shortFormProvider; 226 } 227 228} 229