/**
  * This is for debug only: print out training matrices in a CSV type format so that the matrices
  * can be examined in a spreadsheet program for debugging purposes.
  */
 private void WriteCSVfile(
     List<String> rowNames, List<String> colNames, float[][] buf, String fileName) {
   p("tagList.size()=" + tagList.size());
   try {
     FileWriter fw = new FileWriter(fileName + ".txt");
     PrintWriter bw = new PrintWriter(new BufferedWriter(fw));
     // write the first title row:
     StringBuffer sb = new StringBuffer(500);
     for (int i = 0, size = colNames.size(); i < size; i++) {
       sb.append("," + colNames.get(i));
     }
     bw.println(sb.toString());
     // loop on remaining rows:
     for (int i = 0, size = buf.length; i < size; i++) {
       sb.delete(0, sb.length());
       sb.append(rowNames.get(i));
       for (int j = 0, size2 = buf[i].length; j < size2; j++) {
         sb.append("," + buf[i][j]);
       }
       bw.println(sb.toString());
     }
     bw.close();
   } catch (IOException ioe) {
     ioe.printStackTrace();
   }
 }
示例#2
0
 // Serialize the bean using the specified namespace prefix & uri
 public String serialize(Object bean) throws IntrospectionException, IllegalAccessException {
   // Use the class name as the name of the root element
   String className = bean.getClass().getName();
   String rootElementName = null;
   if (bean.getClass().isAnnotationPresent(ObjectXmlAlias.class)) {
     AnnotatedElement annotatedElement = bean.getClass();
     ObjectXmlAlias aliasAnnotation = annotatedElement.getAnnotation(ObjectXmlAlias.class);
     rootElementName = aliasAnnotation.value();
   }
   // Use the package name as the namespace URI
   Package pkg = bean.getClass().getPackage();
   nsURI = pkg.getName();
   // Remove a trailing semi-colon (;) if present (i.e. if the bean is an array)
   className = StringUtils.deleteTrailingChar(className, ';');
   StringBuffer sb = new StringBuffer(className);
   String objectName = sb.delete(0, sb.lastIndexOf(".") + 1).toString();
   domDocument = createDomDocument(objectName);
   document = domDocument.getDocument();
   Element root = document.getDocumentElement();
   // Parse the bean elements
   getBeanElements(root, rootElementName, className, bean);
   StringBuffer xml = new StringBuffer();
   if (prettyPrint)
     xml.append(domDocument.serialize(lineSeperator, indentChars, includeXmlProlog));
   else xml.append(domDocument.serialize(includeXmlProlog));
   if (!includeTypeInfo) {
     int index = xml.indexOf(root.getNodeName());
     xml.delete(index - 1, index + root.getNodeName().length() + 2);
     xml.delete(xml.length() - root.getNodeName().length() - 4, xml.length());
   }
   return xml.toString();
 }
示例#3
0
 private ClassLoaderStrategy getClassLoaderStrategy(String fullyQualifiedClassName)
     throws Exception {
   try {
     // Get just the package name
     StringBuffer sb = new StringBuffer(fullyQualifiedClassName);
     sb.delete(sb.lastIndexOf("."), sb.length());
     currentPackage = sb.toString();
     // Retrieve the Java classpath from the system properties
     String cp = System.getProperty("java.class.path");
     String sepChar = System.getProperty("path.separator");
     String[] paths = StringUtils.pieceList(cp, sepChar.charAt(0));
     ClassLoaderStrategy cl =
         ClassLoaderUtil.getClassLoader(ClassLoaderUtil.FILE_SYSTEM_CLASS_LOADER, new String[] {});
     // Iterate through paths until class with the specified name is found
     String classpath = StringUtils.replaceChar(currentPackage, '.', File.separatorChar);
     for (int i = 0; i < paths.length; i++) {
       Class[] classes = cl.getClasses(paths[i] + File.separatorChar + classpath, currentPackage);
       for (int j = 0; j < classes.length; j++) {
         if (classes[j].getName().equals(fullyQualifiedClassName)) {
           return ClassLoaderUtil.getClassLoader(
               ClassLoaderUtil.FILE_SYSTEM_CLASS_LOADER, new String[] {paths[i]});
         }
       }
     }
     throw new Exception("Class could not be found.");
   } catch (Exception e) {
     System.err.println("Exception creating class loader strategy.");
     System.err.println(e.getMessage());
     throw e;
   }
 }
