Beispiel #1
0
  private AlbumBean addOrUpdateAlbum(
      Map<Integer, AlbumBean> map, SongBean song, boolean albumOnly) {
    // Add an album bean
    if (song != null && song.getAlbum() != null) {
      int hashCode = (song.getAlbum() + (albumOnly ? "" : '\uFFFF' + song.getArtist())).hashCode();

      // Check if the album beam already exists
      AlbumBean album = map.get(hashCode);
      if (album == null) {
        album = new AlbumBean();
        album.setAlbum(song.getAlbum());
        map.put(hashCode, album);
      }

      // Update the album properties
      try {
        // Add the track id to the album bean
        album.addSong(song);
        album.incTracks();
        album.setArtist(song.getArtist());
        album.setGenre(song.getGenre());
        album.setDisc_Count(song.getDisc_Count());
      } catch (LibraryException le) {
        // There was an error adding the song to this album, remove it
        map.remove(hashCode);

        // Add to warning message
        warningList.add(song.getName() + " " + le);
      }

      return album;
    }

    return null;
  }
Beispiel #2
0
 /**
  * Associated one DocumentBuilder per thread. This is so we avoid synchronizing (parse() for
  * example may take a lot of time on a DocumentBuilder) or creating DocumentBuilder instances all
  * the time. Since typically in an app server we work with a thread pool, not too many instances
  * of DocumentBuilder should be created.
  */
 private static DocumentBuilder getThreadDocumentBuilder() {
   Thread thread = Thread.currentThread();
   DocumentBuilder documentBuilder =
       (documentBuilders == null) ? null : documentBuilders.get(thread);
   // Try a first test outside the synchronized block
   if (documentBuilder == null) {
     synchronized (documentBuilderFactory) {
       // Redo the test within the synchronized block
       documentBuilder = (documentBuilders == null) ? null : documentBuilders.get(thread);
       if (documentBuilder == null) {
         if (documentBuilders == null) documentBuilders = new HashMap<Thread, DocumentBuilder>();
         documentBuilder = newDocumentBuilder();
         documentBuilders.put(thread, documentBuilder);
       }
     }
   }
   return documentBuilder;
 }
Beispiel #3
0
 private void addDefaultValues(Map attributes, Map mappings) {
   if (mappings == null) return;
   Iterator i = mappings.entrySet().iterator();
   while (i.hasNext()) {
     Map.Entry e = (Map.Entry) i.next();
     TagMap.AttributeMapping m = (TagMap.AttributeMapping) e.getValue();
     if (null != m && null != m.getDefaultValue() && null == attributes.get(m.getPropertyName()))
       attributes.put(m.getPropertyName(), m.getDefaultValue());
   }
 }
Beispiel #4
0
  /**
   * Get a SAXParserFactory to build combinations of validating and XInclude-aware SAXParser.
   *
   * @param parserConfiguration parser configuration
   * @return the SAXParserFactory
   */
  public static synchronized SAXParserFactory getSAXParserFactory(
      XMLUtils.ParserConfiguration parserConfiguration) {

    final String key = parserConfiguration.getKey();

    final SAXParserFactory existingFactory = parserFactories.get(key);
    if (existingFactory != null) return existingFactory;

    final SAXParserFactory newFactory = createSAXParserFactory(parserConfiguration);
    parserFactories.put(key, newFactory);
    return newFactory;
  }
Beispiel #5
0
  // returns list of statements
  protected static void replaceMultSPO(
      Statement st, NodeFactory f, Map o2n, Collection result, RDFNode toReplace, int position)
      throws ModelException {

    Collection replacements;

    if (toReplace instanceof Statement) {

      List l = new ArrayList();
      replaceMult((Statement) toReplace, f, o2n, l);

      if (l.size() == 1 && toReplace == l.get(0)) {
        result.add(st);
        return; // keep the same
      } else replacements = l;

    } else {

      Object ro = o2n.get(toReplace);

      if (ro instanceof Collection) replacements = (Collection) ro;
      else if (ro != null) {

        replacements = new ArrayList();
        replacements.add(ro);

      } else { // no replacement needed

        result.add(st); // keep the same statement
        return;
      }
    }

    for (Iterator it = replacements.iterator(); it.hasNext(); ) {

      Statement rs = null;
      Object rr = it.next();

      switch (position) {
        case 0:
          rs = f.createStatement((Resource) rr, st.predicate(), st.object());
          break;
        case 1:
          rs = f.createStatement(st.subject(), (Resource) rr, st.object());
          break;
        case 2:
          rs = f.createStatement(st.subject(), st.predicate(), (RDFNode) rr);
          break;
      }
      result.add(rs);
    }
  }
