Java Code Examples for com.helger.commons.state.ESuccess

The following examples show how to use com.helger.commons.state.ESuccess. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: ph-commons   Source File: GenericJAXBMarshaller.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public final ESuccess write (@Nonnull final JAXBTYPE aObject,
                             @Nonnull final IJAXBMarshaller <JAXBTYPE> aMarshallerFunc)
{
  ValueEnforcer.notNull (aObject, "Object");
  ValueEnforcer.notNull (aMarshallerFunc, "MarshallerFunc");

  try
  {
    final Marshaller aMarshaller = _createMarshaller (getClassLoader ());
    customizeMarshaller (aMarshaller);

    final JAXBElement <JAXBTYPE> aJAXBElement = m_aJAXBElementWrapper.apply (aObject);
    aMarshallerFunc.doMarshal (aMarshaller, aJAXBElement);
    return ESuccess.SUCCESS;
  }
  catch (final JAXBException ex)
  {
    m_aWriteExceptionCallbacks.forEach (x -> x.onException (ex));
  }
  return ESuccess.FAILURE;
}
 
Example 2
Source Project: ph-commons   Source File: IJAXBWriter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Write the passed object to a {@link Writer}.
 *
 * @param aObject
 *        The object to be written. May not be <code>null</code>.
 * @param aWriter
 *        The writer to write to. Will always be closed. May not be
 *        <code>null</code>.
 * @return {@link ESuccess}
 */
@Nonnull
default ESuccess write (@Nonnull final JAXBTYPE aObject, @Nonnull @WillClose final Writer aWriter)
{
  try
  {
    if (USE_JAXB_CHARSET_FIX)
    {
      return write (aObject, SafeXMLStreamWriter.create (aWriter, getXMLWriterSettings ()));
    }
    return write (aObject, TransformResultFactory.create (aWriter));
  }
  finally
  {
    // Needs to be manually closed
    StreamHelper.close (aWriter);
  }
}
 
Example 3
Source Project: ph-commons   Source File: ChannelHelper.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static ESuccess release (@Nullable final FileLock aFileLock)
{
  if (aFileLock != null)
  {
    try
    {
      aFileLock.release ();
      return ESuccess.SUCCESS;
    }
    catch (final IOException ex)
    {
      if (LOGGER.isErrorEnabled ())
        LOGGER.error ("Failed to release object " + aFileLock, ex instanceof IMockException ? null : ex);
    }
  }

  return ESuccess.FAILURE;
}
 
Example 4
Source Project: ph-commons   Source File: MicroWriter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Write a Micro Node to an {@link OutputStream}.
 *
 * @param aNode
 *        The node to be serialized. May be any kind of node (incl.
 *        documents). May not be <code>null</code>.
 * @param aOS
 *        The output stream to write to. May not be <code>null</code>. The
 *        output stream is closed anyway directly after the operation finishes
 *        (on success and on error).
 * @param aSettings
 *        The settings to be used for the creation. May not be
 *        <code>null</code>.
 * @return {@link ESuccess}
 */
@Nonnull
public static ESuccess writeToStream (@Nonnull final IMicroNode aNode,
                                      @Nonnull @WillClose final OutputStream aOS,
                                      @Nonnull final IXMLWriterSettings aSettings)
{
  ValueEnforcer.notNull (aNode, "Node");
  ValueEnforcer.notNull (aOS, "OutputStream");
  ValueEnforcer.notNull (aSettings, "Settings");

  try
  {
    final MicroSerializer aSerializer = new MicroSerializer (aSettings);
    aSerializer.write (aNode, aOS);
    return ESuccess.SUCCESS;
  }
  finally
  {
    StreamHelper.close (aOS);
  }
}
 
Example 5
Source Project: ph-commons   Source File: MicroWriter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Write a Micro Node to a {@link Writer}.
 *
 * @param aNode
 *        The node to be serialized. May be any kind of node (incl.
 *        documents). May not be <code>null</code>.
 * @param aWriter
 *        The writer to write to. May not be <code>null</code>. The writer is
 *        closed anyway directly after the operation finishes (on success and
 *        on error).
 * @param aSettings
 *        The settings to be used for the creation. May not be
 *        <code>null</code>.
 * @return {@link ESuccess}
 */
