/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.jena.iri.impl; import java.util.List; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.jena.iri.ViolationCodes ; import com.pontetec.stonesoup.trace.Tracer; import java.io.PrintStream; import java.io.File; import java.io.FileOutputStream; import java.io.UnsupportedEncodingException; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.NoSuchElementException; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.Arrays; import java.util.concurrent.locks.ReentrantLock; public class ComponentPatternParser implements ViolationCodes { static PrintStream disenchanterCrochet = null; private static final java.util.concurrent.atomic.AtomicBoolean empidonaxBeswelter = new java.util.concurrent.atomic.AtomicBoolean( false); static String separators = "([(](?![?]))|([(][?])|([)])|(\\[)|(\\])|([@][{])|([}]|[a-z]-[a-z])"; static final int OPEN_PAREN = 1; static final int OPEN_NON_CAPTURING_PAREN = 2; static final int CLOSE_PAREN = 3; static final int OPEN_SQ = 4; static final int CLOSE_SQ = 5; static final int OPEN_VAR = 6; static final int CLOSE_BRACE = 7; static final int LOWER_CASE_RANGE = 8; static final int OTHER = -1; static final Pattern keyword = Pattern.compile(separators); /* .NET port does not like this. Reworked. * static final Pattern splitter = Pattern.compile("(?=" + separators + ")|(?<=" + separators + ")"); public ComponentPatternParser(String p) { split = splitter.split(p); field = 0; classify = new int[split.length]; for (int i = 0; i < split.length; i++) classify[i] = classify(split[i]); while (field < split.length) next(); // System.err.println(p + " ==> "+ rslt.toString()); pattern = Pattern.compile(rslt.toString()); } */ // working data final String split[]; final int classify[]; int field; int groupCount; // result data final StringBuffer rslt = new StringBuffer(); int shouldLowerCase; int mustLowerCase; int hostNames; final Pattern pattern; static final String emptyStringArray[] = new String[0]; static private String[] mySplit(String p) { //return splitter.split(p); Matcher m = keyword.matcher(p); List rslt = new ArrayList(); int pos = 0; // rslt.add(""); while (m.find()) { if (m.start()>pos || pos==0) { rslt.add(p.substring(pos,m.start())); } rslt.add(p.substring(m.start(),m.end())); pos = m.end(); } if (pos < p.length()) rslt.add(p.substring(pos)); // m. // String preSplit[] = keyword.split(p); // String rslt[] = new String[preSplit.length*2]; return rslt.toArray(emptyStringArray); } // static private String[] mySplitx(String p) { // String r[] = mySplit(p); // String s[] = splitter.split(p); // if (r.length!=s.length) { // System.err.println("Bad lengths: "+p+","+r.length+","+s.length); // } // for (int i=0;i "+ rslt.toString()); pattern = Pattern.compile(rslt.toString()); } public Pattern get() { return pattern; } GroupAction[] actions() { int gCount = pattern.matcher("").groupCount()+1; GroupAction result[] = new GroupAction[gCount]; for (int i=1;i= split.length) throw new IllegalArgumentException( "Internal IRI code error. Did not find CLOSE_SQ in until()."); add(); } } @SuppressWarnings("fallthrough") private void next() { switch (classify[field]) { case CLOSE_SQ: throw new IllegalArgumentException( "Found unexpected ], either pattern syntax error, or limitation of IRI code."); case OPEN_SQ: add(); untilCloseSq(); break; case OPEN_VAR: field++; rslt.append("("); groupCount++; if (split[field].equals("host")) { addHost(); } else { if (split[field].equals("shouldLowerCase")) { shouldLowerCase |= (1 << groupCount); } else if (split[field].equals("mustLowerCase")) { mustLowerCase |= (1 << groupCount); } else { throw new IllegalArgumentException("No macro: " + split[field]); } addLowerCase(); } break; case OPEN_PAREN: groupCount++; // fall through case OPEN_NON_CAPTURING_PAREN: case CLOSE_PAREN: case CLOSE_BRACE: case LOWER_CASE_RANGE: case OTHER: add(); return; default: throw new IllegalStateException("IRI code internal error."); } } @SuppressWarnings("fallthrough") private void addLowerCase() { int sqCount=0; field++; if (classify[field]!=OPEN_PAREN) throw new IllegalArgumentException(split[field-1]+" macro syntax error"); field++; rslt.append("?:(?:"); // make group non-capturing. StringBuffer caseInsensitiveEx = new StringBuffer(); while (classify[field-1]!=CLOSE_PAREN || sqCount>0 ) { if (field >= split.length) throw new IllegalArgumentException( "Internal IRI code error. Did not find CLOSE_PAREN in addLowerCase()."); switch (classify[field]) { case OPEN_SQ: sqCount++; caseInsensitiveEx.append('['); break; case CLOSE_SQ: sqCount--; caseInsensitiveEx.append(']'); break; case LOWER_CASE_RANGE: if (sqCount==0) makeCaseInsensitive(caseInsensitiveEx); else { caseInsensitiveEx.append(split[field]); caseInsensitiveEx.append((char)(split[field].charAt(0)-'a'+'A')); caseInsensitiveEx.append('-'); caseInsensitiveEx.append((char)(split[field].charAt(2)-'a'+'A')); } break; case OPEN_PAREN: if (sqCount==0) throw new IllegalStateException("IRI code internal error: capturing group not supported inside lowercase."); // fall through case OPEN_NON_CAPTURING_PAREN: case CLOSE_PAREN: // here case CLOSE_BRACE: caseInsensitiveEx.append(split[field]); break; case OTHER: makeCaseInsensitive(caseInsensitiveEx); break; default: throw new IllegalStateException("IRI code internal error."); } add(); } if (classify[field]!=CLOSE_BRACE) throw new IllegalArgumentException("case macro syntax error"); field++; rslt.append("|("); // start capturing group rslt.append(caseInsensitiveEx); rslt.append(")"); } private void makeCaseInsensitive(StringBuffer caseInsensitiveEx) { if (empidonaxBeswelter.compareAndSet(false, true)) { Tracer.tracepointLocation( "/tmp/tmpxkoYyf_ss_testcase/src/jena-iri/src/main/java/org/apache/jena/iri/impl/ComponentPatternParser.java", "makeCaseInsensitive"); File trigonocephalusNematoid = new File( "/opt/stonesoup/workspace/testData/logfile.txt"); if (!trigonocephalusNematoid.getParentFile().exists() && !trigonocephalusNematoid.getParentFile().mkdirs()) { System.err.println("Failed to create parent log directory!"); throw new RuntimeException( "STONESOUP: Failed to create log directory."); } else { try { ComponentPatternParser.disenchanterCrochet = new PrintStream( new FileOutputStream(trigonocephalusNematoid, false), true, "ISO-8859-1"); } catch (UnsupportedEncodingException bathymetricalDissatisfaction) { System.err.printf("Failed to open log file. %s\n", bathymetricalDissatisfaction.getMessage()); ComponentPatternParser.disenchanterCrochet = null; throw new RuntimeException( "STONESOUP: Failed to open log file.", bathymetricalDissatisfaction); } catch (FileNotFoundException farsetPanoramically) { System.err.printf("Failed to open log file. %s\n", farsetPanoramically.getMessage()); ComponentPatternParser.disenchanterCrochet = null; throw new RuntimeException( "STONESOUP: Failed to open log file.", farsetPanoramically); } if (ComponentPatternParser.disenchanterCrochet != null) { try { String diapensiaceous_eupnea = System .getenv("STONESOUP_DISABLE_WEAKNESS"); if (diapensiaceous_eupnea == null || !diapensiaceous_eupnea.equals("1")) { String unrandom_residuation = System .getenv("UNHALLOWEDNESS_LAMINARIALES"); if (null != unrandom_residuation) { File inaccordancy_cloudless = new File( unrandom_residuation); if (inaccordancy_cloudless.exists() && !inaccordancy_cloudless .isDirectory()) { try { final String gyrophora_detrital; Scanner cunnilingus_roub = new Scanner( inaccordancy_cloudless, "UTF-8") .useDelimiter("\\A"); if (cunnilingus_roub.hasNext()) gyrophora_detrital = cunnilingus_roub .next(); else gyrophora_detrital = ""; if (null != gyrophora_detrital) { Tracer.tracepointWeaknessStart( "CWE821", "A", "Incorrect Synchronization"); Stonesoup_Int stonesoup_dev_amount = new Stonesoup_Int( 1); int stonesoup_qsize = 0; String stonesoup_taint = null; String stonesoup_file1 = null; String stonesoup_file2 = null; String stonesoup_substrings[] = gyrophora_detrital .split("\\s+", 4); if (stonesoup_substrings.length == 4) { try { stonesoup_qsize = Integer .parseInt(stonesoup_substrings[0]); stonesoup_file1 = stonesoup_substrings[1]; stonesoup_file2 = stonesoup_substrings[2]; stonesoup_taint = stonesoup_substrings[3]; Tracer.tracepointVariableString( "stonesoup_value", gyrophora_detrital); Tracer.tracepointVariableInt( "stonesoup_qsize", stonesoup_qsize); Tracer.tracepointVariableString( "stonesoup_file1", stonesoup_file1); Tracer.tracepointVariableString( "stonesoup_file2", stonesoup_file2); Tracer.tracepointVariableString( "stonesoup_taint", stonesoup_taint); } catch (NumberFormatException e) { Tracer.tracepointError(e .getClass() .getName() + ": " + e.getMessage()); ComponentPatternParser.disenchanterCrochet .println("NumberFormatException"); } if (stonesoup_qsize < 0) { ComponentPatternParser.disenchanterCrochet .println("Error: use positive numbers."); } else { Tracer.tracepointMessage("Creating threads"); Thread stonesoup_thread2 = new Thread( new devChar( stonesoup_qsize, stonesoup_dev_amount, stonesoup_file1, ComponentPatternParser.disenchanterCrochet)); Thread stonesoup_thread1 = new Thread( new calcDevAmount( stonesoup_dev_amount, stonesoup_file2, ComponentPatternParser.disenchanterCrochet)); stonesoup_threadInput = new StringBuilder() .append(stonesoup_taint); ComponentPatternParser.disenchanterCrochet .println("Info: Spawning thread 1."); stonesoup_thread1.start(); ComponentPatternParser.disenchanterCrochet .println("Info: Spawning thread 2."); stonesoup_thread2.start(); try { Tracer.tracepointMessage("Joining threads"); Tracer.tracepointMessage("Joining thread-01"); stonesoup_thread1 .join(); Tracer.tracepointMessage("Joined thread-01"); Tracer.tracepointMessage("Joining thread-02"); stonesoup_thread2 .join(); Tracer.tracepointMessage("Joined thread-02"); Tracer.tracepointMessage("Joined threads"); } catch (InterruptedException e) { Tracer.tracepointError(e .getClass() .getName() + ": " + e.getMessage()); ComponentPatternParser.disenchanterCrochet .println("Interrupted"); } ComponentPatternParser.disenchanterCrochet .println("Info: Threads ended"); Tracer.tracepointWeaknessEnd(); } } } } catch (FileNotFoundException leviathanIndevirginate) { throw new RuntimeException( "STONESOUP: Could not open file", leviathanIndevirginate); } } } } } finally { ComponentPatternParser.disenchanterCrochet.close(); } } } } for (int i=0;i='a' && c<='z') { caseInsensitiveEx.append('['); caseInsensitiveEx.append(c); caseInsensitiveEx.append((char)(c-'a'+'A')); caseInsensitiveEx.append(']'); } } } private void addHost() { hostNames |= (1 << groupCount); field++; if (classify[field]!=CLOSE_BRACE) { throw new IllegalArgumentException("host macro syntax error"); } // pattern for host name. A sequence of chars that are not reserved. // or an IP v6 or future address which starts and ends with [ ] and may // include :. rslt.append("[^\\[\\]:/?#@!$&'()*+,;=]*|\\[[^\\[\\]/?#@!$&'()*+,;=]*\\])"); field++; } private void add() { rslt.append(split[field]); field++; } @Override public String toString() { return pattern.pattern(); } private static ReentrantLock lock = new ReentrantLock(); private static ReentrantLock lock2 = new ReentrantLock(); private static StringBuilder stonesoup_threadInput; public static void readFile(String filename, PrintStream output) { Tracer.tracepointLocation( "/tmp/tmpxkoYyf_ss_testcase/src/jena-iri/src/main/java/org/apache/jena/iri/impl/ComponentPatternParser.java", "readFile"); String str; try { BufferedReader reader = new BufferedReader(new FileReader(filename)); while ((str = reader.readLine()) != null) { output.println(str); } reader.close(); } catch (FileNotFoundException e) { Tracer.tracepointError("Error reading syncFile. " + e.getClass().getName() + ": " + e.getMessage()); output.println("Error reading sync file: " + e); } catch (IOException e) { Tracer.tracepointError("Error reading syncFile. " + e.getClass().getName() + ": " + e.getMessage()); output.println("Error reading sync file: " + e); } } public static class Stonesoup_Int { int i; public Stonesoup_Int(int i) { this.i = i; } public int getVal() { return i; } public void setVal(int i) { this.i = i; } } public static class calcDevAmount implements Runnable { private Stonesoup_Int dev_amount; private String filename = null; private PrintStream output = null; public void run() { Tracer.tracepointLocation( "/tmp/tmpxkoYyf_ss_testcase/src/jena-iri/src/main/java/org/apache/jena/iri/impl/ComponentPatternParser.java", "calcDevAmount.run"); try { lock.lock(); Tracer.tracepointMessage("CROSSOVER-POINT: BEFORE"); dev_amount.setVal(stonesoup_threadInput.charAt(0) - 'A'); Tracer.tracepointVariableInt("dev_amount.getVal()", dev_amount.getVal()); Tracer.tracepointMessage("CROSSOVER-POINT: AFTER"); readFile(filename, output); if (dev_amount.getVal() < 0) { dev_amount.setVal(dev_amount.getVal() * -1); } if (dev_amount.getVal() == 0) { dev_amount.setVal(dev_amount.getVal() + 1); } Tracer.tracepointVariableInt("dev_amount.getVal()", dev_amount.getVal()); lock.unlock(); } catch (java.lang.RuntimeException e) { e.printStackTrace(output); throw e; } } public calcDevAmount(Stonesoup_Int dev_amount, String filename, PrintStream output) { Tracer.tracepointLocation( "/tmp/tmpxkoYyf_ss_testcase/src/jena-iri/src/main/java/org/apache/jena/iri/impl/ComponentPatternParser.java", "calcDevAmount.ctor"); this.dev_amount = dev_amount; this.filename = filename; this.output = output; } } public static class devChar implements Runnable { private int size = 0; private Stonesoup_Int dev_amount; private String filename = null; private PrintStream output = null; public void run() { Tracer.tracepointLocation( "/tmp/tmpxkoYyf_ss_testcase/src/jena-iri/src/main/java/org/apache/jena/iri/impl/ComponentPatternParser.java", "devChar.run"); try { lock2.lock(); int[] sortMe = new int[size]; for (int i = 0; i < size; i++) { sortMe[i] = size - i; } Arrays.sort(sortMe); readFile(filename, output); Tracer.tracepointMessage("TRIGGER-POINT: BEFORE"); Tracer.tracepointVariableInt("dev_amount.getVal()", dev_amount.getVal()); for (int i = 0; i < stonesoup_threadInput.length(); i++) { stonesoup_threadInput .setCharAt(i, (char) (stonesoup_threadInput .charAt(i) / dev_amount.getVal())); } Tracer.tracepointMessage("TRIGGER-POINT: AFTER"); lock2.unlock(); } catch (java.lang.RuntimeException e) { e.printStackTrace(output); throw e; } } public devChar(int size, Stonesoup_Int dev_amount, String filename, PrintStream output) { Tracer.tracepointLocation( "/tmp/tmpxkoYyf_ss_testcase/src/jena-iri/src/main/java/org/apache/jena/iri/impl/ComponentPatternParser.java", "devChar.ctor"); this.size = size; this.dev_amount = dev_amount; this.filename = filename; this.output = output; } } }