package org.zoneproject.extractor.utils;

/*
 * #%L
 * ZONE-utils
 * %%
 * Copyright (C) 2012 ZONE-project
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringEscapeUtils;

/**
 *
 * @author Desclaux Christophe <christophe@zouig.org>
 */
public class Prop {
    private static final long serialVersionUID = 1L;
    private Property type=null;
    private String value="";
    private boolean isLiteral;
    private boolean isSearchable=false;
    private ArrayList<Prop> children = null;
    private static Pattern unicodeOutliers = Pattern.compile("[^\\x00-\\x7F]", Pattern.UNICODE_CASE | Pattern.CANON_EQ| Pattern.CASE_INSENSITIVE);

    public Prop(Property t, String value){
        this(t,value,true,false);
    }

    public Prop(String type, String value) {
        this(type,value,true);
    }
    public Prop(String type, String value, boolean isLi) {
        this(type,value,isLi,false);
    }
    public Prop(String t, String value, boolean isLi, boolean isSearchable){
        this(ResourceFactory.createProperty(t),value,isLi,isSearchable);
    }
    
    public Prop(Property t, String val, boolean isLi, boolean isS){
        this.type = t;
        byte[] utf8;
        val = StringEscapeUtils.unescapeJava(val);
        utf8 = val.getBytes(Charset.forName("UTF-8"));
        val= new String(utf8, Charset.forName("UTF-8"));
        Matcher unicodeOutlierMatcher = unicodeOutliers.matcher(val);
        val = unicodeOutlierMatcher.replaceAll(" ");
        if(val.equals("")) {
            val = "_";
        }
        this.value = val;
        this.isSearchable = isS;
    }
    
    public Property getType() {
        return type;
    }

    public void setType(Property type) {
        this.type = type;
    }

    public String getValue() {
        return value;
    }
    
    public String getProp(){
        return getType().toString();
    }

    public void setValue(String value) {
        this.value = value;
    }

    public boolean isLiteral() {
        return isLiteral;
    }

    public void setLiteral(boolean isLiteral) {
        this.isLiteral = isLiteral;
    }

    public boolean isIsLiteral() {
        return isLiteral;
    }

    public void setIsLiteral(boolean isLiteral) {
        this.isLiteral = isLiteral;
    }

    public boolean isIsSearchable() {
        return isSearchable;
    }

    public void setIsSearchable(boolean isSearchable) {
        this.isSearchable = isSearchable;
    }

    public ArrayList<Prop> getChildren() {
        return children;
    }

    public void setChildren(ArrayList<Prop> children) {
        this.children = children;
    }

    @Override
    public String toString(){
        return "Prop("+this.getType()+","+this.getValue()+","+this.isLiteral()+","+this.getChildren()+")";
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Prop other = (Prop) obj;
        if ((this.type == null) ? (other.type != null) : !this.type.equals(other.type)) {
            return false;
        }
        if ((this.value == null) ? (other.value != null) : !this.value.equals(other.value)) {
            return false;
        }
        if (this.isLiteral != other.isLiteral) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 31 * hash + (this.type != null ? this.type.hashCode() : 0);
        hash = 31 * hash + (this.value != null ? this.value.hashCode() : 0);
        hash = 31 * hash + (this.isLiteral ? 1 : 0);
        return hash;
    }
}

@@ -889,169 +889,22 @@
 ava.
-nio.charset.Charset;%0Aimport java.util.ArrayList;%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0Aimport org.apache.commons.lang.StringEscapeUtils
+util.ArrayList
 ;%0A%0A/
@@ -1220,155 +1220,8 @@
 ull;
-%0A    private static Pattern unicodeOutliers = Pattern.compile(%22%5B%5E%5C%5Cx00-%5C%5Cx7F%5D%22, Pattern.UNICODE_CASE %7C Pattern.CANON_EQ%7C Pattern.CASE_INSENSITIVE);
 %0A%0A  
@@ -1750,303 +1750,52 @@
     
-byte%5B%5D utf8;%0A        val = StringEscapeUtils.unescapeJava(val);%0A        utf8 = val.getBytes(Charset.forName(%22UTF-8%22));%0A        val= new String(utf8, Charset.forName(%22UTF-8%22));%0A        Matcher unicodeOutlierMatcher = unicodeOutliers.matcher(val);%0A        val = unicodeOutlierMatcher.replaceAll(%22 
+val = val.replaceAll(%22%5B%5E%5C%5Cu0000-%5C%5CuFFFF%5D%22, %22
 %22);%0A

change the way to remove all non utf8 chars