Beispiel #6
0
  public static Statement replaceResources(Statement st, NodeFactory f, Map o2n)
      throws ModelException {

    boolean replaced = false;
    Resource subj = st.subject();
    Resource pred = st.predicate();
    RDFNode obj = st.object();

    Object n = null;

    if (obj instanceof Statement) {

      n = obj;
      obj = replaceResources((Statement) obj, f, o2n);
      replaced = n != obj;

    } else if ((n = o2n.get(obj)) != null) {
      replaced = true;
      obj = (RDFNode) n;
    }

    if (subj instanceof Statement) {

      n = subj;
      subj = replaceResources((Statement) subj, f, o2n);
      replaced = n != subj;
    }
    if ((n = o2n.get(subj)) != null) {
      replaced = true;
      subj = (Resource) n;
    }

    if ((n = o2n.get(pred)) != null) {
      replaced = true;
      pred = (Resource) n;
    }
    return replaced ? f.createStatement(subj, pred, obj) : st;
  }
Beispiel #7
0
 /**
  * Returns the the JDOM Attribute type value from the SAX 2.0 attribute type string provided by
  * the parser.
  *
  * @param typeName <code>String</code> the SAX 2.0 attribute type string.
  * @return <code>int</code> the JDOM attribute type.
  * @see Attribute#setAttributeType
  * @see Attributes#getType
  */
 private static int getAttributeType(String typeName) {
   Integer type = (Integer) (attrNameToTypeMap.get(typeName));
   if (type == null) {
     if (typeName != null && typeName.length() > 0 && typeName.charAt(0) == '(') {
       // Xerces 1.4.X reports attributes of enumerated type with
       // a type string equals to the enumeration definition, i.e.
       // starting with a parenthesis.
       return Attribute.ENUMERATED_TYPE;
     } else {
       return Attribute.UNDECLARED_TYPE;
     }
   } else {
     return type.intValue();
   }
 }
Beispiel #8
0
  public void startEntity(String name) throws SAXException {
    entityDepth++;

    if (expand || entityDepth > 1) {
      // Short cut out if we're expanding or if we're nested
      return;
    }

    // A "[dtd]" entity indicates the beginning of the external subset
    if (name.equals("[dtd]")) {
      inInternalSubset = false;
      return;
    }

    // Ignore DTD references, and translate the standard 5
    if ((!inDTD)
        && (!name.equals("amp"))
        && (!name.equals("lt"))
        && (!name.equals("gt"))
        && (!name.equals("apos"))
        && (!name.equals("quot"))) {

      if (!expand) {
        String pub = null;
        String sys = null;
        String[] ids = (String[]) externalEntities.get(name);
        if (ids != null) {
          pub = ids[0]; // may be null, that's OK
          sys = ids[1]; // may be null, that's OK
        }
        /**
         * if no current element, this entity belongs to an attribute in these cases, it is an error
         * on the part of the parser to call startEntity but this will help in some cases. See
         * org/xml/sax/ext/LexicalHandler.html#startEntity(java.lang.String) for more information
         */
        if (!atRoot) {
          flushCharacters();
          EntityRef entity = factory.entityRef(name, pub, sys);

          // no way to tell if the entity was from an attribute or element so just assume element
          factory.addContent(getCurrentElement(), entity);
        }
        suppress = true;
      }
    }
  }
Beispiel #9
0
 private void checkForRequiredAttributes(String tagName, Map attributes, Map mappings)
     throws ParserException {
   if (mappings == null) return;
   Iterator i = mappings.entrySet().iterator();
   while (i.hasNext()) {
     Map.Entry e = (Map.Entry) i.next();
     TagMap.AttributeMapping m = (TagMap.AttributeMapping) e.getValue();
     if (null != m && m.getRequired())
       if (null == mappings || null == attributes.get(m.getPropertyName()))
         throw new ParserException(
             "An attribute that maps to property name: "
                 + m.getPropertyName()
                 + " is required and was not specified for tag:"
                 + tagName
                 + "!");
   }
 }
Beispiel #10
0
 private Map attributeMap(String tagName, Attributes atts) throws ParserException {
   if (null == tagName || null == atts) return null;
   Map mapping = null;
   try {
     mapping = (Map) attributeMaps.get(tagName);
   } catch (Exception e) {
     throw new ParserException(
         "Typecast error, unknown element found in attribute list mappings! " + e.getMessage());
   }
   if (null == mapping) return null;
   Map resultMapping = new HashMap();
   for (int i = 0; i < atts.getLength(); i++) {
     String xmlName = atts.getQName(i);
     String value = atts.getValue(i);
     TagMap.AttributeMapping aMap = null;
     try {
       aMap = (TagMap.AttributeMapping) mapping.get(xmlName);
     } catch (Exception e) {
       throw new ParserException(
           "Typecast error, unknown element found in property mapping! " + e.getMessage());
     }
     if (null == aMap)
       throw new ParserException(
           "No attribute mapping specified for attribute: " + xmlName + " in tag: " + tagName);
     String propertyName = aMap.getPropertyName();
     try {
       resultMapping.put(propertyName, aMap.convertValue(value));
     } catch (Exception e) {
       throw new ParserException(
           "Can not convert given value: \""
               + value
               + "\" to specified type: "
               + aMap.getType()
               + " for attribute: "
               + xmlName
               + " in tag: "
               + tagName
               + "! "
               + e.getMessage());
     }
   }
   checkForRequiredAttributes(tagName, resultMapping, mapping);
   addDefaultValues(resultMapping, mapping);
   return resultMapping;
 }
 public MissionInfo getMission(String name) {
   return data.get(name);
 }