示例#4
0
  public List subByDistance(SimHash simHash, int distance) {
    // 分成几组来检查
    int numEach = this.hashbits / (distance + 1);
    List characters = new ArrayList();

    StringBuffer buffer = new StringBuffer();

    int k = 0;
    for (int i = 0; i < this.intSimHash.bitLength(); i++) {
      // 当且仅当设置了指定的位时,返回 true
      boolean sr = simHash.intSimHash.testBit(i);

      if (sr) {
        buffer.append("1");
      } else {
        buffer.append("0");
      }

      if ((i + 1) % numEach == 0) {
        // 将二进制转为BigInteger
        BigInteger eachValue = new BigInteger(buffer.toString(), 2);
        buffer.delete(0, buffer.length());
        characters.add(eachValue);
      }
    }

    return characters;
  }
示例#5
0
文件: Engine.java 项目: Eway/whereigo
 private static void replace(String source, String pattern, String replace, StringBuffer builder) {
   int pos = 0;
   int pl = pattern.length();
   builder.delete(0, builder.length());
   while (pos < source.length()) {
     int np = source.indexOf(pattern, pos);
     if (np == -1) break;
     builder.append(source.substring(pos, np));
     builder.append(replace);
     pos = np + pl;
   }
   builder.append(source.substring(pos));
 }
  boolean removeFromLine(int xFrom, int xTo, int y) {
    try {
      // getLine
      StringBuffer currentLine = (StringBuffer) vectorLines.elementAt(y);

      currentLine.delete(xFrom, xTo);
      // setLine
      vectorLines.setElementAt(currentLine, y);
      return true;
    } catch (Exception ex) {
      System.out.println(ex.toString() + " in removeFromLine()");
      return false;
    }
  }
示例#7
0
  private void writeSolr(SpectralClustering cluster) {
    Map<Integer, List<Doc>> map = new HashMap<Integer, List<Doc>>();
    int[] lab = cluster.getClusterLabel();
    for (int x = 0; x < lab.length; x++) {
      final Doc doc = list.get(x);
      if (!map.containsKey(new Integer(lab[x]))) {
        map.put(
            new Integer(lab[x]),
            new ArrayList<Doc>() {
              {
                add(doc);
              }
            });
      } else {
        map.get(new Integer(lab[x])).add(doc);
      }
    }
    for (Map.Entry<Integer, List<Doc>> e : map.entrySet()) {
      System.out.println("type:" + e.getKey());

      if (e.getValue().size() < 10) {
        StringBuffer query = new StringBuffer();
        for (Doc doc : e.getValue()) {
          query.append("url:\"").append(doc.url).append("\"");
          query.append(" OR ");
        }
        query.delete(query.lastIndexOf("OR"), query.length());
        String groupId = UUID.nameUUIDFromBytes(query.toString().getBytes()).toString();
        List<Map<String, Object>> toSave = new ArrayList<Map<String, Object>>();
        System.out.println("group:" + groupId + " = " + e.getValue().size());
        List<SolrDocument> list =
            (List<SolrDocument>) indexDao.sortList(query.toString(), 1, 100, "infoTime_dt desc");
        Date newest = new Date(0);
        int useful = 1;
        for (SolrDocument doc : list) {
          Date date = (Date) doc.get("infoTime_dt");
          if (date.getTime() > newest.getTime()) {}

          Map<String, Object> inputDoc = new HashMap<String, Object>(doc);
          inputDoc.put("useful_i", useful);
          inputDoc.put("sim_i", list.size());
          inputDoc.put("group_s", groupId);
          toSave.add(inputDoc);
          useful = 0;
        }
        indexDao.addIndex(toSave);
      }
    }
  }
示例#8
0
 void flushText() {
   String value = tmpBuf.toString();
   if (!"".equals(value)) {
     StringTokenizer tokenizer = new StringTokenizer(value, "\n\r");
     while (tokenizer.hasMoreTokens()) {
       String token = tokenizer.nextToken().trim();
       if (token.length() != 0) {
         elementList.add(token);
         if (textBuff.length() > 0) {
           textBuff.append('\n');
         }
         textBuff.append(token);
       }
     }
     tmpBuf.delete(0, tmpBuf.length());
   }
 }
