List of usage examples for java.lang StringBuffer setLength
@Override public synchronized void setLength(int newLength)
From source file:org.apache.nutch.parse.tika.TikaParser.java
@SuppressWarnings("deprecation") ParseResult getParse(Content content, HTMLDocumentImpl doc, DocumentFragment root) { String mimeType = content.getContentType(); boolean useBoilerpipe = getConf().get("tika.extractor", "none").equals("boilerpipe"); String boilerpipeExtractorName = getConf().get("tika.extractor.boilerpipe.algorithm", "ArticleExtractor"); URL base;//w w w . j a v a 2 s .c om try { base = new URL(content.getBaseUrl()); } catch (MalformedURLException e) { return new ParseStatus(e).getEmptyParseResult(content.getUrl(), getConf()); } // get the right parser using the mime type as a clue Parser parser = tikaConfig.getParser(MediaType.parse(mimeType)); byte[] raw = content.getContent(); if (parser == null) { String message = "Can't retrieve Tika parser for mime-type " + mimeType; LOG.error(message); return new ParseStatus(ParseStatus.FAILED, message).getEmptyParseResult(content.getUrl(), getConf()); } LOG.debug("Using Tika parser " + parser.getClass().getName() + " for mime-type " + mimeType); Metadata tikamd = new Metadata(); ContentHandler domHandler; // Check whether to use Tika's BoilerplateContentHandler if (useBoilerpipe) { BoilerpipeContentHandler bpHandler = new BoilerpipeContentHandler( (ContentHandler) new DOMBuilder(doc, root), BoilerpipeExtractorRepository.getExtractor(boilerpipeExtractorName)); bpHandler.setIncludeMarkup(true); domHandler = (ContentHandler) bpHandler; } else { DOMBuilder domBuilder = new DOMBuilder(doc, root); domBuilder.setUpperCaseElementNames(upperCaseElementNames); domBuilder.setDefaultNamespaceURI(XHTMLContentHandler.XHTML); domHandler = (ContentHandler) domBuilder; } LinkContentHandler linkContentHandler = new LinkContentHandler(); ParseContext context = new ParseContext(); TeeContentHandler teeContentHandler = new TeeContentHandler(domHandler, linkContentHandler); if (HTMLMapper != null) context.set(HtmlMapper.class, HTMLMapper); tikamd.set(Metadata.CONTENT_TYPE, mimeType); try { parser.parse(new ByteArrayInputStream(raw), (ContentHandler) teeContentHandler, tikamd, context); } catch (Exception e) { LOG.error("Error parsing " + content.getUrl(), e); return new ParseStatus(ParseStatus.FAILED, e.getMessage()).getEmptyParseResult(content.getUrl(), getConf()); } HTMLMetaTags metaTags = new HTMLMetaTags(); String text = ""; String title = ""; Outlink[] outlinks = new Outlink[0]; org.apache.nutch.metadata.Metadata nutchMetadata = new org.apache.nutch.metadata.Metadata(); // we have converted the sax events generated by Tika into a DOM object // so we can now use the usual HTML resources from Nutch // get meta directives HTMLMetaProcessor.getMetaTags(metaTags, root, base); if (LOG.isTraceEnabled()) { LOG.trace("Meta tags for " + base + ": " + metaTags.toString()); } // check meta directives if (!metaTags.getNoIndex()) { // okay to index StringBuffer sb = new StringBuffer(); if (LOG.isTraceEnabled()) { LOG.trace("Getting text..."); } utils.getText(sb, root); // extract text text = sb.toString(); sb.setLength(0); if (LOG.isTraceEnabled()) { LOG.trace("Getting title..."); } utils.getTitle(sb, root); // extract title title = sb.toString().trim(); } if (!metaTags.getNoFollow()) { // okay to follow links ArrayList<Outlink> l = new ArrayList<Outlink>(); // extract outlinks URL baseTag = base; String baseTagHref = tikamd.get("Content-Location"); if (baseTagHref != null) { try { baseTag = new URL(base, baseTagHref); } catch (MalformedURLException e) { LOG.trace("Invalid <base href=\"{}\">", baseTagHref); } } if (LOG.isTraceEnabled()) { LOG.trace("Getting links (base URL = {}) ...", baseTag); } // pre-1233 outlink extraction //utils.getOutlinks(baseTag != null ? baseTag : base, l, root); // Get outlinks from Tika List<Link> tikaExtractedOutlinks = linkContentHandler.getLinks(); utils.getOutlinks(baseTag, l, tikaExtractedOutlinks); outlinks = l.toArray(new Outlink[l.size()]); if (LOG.isTraceEnabled()) { LOG.trace("found " + outlinks.length + " outlinks in " + content.getUrl()); } } // populate Nutch metadata with Tika metadata String[] TikaMDNames = tikamd.names(); for (String tikaMDName : TikaMDNames) { if (tikaMDName.equalsIgnoreCase(Metadata.TITLE)) continue; String[] values = tikamd.getValues(tikaMDName); for (String v : values) nutchMetadata.add(tikaMDName, v); } // no outlinks? try OutlinkExtractor e.g works for mime types where no // explicit markup for anchors if (outlinks.length == 0) { outlinks = OutlinkExtractor.getOutlinks(text, getConf()); } ParseStatus status = new ParseStatus(ParseStatus.SUCCESS); if (metaTags.getRefresh()) { status.setMinorCode(ParseStatus.SUCCESS_REDIRECT); status.setArgs(new String[] { metaTags.getRefreshHref().toString(), Integer.toString(metaTags.getRefreshTime()) }); } ParseData parseData = new ParseData(status, title, outlinks, content.getMetadata(), nutchMetadata); ParseResult parseResult = ParseResult.createParseResult(content.getUrl(), new ParseImpl(text, parseData)); // run filters on parse ParseResult filteredParse = this.htmlParseFilters.filter(content, parseResult, metaTags, root); if (metaTags.getNoCache()) { // not okay to cache for (Map.Entry<org.apache.hadoop.io.Text, Parse> entry : filteredParse) entry.getValue().getData().getParseMeta().set(Nutch.CACHING_FORBIDDEN_KEY, cachingPolicy); } return filteredParse; }
From source file:org.pentaho.platform.plugin.action.eclipsebirt.BirtSystemListener.java
private IReportEngine createBIRTEngine() { try {//w ww . j a v a 2s . c om // Get the global settings for the BIRT engine from our system settings String birtHome = PentahoSystem.getApplicationContext().getSolutionPath("system/BIRT"); //$NON-NLS-1$ birtHome = birtHome.replaceAll("\\\\.\\\\", "\\\\"); //$NON-NLS-1$ //$NON-NLS-2$ String birtResourcePath = PentahoSystem.getApplicationContext() .getSolutionPath("system/BIRT/resources"); //$NON-NLS-1$ birtResourcePath = birtResourcePath.replaceAll("\\\\.\\\\", "\\\\"); //$NON-NLS-1$//$NON-NLS-2$ if (PentahoSystem.debug) { Logger.debug(BirtSystemListener.class.getName(), Messages.getString("BIRT.DEBUG_BIRT_HOME", birtHome)); //$NON-NLS-1$ } // Create an appropriate Config object EngineConfig config = new EngineConfig(); config.setEngineHome(birtHome); // Configuring where BIRT engine is installed config.setResourcePath(birtResourcePath); // Set the directory where the BIRT log files will go String logDest = PentahoSystem.getApplicationContext().getFileOutputPath("system/logs/BIRT"); //$NON-NLS-1$ // Set the logging level int loggingLevel = Logger.getLogLevel(); if (loggingLevel == ILogger.TRACE) { config.setLogConfig(logDest, Level.ALL); } else if (loggingLevel == ILogger.DEBUG) { config.setLogConfig(logDest, Level.FINE); } else if (loggingLevel == ILogger.INFO) { config.setLogConfig(logDest, Level.INFO); } else if (loggingLevel == ILogger.WARN) { config.setLogConfig(logDest, Level.WARNING); } else if (loggingLevel == ILogger.ERROR) { config.setLogConfig(logDest, Level.SEVERE); } else if (loggingLevel == ILogger.FATAL) { config.setLogConfig(logDest, Level.SEVERE); } // Register new image handler // Bart Maertens, 14/11/2007: Replace HTMLEmitterConfig with HTMLRenderOption. // HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig(); // emitterConfig.setActionHandler(new HTMLActionHandler()); // emitterConfig.setImageHandler(new HTMLServerImageHandler()); // config.getEmitterConfigs().put(RenderOptionBase.OUTPUT_FORMAT_HTML, emitterConfig); IRenderOption option = new RenderOption(); option.setOutputFormat("html"); //$NON-NLS-1$ HTMLRenderOption renderOption = new HTMLRenderOption(option); renderOption.setImageDirectory(imageDirectory); config.getEmitterConfigs().put(IRenderOption.OUTPUT_FORMAT_HTML, renderOption); // Workaround for Eclipse bug 156877 String protocolHandler = System.getProperty("java.protocol.handler.pkgs"); //$NON-NLS-1$ if ((protocolHandler != null) && (protocolHandler.indexOf("org.jboss.net.protocol") != -1)) { //$NON-NLS-1$ StringTokenizer tok = new StringTokenizer(protocolHandler, "|"); //$NON-NLS-1$ StringBuffer newProtocolHandler = new StringBuffer(); String name; while (tok.hasMoreElements()) { name = tok.nextToken(); if (!name.equals("org.jboss.net.protocol")) { //$NON-NLS-1$ newProtocolHandler.append(name).append('|'); } } newProtocolHandler.setLength(Math.max(0, newProtocolHandler.length() - 1)); // chop the last '|' BirtSystemListener.workaroundProtocolHandler = System.setProperty("java.protocol.handler.pkgs", //$NON-NLS-1$ newProtocolHandler.toString()); } // End Workaround Platform.startup(config); IReportEngineFactory factory = (IReportEngineFactory) Platform .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY); IReportEngine engine = factory.createReportEngine(config); return engine; } catch (BirtException be) { BirtSystemListener.logger.error(null, be); Logger.error(BirtSystemListener.class.getName(), Messages.getErrorString("BIRT.ERROR_0008_INVALID_CONFIGURATION")); //$NON-NLS-1$ } return null; }
From source file:org.apache.jasper.compiler.JspReader.java
/** * Parse a space delimited token./*w w w .ja v a 2s .c o m*/ * If quoted the token will consume all characters up to a matching quote, * otherwise, it consumes up to the first delimiter character. * * @param quoted If <strong>true</strong> accept quoted strings. */ String parseToken(boolean quoted) throws JasperException { StringBuffer stringBuffer = new StringBuffer(); skipSpaces(); stringBuffer.setLength(0); int ch = peekChar(); if (quoted) { if (ch == '"' || ch == '\'') { char endQuote = ch == '"' ? '"' : '\''; // Consume the open quote: ch = nextChar(); for (ch = nextChar(); ch != -1 && ch != endQuote; ch = nextChar()) { if (ch == '\\') ch = nextChar(); stringBuffer.append((char) ch); } // Check end of quote, skip closing quote: if (ch == -1) { err.jspError(mark(), "jsp.error.quotes.unterminated"); } } else { err.jspError(mark(), "jsp.error.attr.quoted"); } } else { if (!isDelimiter()) { // Read value until delimiter is found: do { ch = nextChar(); // Take care of the quoting here. if (ch == '\\') { if (peekChar() == '"' || peekChar() == '\'' || peekChar() == '>' || peekChar() == '%') ch = nextChar(); } stringBuffer.append((char) ch); } while (!isDelimiter()); } } return stringBuffer.toString(); }
From source file:com.digitalgeneralists.assurance.ui.components.ResultsPanel.java
public void notify(IAssuranceEvent event) { StringBuffer loggingMessage = new StringBuffer(256); logger.info(loggingMessage.append("ResultsPanel received event: ").append(event)); loggingMessage.setLength(0); loggingMessage = null;/*ww w . j a v a2s . c om*/ if (event instanceof SelectedScanChangedEvent) { this.resetPanel(); StringBuilder labelText = new StringBuilder(512); this.resultsLabel .setText(labelText.append("Results for ").append(event.getSource().toString()).toString()); labelText.setLength(0); labelText = null; } if (event instanceof ScanStartedEvent) { this.resetPanel(); StringBuilder labelText = new StringBuilder(512); this.resultsLabel.setText( labelText.append("Loading results for ").append(event.getSource().toString()).toString()); labelText.setLength(0); labelText = null; this.add(this.progressIndicator, this.progressIndicatorConstraints); this.validate(); this.progressIndicator.repaint(); } if (event instanceof ScanCompletedEvent) { this.applicationDelegate.loadScanResults((Scan) event.getSource()); this.remove(this.progressIndicator); this.validate(); } if (event instanceof ScanProgressEvent) { this.resultsLabel.setText(event.getSource().toString()); this.validate(); } if (event instanceof ScanResultsLoadedEvent) { int selectedResultIndex = this.resultsTable.getSelectedRow(); Scan scan = (Scan) event.getSource(); // NOTE: Leaking model initialization like this into the UI is less than ideal. scan = (Scan) ModelUtils.initializeEntity(scan, Scan.RESULTS_PROPERTY); Collection<ComparisonResult> list = scan.getUnmodifiableResults(); // NOTE: Swapping the list model out to suppress change // notifications during a data reload feels less than ideal. this.resultsTable.setModel(new DefaultTableModel()); this.resultsTableModel.removeAllElements(); for (ComparisonResult result : list) { this.resultsTableModel.addRow(result); result = null; } this.resultsTable.setModel(this.resultsTableModel); this.conditionallyShowMessagePanel(list.size() == 0, "The source and target locations are identical."); list = null; StringBuilder labelText = new StringBuilder(512); this.resultsLabel.setText(labelText.append("Results for ").append(scan.toString()).toString()); labelText.setLength(0); labelText = null; if ((selectedResultIndex >= 0) && (this.resultsTableModel.getRowCount() > selectedResultIndex)) { this.resultsTable.setRowSelectionInterval(selectedResultIndex, selectedResultIndex); } scan = null; } if ((event instanceof ResultMergeCompletedEvent) || (event instanceof DeletedItemRestoreCompletedEvent)) { for (int index = 0; index < this.resultsTableModel.getRowCount(); index++) { List<List<ComparisonResult>> resultList = new ArrayList<List<ComparisonResult>>( this.resultsTableModel.data); if (((ComparisonResult) event.getSource()).getId() == resultList.get(index).get(0).getId()) { this.resultsTableModel.updateRow(index, (ComparisonResult) event.getSource()); } resultList = null; } } if (event instanceof ScanMergeStartedEvent) { this.resetPanel(); StringBuilder labelText = new StringBuilder(512); this.resultsLabel.setText(labelText.append("Merging ").append(event.getSource().toString()).toString()); labelText.setLength(0); labelText = null; this.add(this.progressIndicator, this.progressIndicatorConstraints); this.validate(); this.progressIndicator.repaint(); StringBuilder message = new StringBuilder(512); this.conditionallyShowMessagePanel(true, message.append("Merging ").append(event.getSource().toString()).toString()); message.setLength(0); message = null; } if (event instanceof ScanMergeProgressEvent) { this.resultsLabel.setText(event.getSource().toString()); this.validate(); } if (event instanceof ScanMergeCompletedEvent) { this.applicationDelegate.loadScanResults((Scan) event.getSource()); this.remove(this.progressIndicator); this.validate(); } }
From source file:com.concursive.connect.web.modules.wiki.utils.WikiToHTMLUtils.java
protected static void flushData(StringBuffer data, StringBuffer sb) { if (data.length() > 0) { String value = data.toString(); value = processMarkupCharacters(value); sb.append(StringUtils.toHtmlValue(value, false, false)); data.setLength(0); }/*from w w w. j a v a 2 s . c o m*/ }
From source file:com.github.maven_nar.cpptasks.compiler.CommandLineCompiler.java
protected void buildDefineArguments(final CompilerDef[] defs, final Vector<String> args) { ////from w w w .java2s . c o m // assume that we aren't inheriting defines from containing <cc> // UndefineArgument[] merged = defs[0].getActiveDefines(); for (int i = 1; i < defs.length; i++) { // // if we are inheriting, merge the specific defines with the // containing defines merged = UndefineArgument.merge(defs[i].getActiveDefines(), merged); } final StringBuffer buf = new StringBuffer(30); for (final UndefineArgument current : merged) { buf.setLength(0); if (current.isDefine()) { getDefineSwitch(buf, current.getName(), current.getValue()); } else { getUndefineSwitch(buf, current.getName()); } args.addElement(buf.toString()); } }
From source file:com.vladium.emma.report.html.ReportGenerator.java
private static void trimForDisplay(final StringBuffer buf) { if (buf.length() > MAX_DISPLAY_NAME_LENGTH) { buf.setLength(MAX_DISPLAY_NAME_LENGTH - 3); buf.append("..."); }/*from ww w . j a v a 2 s .c om*/ }
From source file:com.tremolosecurity.proxy.ConfigSys.java
private void procData(HttpServletResponse resp, UrlHolder holder, boolean isText, InputStream ins) throws IOException, Exception { byte[] buffer = new byte[1024]; //InputStream in = entity.getContent(); int len;//from w w w . j a va 2 s. co m if (isText) { BufferedReader in = new BufferedReader(new InputStreamReader(ins)); PrintWriter out = resp.getWriter(); //OutputStream out = resp.getOutputStream(); String line; StringBuffer lineBuff = new StringBuffer(); StringBuffer data = new StringBuffer(); while ((line = in.readLine()) != null) { lineBuff.setLength(0); lineBuff.append(line); try { out.println(line); //out.write(line.getBytes("UTF-8")); //out.write("\n".getBytes("UTF-8")); } catch (Exception e) { //do nothing } //out.flush(); } } else { //req.setAttribute(ProxySys.TREMOLO_BINARY_DATA, baos.toByteArray()); //InputStream in = entity.getContent(); OutputStream out = resp.getOutputStream(); while ((len = ins.read(buffer)) != -1) { out.write(buffer, 0, len); } //out.close(); } }
From source file:CircularGenerator.java
private void extendFastA(File inputFasta) { // create the new files String fileExtension = com.google.common.io.Files.getFileExtension(inputFasta.getAbsolutePath()); String fileName = com.google.common.io.Files.getNameWithoutExtension(inputFasta.getAbsolutePath()); String filePath = inputFasta.getAbsolutePath().substring(0, inputFasta.getAbsolutePath().lastIndexOf(File.separator)); File output = new File(filePath + "/" + fileName + "_" + this.elongationfactor + "." + fileExtension); File outputChanged = new File(inputFasta.getAbsoluteFile() + "_" + this.elongationfactor + "_elongated"); PrintWriter out;//from w w w. j a v a 2s . c o m // read the sequences try { out = new PrintWriter(new BufferedWriter(new FileWriter(output, false))); PrintWriter outChanged = new PrintWriter(new BufferedWriter(new FileWriter(outputChanged, false))); @SuppressWarnings("resource") BufferedReader br = new BufferedReader(new FileReader(inputFasta)); String currLine = ""; String header = ""; Boolean firstEntry = true; StringBuffer sequence = new StringBuffer(); // read the fasta file entry by entry while ((currLine = br.readLine()) != null) { // start of new sequence if (currLine.startsWith(">")) { if (!firstEntry) { extendSequence(header, sequence, out, outChanged); } // reset sequence firstEntry = false; header = currLine.substring(1); sequence.setLength(0); } else { sequence.append(currLine); } } // modify last entry extendSequence(header, sequence, out, outChanged); // close the writers out.flush(); outChanged.flush(); out.close(); outChanged.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.clustercontrol.infra.view.action.DeleteInfraModuleAction.java
@Override public Object execute(ExecutionEvent event) throws ExecutionException { this.window = HandlerUtil.getActiveWorkbenchWindow(event); // In case this action has been disposed if (null == this.window || !isEnabled()) { return null; }//from w ww . j ava2 s. c om // ??? this.viewPart = HandlerUtil.getActivePart(event); if (!(viewPart instanceof InfraModuleView)) { return null; } InfraModuleView infraModuleView = null; try { infraModuleView = (InfraModuleView) viewPart.getAdapter(InfraModuleView.class); } catch (Exception e) { m_log.info("execute " + e.getMessage()); return null; } if (infraModuleView == null) { m_log.info("execute: view is null"); return null; } StructuredSelection selection = null; if (infraModuleView.getComposite().getTableViewer().getSelection() instanceof StructuredSelection) { selection = (StructuredSelection) infraModuleView.getComposite().getTableViewer().getSelection(); } List<String> moduleIds = new ArrayList<String>(); StringBuffer strModuleIds = new StringBuffer(); String tmpModuleId = null; if (selection != null) { for (Object object : selection.toList()) { tmpModuleId = (String) ((ArrayList<?>) object).get(GetInfraModuleTableDefine.MODULE_ID); moduleIds.add(tmpModuleId); strModuleIds.append(tmpModuleId + ", "); } strModuleIds.setLength(strModuleIds.length() - 2); } InfraManagementInfo info = null; try { InfraEndpointWrapper wrapper = InfraEndpointWrapper .getWrapper(infraModuleView.getComposite().getManagerName()); info = wrapper.getInfraManagement(infraModuleView.getComposite().getManagementId()); } catch (HinemosUnknown_Exception | InvalidRole_Exception | InvalidUserPass_Exception | NotifyNotFound_Exception | InfraManagementNotFound_Exception e) { m_log.debug("execute getInfraManagement, " + e.getMessage()); } List<InfraModuleInfo> tmpDeleteList = new ArrayList<InfraModuleInfo>(); if (info != null && info.getModuleList() != null) { for (String moduleId : moduleIds) { for (InfraModuleInfo module : info.getModuleList()) { if (module.getModuleId().equals(moduleId)) { tmpDeleteList.add(module); break; } } } info.getModuleList().removeAll(tmpDeleteList); if (MessageDialog.openConfirm(null, Messages.getString("confirmed"), Messages.getString("message.infra.confirm.action", new Object[] { Messages.getString("infra.module"), Messages.getString("delete"), strModuleIds }))) { try { InfraEndpointWrapper wrapper = InfraEndpointWrapper .getWrapper(infraModuleView.getComposite().getManagerName()); wrapper.modifyInfraManagement(info); MessageDialog.openInformation(null, Messages.getString("successful"), Messages.getString("message.infra.action.result", new Object[] { Messages.getString("infra.module"), Messages.getString("delete") + "(" + infraModuleView.getComposite().getManagerName() + ")", Messages.getString("successful"), strModuleIds })); } catch (InvalidRole_Exception e) { // ??? MessageDialog.openError(null, Messages.getString("failed"), Messages.getString("message.accesscontrol.16")); return null; } catch (HinemosUnknown_Exception | InvalidUserPass_Exception | InvalidSetting_Exception | NotifyDuplicate_Exception | NotifyNotFound_Exception | InfraManagementNotFound_Exception | InfraManagementDuplicate_Exception e) { m_log.debug("execute modifyInfraManagement, " + e.getMessage()); MessageDialog.openError(null, Messages.getString("failed"), Messages.getString("message.infra.action.result", new Object[] { Messages.getString("infra.modlue"), Messages.getString("delete"), Messages.getString("failed"), HinemosMessage.replace(e.getMessage()) })); return null; } infraModuleView.update(infraModuleView.getComposite().getManagerName(), info.getManagementId()); } } return null; }