@Nonnull
public static ESuccess writeToWriter (@Nonnull final IMicroNode aNode,
                                      @Nonnull @WillClose final Writer aWriter,
                                      @Nonnull final IXMLWriterSettings aSettings)
{
  ValueEnforcer.notNull (aNode, "Node");
  ValueEnforcer.notNull (aWriter, "Writer");
  ValueEnforcer.notNull (aSettings, "Settings");

  try
  {
    final MicroSerializer aSerializer = new MicroSerializer (aSettings);
    aSerializer.write (aNode, aWriter);
    return ESuccess.SUCCESS;
  }
  finally
  {
    StreamHelper.close (aWriter);
  }
}
 
Example 6
Source Project: ph-commons   Source File: XMLMapHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Read a mapping from the passed input stream.
 *
 * @param aIS
 *        The input stream to read from. May not be <code>null</code>.
 * @param aTargetMap
 *        The target map to be filled.
 * @return {@link ESuccess#SUCCESS} if the stream could be opened, if it could
 *         be read as XML and if the root element was correct.
 *         {@link ESuccess#FAILURE} otherwise.
 */
@Nonnull
public static ESuccess readMap (@Nonnull @WillClose final InputStream aIS,
                                @Nonnull final Map <String, String> aTargetMap)
{
  ValueEnforcer.notNull (aIS, "InputStream");
  ValueEnforcer.notNull (aTargetMap, "TargetMap");

  try (final InputStream aCloseMe = aIS)
  {
    // open file
    final IMicroDocument aDoc = MicroReader.readMicroXML (aIS);
    if (aDoc != null)
    {
      readMap (aDoc.getDocumentElement (), aTargetMap);
      return ESuccess.SUCCESS;
    }
  }
  catch (final Exception ex)
  {
    if (LOGGER.isWarnEnabled ())
      LOGGER.warn ("Failed to read mapping resource '" + aIS + "'", ex);
  }
  return ESuccess.FAILURE;
}
 
Example 7
Source Project: ph-commons   Source File: XMLListHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Write the passed collection to the passed output stream using the
 * predefined XML layout.
 *
 * @param aCollection
 *        The map to be written. May not be <code>null</code>.
 * @param aOS
 *        The output stream to write to. The stream is closed independent of
 *        success or failure. May not be <code>null</code>.
 * @return {@link ESuccess#SUCCESS} when everything went well,
 *         {@link ESuccess#FAILURE} otherwise.
 */
@Nonnull
public static ESuccess writeList (@Nonnull final Collection <String> aCollection,
                                  @Nonnull @WillClose final OutputStream aOS)
{
  ValueEnforcer.notNull (aCollection, "Collection");
  ValueEnforcer.notNull (aOS, "OutputStream");

  try
  {
    final IMicroDocument aDoc = createListDocument (aCollection);
    return MicroWriter.writeToStream (aDoc, aOS, XMLWriterSettings.DEFAULT_XML_SETTINGS);
  }
  finally
  {
    StreamHelper.close (aOS);
  }
}
 
Example 8
Source Project: ph-commons   Source File: StreamHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Write bytes to an {@link OutputStream}.
 *
 * @param aOS
 *        The output stream to write to. May not be <code>null</code>. Is
 *        closed independent of error or success.
 * @param aBuf
 *        The byte array from which is to be written. May not be
 *        <code>null</code>.
 * @param nOfs
 *        The 0-based index to the first byte in the array to be written. May
 *        not be &lt; 0.
 * @param nLen
 *        The non-negative amount of bytes to be written. May not be &lt; 0.
 * @return {@link ESuccess}
 */
@Nonnull
public static ESuccess writeStream (@WillClose @Nonnull final OutputStream aOS,
                                    @Nonnull final byte [] aBuf,
                                    @Nonnegative final int nOfs,
                                    @Nonnegative final int nLen)
{
  try
  {
    ValueEnforcer.notNull (aOS, "OutputStream");
    ValueEnforcer.isArrayOfsLen (aBuf, nOfs, nLen);

    aOS.write (aBuf, nOfs, nLen);
    aOS.flush ();
    return ESuccess.SUCCESS;
  }
  catch (final IOException ex)
  {
    LOGGER.error ("Failed to write to output stream", _propagate (ex));
    return ESuccess.FAILURE;
  }
  finally
  {
    close (aOS);
  }
}
 
Example 9
Source Project: ph-commons   Source File: BasicTreeItem.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public final ESuccess changeParent (@Nonnull final ITEMTYPE aNewParent)
{
  ValueEnforcer.notNull (aNewParent, "NewParent");

  // no change so far
  if (getParent () == aNewParent)
    return ESuccess.SUCCESS;

  // cannot make a child of this, this' new parent.
  final ITEMTYPE aThis = thisAsT ();
  if (aNewParent.isSameOrChildOf (aThis))
    return ESuccess.FAILURE;

  // add this to the new parent
  if (getParent ().removeChild (aThis).isUnchanged ())
    throw new IllegalStateException ("Failed to remove this from parent!");

  // Remember new parent!
  m_aParent = aNewParent;
  return ESuccess.valueOfChange (aNewParent.internalAddChild (aThis));
}
 
Example 10
Source Project: ph-commons   Source File: ObjectPool.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public ESuccess returnObject (@Nonnull final DATATYPE aItem)
{
  m_aLock.lock ();
  try
  {
    for (int i = 0; i < m_aItems.length; ++i)
      if (m_aUsed[i] && aItem == m_aItems[i])
      {
        m_aUsed[i] = false;

        // Okay, we have one more unused item
        m_aAvailable.release ();
        return ESuccess.SUCCESS;
      }
    if (!isSilentMode ())
      LOGGER.warn ("Object " + aItem + " is not pooled!");
    return ESuccess.FAILURE;
  }
  finally
  {
    m_aLock.unlock ();
  }
}
 
Example 11
Source Project: ph-commons   Source File: AbstractConcurrentCollector.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public final ESuccess queueObject (@Nonnull final DATATYPE aObject)
{
  ValueEnforcer.notNull (aObject, "Object");
  if (isStopped ())
    throw new IllegalStateException ("The queue is already stopped and does not take any more elements");

  return m_aRWLock.writeLockedGet ( () -> {
    try
    {
      m_aQueue.put (aObject);
      return ESuccess.SUCCESS;
    }
    catch (final InterruptedException ex)
    {
      LOGGER.error ("Failed to submit object to queue", ex);
      Thread.currentThread ().interrupt ();
      return ESuccess.FAILURE;
    }
  });
}
 
Example 12
Source Project: ph-commons   Source File: AbstractConcurrentCollector.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public final ESuccess stopQueuingNewObjects ()
{
  return m_aRWLock.writeLockedGet ( () -> {
    try
    {
      // put specific stop queue object
      m_aQueue.put (STOP_QUEUE_OBJECT);
      m_bStopTakingNewObjects = true;
      return ESuccess.SUCCESS;
    }
    catch (final InterruptedException ex)
    {
      LOGGER.error ("Error stopping queue", ex);
      Thread.currentThread ().interrupt ();
      return ESuccess.FAILURE;
    }
  });
}
 
Example 13
Source Project: ph-css   Source File: WikiWriteCSS.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Write a CSS 3.0 declaration to a file using UTF-8 encoding.
 *
 * @param aCSS
 *        The CSS to be written to a file. May not be <code>null</code>.
 * @param aFile
 *        The file to be written. May not be <code>null</code>.
 * @return {@link ESuccess#SUCCESS} if everything went okay, and
 *         {@link ESuccess#FAILURE} if an error occurred
 */
public ESuccess writeCSS30 (final CascadingStyleSheet aCSS, final File aFile)
{
  // 1.param: version to write
  // 2.param: false== non-optimized output
  final CSSWriterSettings aSettings = new CSSWriterSettings (ECSSVersion.CSS30, false);
  try
  {
    final CSSWriter aWriter = new CSSWriter (aSettings);
    // Write the @charset rule: (optional)
    aWriter.setContentCharset (StandardCharsets.UTF_8.name ());
    // Write a nice file header
    aWriter.setHeaderText ("This file was generated by ph-css\nGrab a copy at https://github.com/phax/ph-css");
    // Convert the CSS to a String
    final String sCSSCode = aWriter.getCSSAsString (aCSS);
    // Finally write the String to a file
    return SimpleFileIO.writeFile (aFile, sCSSCode, StandardCharsets.UTF_8);
  }
  catch (final Exception ex)
  {
    LOGGER.error ("Failed to write the CSS to a file", ex);
    return ESuccess.FAILURE;
  }
}
 
Example 14
Source Project: ph-commons   Source File: StreamHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Close the passed object, without trying to call flush on it.
 *
 * @param aCloseable
 *        The object to be closed. May be <code>null</code>.
 * @return {@link ESuccess#SUCCESS} if the object was successfully closed.
 */
@Nonnull
public static ESuccess closeWithoutFlush (@Nullable @WillClose final AutoCloseable aCloseable)
{
  if (aCloseable != null)
  {
    try
    {
      // close stream
      aCloseable.close ();
      return ESuccess.SUCCESS;
    }
    catch (final Exception ex)
    {
      if (!isKnownEOFException (ex))
        LOGGER.error ("Failed to close object " + aCloseable.getClass ().getName (), _propagate (ex));
    }
  }
  return ESuccess.FAILURE;
}
 
Example 15
Source Project: ph-commons   Source File: IJAXBWriter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Write the passed object to an {@link IWritableResource}.
 *
 * @param aObject
 *        The object to be written. May not be <code>null</code>.
 * @param aResource
 *        The result resource to be written to. May not be <code>null</code>.
 * @return {@link ESuccess}
 */
@Nonnull
default ESuccess write (@Nonnull final JAXBTYPE aObject, @Nonnull final IWritableResource aResource)
{
  if (USE_JAXB_CHARSET_FIX)
  {
    final OutputStream aOS = aResource.getOutputStream (EAppend.TRUNCATE);
    if (aOS == null)
      return ESuccess.FAILURE;
    return write (aObject, aOS);
  }
  return write (aObject, TransformResultFactory.create (aResource));
}
 
Example 16
Source Project: ph-commons   Source File: SettingsPersistenceJson.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public ESuccess writeSettings (@Nonnull final ISettings aSettings, @Nonnull @WillClose final OutputStream aOS)
{
  ValueEnforcer.notNull (aOS, "OutputStream");

  try
  {
    final IJsonObject aProps = new JsonObject ();
    for (final Map.Entry <String, Object> aEntry : CollectionHelper.getSorted (aSettings.entrySet (),
                                                                               Comparator.comparing (Map.Entry::getKey)))
    {
      final String sName = aEntry.getKey ();
      final Object aValue = aEntry.getValue ();
      final String sValue = TypeConverter.convert (aValue, String.class);
      aProps.add (sName, sValue);
    }

    final JsonWriterSettings aJWS = new JsonWriterSettings ();
    aJWS.setIndentEnabled (true);
    aJWS.setQuoteNames (false);

    // Does not close the output stream!
    new JsonWriter (aJWS).writeToWriterAndClose (aProps, StreamHelper.createWriter (aOS, m_aCharset));
    return ESuccess.SUCCESS;
  }
  catch (final IOException ex)
  {
    LOGGER.error ("Failed to write settings to JSON file", ex);
    return ESuccess.FAILURE;
  }
  finally
  {
    StreamHelper.close (aOS);
  }
}
 
Example 17
Source Project: ph-commons   Source File: IFileRelativeIO.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Helper function for saving a file with correct error handling.
 *
 * @param sRelativePath
 *        name of the file. May not be <code>null</code>.
 * @param eAppend
 *        Appending mode. May not be <code>null</code>.
 * @param aBytes
 *        the bytes to be written. May not be <code>null</code>.
 * @return {@link ESuccess}
 */
@Nonnull
default ESuccess writeFile (@Nonnull final String sRelativePath,
                            @Nonnull final EAppend eAppend,
                            @Nonnull final byte [] aBytes)
{
  // save to file
  final OutputStream aOS = getOutputStream (sRelativePath, eAppend);
  if (aOS == null)
    return ESuccess.FAILURE;

  // Close the OS automatically!
  return StreamHelper.writeStream (aOS, aBytes);
}
 
Example 18
Source Project: ph-commons   Source File: FileOperations.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Copy the content of the source file to the destination file using
 * {@link InputStream} and {@link OutputStream}.
 *
 * @param aSrcFile
 *        Source file. May not be <code>null</code>.
 * @param aDestFile
 *        Destination file. May not be <code>null</code>.
 * @return {@link ESuccess}
 */
@Nonnull
private static ESuccess _copyFileViaStreams (@Nonnull final File aSrcFile, @Nonnull final File aDestFile)
{
  final InputStream aSrcIS = FileHelper.getInputStream (aSrcFile);
  if (aSrcIS == null)
    return ESuccess.FAILURE;

  try
  {
    final OutputStream aDstOS = FileHelper.getOutputStream (aDestFile, EAppend.TRUNCATE);
    if (aDstOS == null)
      return ESuccess.FAILURE;

    try
    {
      return StreamHelper.copyInputStreamToOutputStream (aSrcIS, aDstOS);
    }
    finally
    {
      StreamHelper.close (aDstOS);
    }
  }
  finally
  {
    StreamHelper.close (aSrcIS);
  }
}
 
Example 19
Source Project: ph-commons   Source File: StreamHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Pass the content of the given reader to the given writer. The reader is
 * automatically closed, whereas the writer stays open!
 *
 * @param aReader
 *        The reader to read from. May be <code>null</code>. Automatically
 *        closed!
 * @param aWriter
 *        The writer to write to. May be <code>null</code>. Not automatically
 *        closed!
 * @return <code>{@link ESuccess#SUCCESS}</code> if copying took place, <code>
 *         {@link ESuccess#FAILURE}</code> otherwise
 */
@Nonnull
public static ESuccess copyReaderToWriter (@WillClose @Nullable final Reader aReader,
                                           @WillNotClose @Nullable final Writer aWriter)
{
  return copyReaderToWriter (aReader,
                             true,
                             aWriter,
                             false,
                             createDefaultCopyBufferChars (),
                             (Long) null,
                             null,
                             (MutableLong) null);
}
 
Example 20
Source Project: ph-commons   Source File: StreamHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Pass the content of the given input stream to the given output stream. The
 * input stream is automatically closed, whereas the output stream stays open!
 *
 * @param aIS
 *        The input stream to read from. May be <code>null</code>.
 *        Automatically closed!
 * @param aOS
 *        The output stream to write to. May be <code>null</code>. Not
 *        automatically closed!
 * @return <code>{@link ESuccess#SUCCESS}</code> if copying took place, <code>
 *         {@link ESuccess#FAILURE}</code> otherwise
 */
@Nonnull
public static ESuccess copyInputStreamToOutputStream (@WillClose @Nullable final InputStream aIS,
                                                      @WillNotClose @Nullable final OutputStream aOS)
{
  return copyInputStreamToOutputStream (aIS,
                                        true,
                                        aOS,
                                        false,
                                        createDefaultCopyBufferBytes (),
                                        (Long) null,
                                        null,
                                        (MutableLong) null);
}
 
Example 21
Source Project: ph-commons   Source File: SAXReader.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ESuccess readXMLSAX (@Nonnull final char [] aXML,
                                   @Nonnegative final int nOfs,
                                   @Nonnegative final int nLen,
                                   @Nonnull final ISAXReaderSettings aSettings)
{
  return readXMLSAX (InputSourceFactory.create (aXML, nOfs, nLen), aSettings);
}
 
Example 22
Source Project: ph-commons   Source File: SAXReader.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ESuccess readXMLSAX (@Nonnull @WillClose final InputStream aIS,
                                   @Nonnull final ISAXReaderSettings aSettings)
{
  ValueEnforcer.notNull (aIS, "InputStream");

  try
  {
    return readXMLSAX (InputSourceFactory.create (aIS), aSettings);
  }
  finally
  {
    StreamHelper.close (aIS);
  }
}
 
Example 23
Source Project: ph-commons   Source File: SAXReader.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ESuccess readXMLSAX (@Nonnull @WillClose final Reader aReader,
                                   @Nonnull final ISAXReaderSettings aSettings)
{
  ValueEnforcer.notNull (aReader, "Reader");

  try
  {
    return readXMLSAX (InputSourceFactory.create (aReader), aSettings);
  }
  finally
  {
    StreamHelper.close (aReader);
  }
}
 
Example 24
Source Project: ph-commons   Source File: StreamHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Pass the content of the given reader to the given writer. The reader and
 * the writer are automatically closed!
 *
 * @param aReader
 *        The reader to read from. May be <code>null</code>. Automatically
 *        closed!
 * @param aWriter
 *        The writer to write to. May be <code>null</code>. Automatically
 *        closed!
 * @return <code>{@link ESuccess#SUCCESS}</code> if copying took place, <code>
 *         {@link ESuccess#FAILURE}</code> otherwise
 */
@Nonnull
public static ESuccess copyReaderToWriterAndCloseWriter (@Nullable @WillClose final Reader aReader,
                                                         @Nullable @WillClose final Writer aWriter)
{
  return copyReaderToWriter (aReader,
                             true,
                             aWriter,
                             true,
                             createDefaultCopyBufferChars (),
                             (Long) null,
                             null,
                             (MutableLong) null);
}
 
Example 25
Source Project: ph-commons   Source File: XMLMapHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ESuccess readMap (@Nonnull final IHasInputStream aISP, @Nonnull final Map <String, String> aTargetMap)
{
  ValueEnforcer.notNull (aISP, "InputStreamProvider");

  return readMap (aISP.getInputStream (), aTargetMap);
}
 
Example 26
Source Project: ph-commons   Source File: XMLMapHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ESuccess readMap (@Nonnull final IMicroElement aParentElement,
                                @Nonnull final Map <String, String> aTargetMap)
{
  ValueEnforcer.notNull (aParentElement, "ParentElement");
  ValueEnforcer.notNull (aTargetMap, "TargetMap");

  try
  {
    // and insert all elements
    for (final IMicroElement eMap : aParentElement.getAllChildElements (ELEMENT_MAP))
    {
      final String sName = eMap.getAttributeValue (ATTR_KEY);
      if (sName == null)
        LOGGER.warn ("Ignoring mapping element because key is null");
      else
      {
        final String sValue = eMap.getAttributeValue (ATTR_VALUE);
        if (sValue == null)
          LOGGER.warn ("Ignoring mapping element because value is null");
        else
        {
          if (aTargetMap.containsKey (sName))
            if (LOGGER.isWarnEnabled ())
              LOGGER.warn ("Key '" + sName + "' is already contained - overwriting!");
          aTargetMap.put (sName, sValue);
        }
      }
    }
    return ESuccess.SUCCESS;
  }
  catch (final Exception ex)
  {
    if (LOGGER.isWarnEnabled ())
      LOGGER.warn ("Failed to read mapping document", ex);
  }
  return ESuccess.FAILURE;
}
 
Example 27
Source Project: ph-commons   Source File: XMLMapHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ESuccess writeMap (@Nonnull final Map <String, String> aMap, @Nonnull final IHasOutputStream aOSP)
{
  ValueEnforcer.notNull (aOSP, "OutputStreamProvider");

  return writeMap (aMap, aOSP.getOutputStream (EAppend.DEFAULT));
}
 
Example 28
Source Project: ph-commons   Source File: XMLListHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ESuccess readList (@Nonnull final IHasInputStream aISP, @Nonnull final Collection <String> aTargetList)
{
  ValueEnforcer.notNull (aISP, "InputStreamProvider");

  return readList (aISP.getInputStream (), aTargetList);
}
 
Example 29
Source Project: ph-commons   Source File: XMLListHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Read a predefined XML file that contains list items.
 *
 * @param aIS
 *        The input stream to read from. May not be <code>null</code>.
 *        Automatically closed no matter whether reading succeeded or not.
 * @param aTargetList
 *        The target collection to be filled. May not be <code>null</code>.
 * @return {@link ESuccess#SUCCESS} if reading succeeded,
 *         {@link ESuccess#FAILURE} if the input stream is no valid XML or any
 *         other error occurred.
 */
@Nonnull
public static ESuccess readList (@Nonnull @WillClose final InputStream aIS,
                                 @Nonnull final Collection <String> aTargetList)
{
  ValueEnforcer.notNull (aIS, "InputStream");
  ValueEnforcer.notNull (aTargetList, "TargetList");

  try
  {
    // open file
    final IMicroDocument aDoc = MicroReader.readMicroXML (aIS,
                                                          new SAXReaderSettings ().setFeatureValues (EXMLParserFeature.AVOID_XXE_SETTINGS));
    if (aDoc != null)
    {
      readList (aDoc.getDocumentElement (), aTargetList);
      return ESuccess.SUCCESS;
    }
  }
  catch (final Exception ex)
  {
    if (LOGGER.isWarnEnabled ())
      LOGGER.warn ("Failed to read list resource '" + aIS + "'", ex);
  }
  finally
  {
    StreamHelper.close (aIS);
  }

  return ESuccess.FAILURE;
}
 
Example 30
Source Project: ph-commons   Source File: XMLListHandler.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ESuccess readList (@Nonnull final IMicroElement aParentElement,
                                 @Nonnull final Collection <String> aTargetList)
{
  ValueEnforcer.notNull (aParentElement, "ParentElement");
  ValueEnforcer.notNull (aTargetList, "TargetList");

  try
  {
    // and insert all elements
    for (final IMicroElement eItem : aParentElement.getAllChildElements (ELEMENT_ITEM))
    {
      final String sValue = eItem.getAttributeValue (ATTR_VALUE);
      if (sValue == null)
        LOGGER.warn ("Ignoring list item because value is null");
      else
        if (!aTargetList.add (sValue))
        {
          if (LOGGER.isWarnEnabled ())
            LOGGER.warn ("Ignoring list item '" + sValue + "' because value is already contained");
        }
    }
    return ESuccess.SUCCESS;
  }
  catch (final Exception ex)
  {
    LOGGER.warn ("Failed to read list document", ex);
  }
  return ESuccess.FAILURE;
}