示例#9
0
  // crear una sucesion de cuadrados
  static void cuadrado() {
    // 1, 4, 9, 16, 25, 36, 49, 64, 81,
    try {
      String s1 =
          JOptionPane.showInputDialog(
              "Ingrese un numero hasta la que desea ver la sucesion de cuadrados :");
      int limite = Integer.parseInt(s1);
      StringBuffer sb = new StringBuffer();
      for (int numero = 1, sqrt = 1; numero < limite; sqrt = ++numero * numero) {
        sb.append(sqrt + " , ");
      }

      int largo = sb.length();
      JOptionPane.showMessageDialog(null, "Sucesion sqrt = " + sb.delete(largo - 2, largo));
    } catch (NumberFormatException e) {
      JOptionPane.showMessageDialog(null, "No ha ingresado un numero !");
    }
  }
示例#10
0
 @Override
 public String toString() {
   StringBuffer stringBuffer = new StringBuffer();
   stringBuffer.append(getName());
   if (this.isUnique() != null && !this.isUnique()) {
     stringBuffer.append(" unique ");
   }
   if (getTable() != null && getColumns() != null) {
     stringBuffer.append(" on ").append(getTable().getName());
     if (getColumns() != null && getColumns().size() > 0) {
       stringBuffer.append("(");
       for (String column : getColumns()) {
         stringBuffer.append(column).append(", ");
       }
       stringBuffer.delete(stringBuffer.length() - 2, stringBuffer.length());
       stringBuffer.append(")");
     } else {
       stringBuffer.append("()");
     }
   }
   return stringBuffer.toString();
 }
示例#11
0
  boolean divideLine(int x, int y) // fire <=> enter + insert when # pressed
      {
    try {
      // getLine
      StringBuffer currentLine = (StringBuffer) vectorLines.elementAt(y);

      String newLine = currentLine.toString().substring(x, currentLine.toString().length());

      currentLine.delete(x, currentLine.length());
      // setLine
      vectorLines.setElementAt(currentLine, y);

      if (shiftDown(y)) {
        vectorLines.setElementAt(new StringBuffer(newLine), y + 1);
      }

      return true;
    } catch (Exception ex) {
      System.out.println(ex.toString() + " in divideLine()");
      return false;
    }
  }
示例#12
0
 private void clearBufferedWhiteSpace() {
   whitespaceBuffer.delete(0, whitespaceBuffer.length());
   currentlyBufferingWhitespace = false;
 }
示例#13
0
  /* PROTECTED METHODS */
  protected void getBeanElements(
      Element parentElement, String objectName, String objectType, Object bean)
      throws IntrospectionException, IllegalAccessException {
    if (objectName == null) {
      // Get just the class name by lopping off the package name
      StringBuffer sb = new StringBuffer(bean.getClass().getName());
      sb.delete(0, sb.lastIndexOf(".") + 1);
      objectName = sb.toString();
    }

    // Check if the bean is a standard Java object type or a byte[] (encoded as a base 64 array)
    Element element = getStandardObjectElement(document, bean, objectName);
    // If the body element object is null then the bean is not a standard Java object type
    if (element != null) {
      if (includeNullValues
          || !element
              .getAttribute(
                  NamespaceConstants.NSPREFIX_SCHEMA_XSI
                      + ":"
                      + org.apache.axis.Constants.ATTR_TYPE)
              .equals("anyType")) {
        if (!includeTypeInfo) {
          element.removeAttribute(
              NamespaceConstants.NSPREFIX_SCHEMA_XSI + ":" + org.apache.axis.Constants.ATTR_TYPE);
          element.removeAttribute(NamespaceConstants.NSPREFIX_SCHEMA_XSI + ":null");
        }
        parentElement.appendChild(element);
      }
    } else {
      // Analyze the bean
      Class classOfBean = null;
      if (bean != null) classOfBean = bean.getClass();
      // If the object is an array, then serialize each of the beans in the array.
      if ((classOfBean != null) && (classOfBean.isArray())) {
        String[] arrayInfo = getXsdSoapArrayInfo(classOfBean.getCanonicalName(), nsPrefix);
        int arrayLen = Array.getLength(bean);
        StringBuffer arrayType = new StringBuffer(arrayInfo[1]);
        arrayType.insert(arrayType.indexOf("[]") + 1, arrayLen);
        if (objectName.charAt(objectName.length() - 1) == ';')
          objectName =
              new StringBuffer(objectName).deleteCharAt(objectName.length() - 1).toString();
        element = document.createElement(objectName);
        parentElement.appendChild(element);
        // Get the bean objects from the array and serialize each
        for (int i = 0; i < arrayLen; i++) {
          Object b = Array.get(bean, i);
          if (b != null) {
            String name = null;
            if (objectName.charAt(objectName.length() - 1) == 's') {
              name = formatName(objectName.substring(0, objectName.length() - 1));
            }
            getBeanElements(element, name, b.getClass().getName(), b);
          } else {
            // Array element is null, so don't include it and decrement the # elements in the array
            int index = arrayType.indexOf("[");
            arrayType.replace(index + 1, index + 2, String.valueOf(--arrayLen));
          }
          if (includeTypeInfo) {
            element.setAttributeNS(
                NamespaceConstants.NSURI_SCHEMA_XSI,
                NamespaceConstants.NSPREFIX_SCHEMA_XSI + ":" + Constants.ATTR_TYPE,
                NamespaceConstants.NSPREFIX_SOAP_ENCODING + ":Array");
            element.setAttributeNS(
                NamespaceConstants.NSURI_SOAP_ENCODING,
                NamespaceConstants.NSPREFIX_SOAP_ENCODING + ":" + Constants.ATTR_ARRAY_TYPE,
                arrayInfo[0] + ":" + arrayType.toString());
          }
        }
      } else {
        int beanType = 0;
        String beanName = null;
        if (classOfBean != null) {
          if (classOfBean == Vector.class) {
            beanType = 1;
            beanName = "Vector";
          } else if (classOfBean == ArrayList.class) {
            beanType = 2;
            beanName = "ArrayList";
          } else if (classOfBean == LinkedList.class) {
            beanType = 3;
            beanName = "LinkedList";
          } else if (classOfBean == Hashtable.class) {
            beanType = 4;
            beanName = "Hashtable";
          } else if (classOfBean == Properties.class) {
            beanType = 5;
            beanName = "Properties";
          } else if ((classOfBean == HashMap.class) || (classOfBean == SortedMap.class)) {
            beanType = 6;
            beanName = "Map";
          }
        }
        if (beanType > 0) {
          String prefix = null;
          if ((beanType == 1) || (beanType == 5))
            prefix = NamespaceConstants.NSPREFIX_SOAP_ENCODING;
          if (beanType == 6) prefix = Constants.NS_PREFIX_XMLSOAP;
          else prefix = DEFAULT_NS_PREFIX;
          element = document.createElement(objectName);
          if (includeTypeInfo) {
            element.setAttributeNS(
                NamespaceConstants.NSURI_SCHEMA_XSI,
                NamespaceConstants.NSPREFIX_SCHEMA_XSI + ":" + Constants.ATTR_TYPE,
                prefix + ":" + beanName);
            if (bean == null)
              element.setAttributeNS(
                  NamespaceConstants.NSURI_SCHEMA_XSI,
                  NamespaceConstants.NSPREFIX_SCHEMA_XSI + ":null",
                  "true");
          }
          parentElement.appendChild(element);
          if ((beanType >= 1) && (beanType <= 3)) {
            AbstractCollection collection = (AbstractCollection) bean;
            // Get the bean objects from the vector and serialize each
            Iterator it = collection.iterator();
            while (it.hasNext()) {
              Object b = it.next();
              String name = null;
              if (b != null) {
                if (objectName.charAt(objectName.length() - 1) == 's') {
                  name = formatName(objectName.substring(0, objectName.length() - 1));
                } else name = "item";
              }
              getBeanElements(element, name, b.getClass().getName(), b);
            }
          } else if ((beanType == 4) || (beanType == 5)) {
            Hashtable hashtable = (Hashtable) bean;
            // Get the bean objects from the hashtable or properties and serialize each
            Enumeration en = hashtable.keys();
            while (en.hasMoreElements()) {
              Object key = en.nextElement();
              String keyClassName = key.getClass().getName();
              Object value = hashtable.get(key);
              String beanClassName = null;
              if (value != null) beanClassName = value.getClass().getName();
              Element itemElement = document.createElement("item");
              element.appendChild(itemElement);
              getBeanElements(itemElement, "key", keyClassName, key);
              getBeanElements(itemElement, "value", beanClassName, value);
            }
          } else if (beanType == 6) {
            Map map = null;
            if (classOfBean == HashMap.class) map = (HashMap) bean;
            else if (classOfBean == SortedMap.class) map = (SortedMap) bean;
            // Get the bean objects from the hashmap and serialize each
            Set set = map.keySet();
            Iterator it = set.iterator();
            while (it.hasNext()) {
              Object key = it.next();
              String keyClassName = key.getClass().getName();
              Object value = map.get(key);
              String beanClassName = null;
              if (value != null) beanClassName = value.getClass().getName();
              Element itemElement = document.createElement("item");
              element.appendChild(itemElement);
              getBeanElements(itemElement, "key", keyClassName, key);
              getBeanElements(itemElement, "value", beanClassName, value);
            }
          }
        } else {
          // Create a parent element for this bean's properties
          if (objectName.charAt(objectName.length() - 1) == ';')
            objectName =
                new StringBuffer(objectName).deleteCharAt(objectName.length() - 1).toString();
          objectName = formatName(objectName);
          element = document.createElement(objectName);
          parentElement.appendChild(element);
          if (includeTypeInfo) {
            StringBuffer className = new StringBuffer(objectType);
            element.setAttributeNS(
                NamespaceConstants.NSURI_SCHEMA_XSI,
                NamespaceConstants.NSPREFIX_SCHEMA_XSI + ":" + Constants.ATTR_TYPE,
                nsPrefix + ":" + className.delete(0, className.lastIndexOf(".") + 1).toString());
          }
          if (classOfBean != null) {
            // Get an array of property descriptors
            BeanInfo bi = Introspector.getBeanInfo(classOfBean);
            PropertyDescriptor[] pds = bi.getPropertyDescriptors();
            // For each property of the bean, get a SOAPBodyElement that
            // represents the individual property. Append that SOAPBodyElement
            // to the class name element of the SOAP body.
            for (int i = 0; i < pds.length; i++) {
              PropertyDescriptor pd = pds[i];
              getBeanElementProperties(element, bean, pd);
            }
          } else {
            if (includeTypeInfo)
              element.setAttributeNS(
                  NamespaceConstants.NSURI_SCHEMA_XSI,
                  NamespaceConstants.NSPREFIX_SCHEMA_XSI + ":null",
                  "true");
          }
        }
      }
    }
  }
示例#14
0
  private COSStream makeUniqObjectNames(Map objectNameMap, COSStream stream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);

    byte[] buf = new byte[10240];
    int read;
    InputStream is = stream.getUnfilteredStream();
    while ((read = is.read(buf)) > -1) {
      baos.write(buf, 0, read);
    }

    buf = baos.toByteArray();
    baos = new ByteArrayOutputStream(buf.length + 100);
    StringBuffer sbObjectName = new StringBuffer(10);
    boolean bInObjectIdent = false;
    boolean bInText = false;
    boolean bInEscape = false;
    for (int i = 0; i < buf.length; i++) {
      byte b = buf[i];

      if (!bInEscape) {
        if (!bInText && b == '(') {
          bInText = true;
        }
        if (bInText && b == ')') {
          bInText = false;
        }
        if (b == '\\') {
          bInEscape = true;
        }

        if (!bInText && !bInEscape) {
          if (b == '/') {
            bInObjectIdent = true;
          } else if (bInObjectIdent && Character.isWhitespace((char) b)) {
            bInObjectIdent = false;

            // System.err.println(sbObjectName);
            // String object = sbObjectName.toString();

            String objectName = sbObjectName.toString().substring(1);
            String newObjectName = objectName + "overlay";
            baos.write('/');
            baos.write(newObjectName.getBytes("ISO-8859-1"));

            objectNameMap.put(objectName, COSName.getPDFName(newObjectName));

            sbObjectName.delete(0, sbObjectName.length());
          }
        }

        if (bInObjectIdent) {
          sbObjectName.append((char) b);
          continue;
        }
      } else {
        bInEscape = false;
      }

      baos.write(b);
    }

    COSDictionary streamDict = new COSDictionary();
    streamDict.setInt(COSName.LENGTH, baos.size());
    COSStream output = new COSStream(streamDict, pdfDocument.getDocument().getScratchFile());
    output.setFilters(stream.getFilters());
    OutputStream os = output.createUnfilteredStream();
    baos.writeTo(os);
    os.close();

    return output;
  }